-
Notifications
You must be signed in to change notification settings - Fork 11
/
db_dump.sh
executable file
·47 lines (38 loc) · 1.02 KB
/
db_dump.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
#
# Dumps a database.
#
# Usage: db_dump <dbname> <dbuser> <dbpassword> <dumpdir>
#
# Gather script arguments
dbname=$1
dbuser=$2
dbpass=$3
dumpdir=$4
# Check destination directory exists and is writable
if ! [ -d "$dumpdir" ] || ! [ -w "$dumpdir" ]; then
echo "Dump directory does not exist or is not writable"
exit 1
fi
# Determine whether this is daily/weekly/monthly based on current date
dayofmonth=`date +%d` # 01-31
dayofweek=`date +%u` # 1-7 (Monday-Sunday)
if [ $dayofmonth == "01" ]; then
dumpfilename="${dbname}-`date '+%Y-%m-%d'`-monthly.gz"
elif [ $dayofweek == "1" ]; then
dumpfilename="${dbname}-`date '+%Y-%m-%d'`-weekly.gz"
else
dumpfilename="${dbname}-`date '+%Y-%m-%d'`-daily.gz"
fi
dumpfile="$dumpdir/$dumpfilename"
# Delete dump file if it already exists
if [ -e $dumpfile ]; then
rm $dumpfile
fi
# Dump OpenMRS database and gzip result
mysqldump -u$dbuser -p$dbpass $dbname | gzip -c > $dumpfile
# Check dump was successful
if [ ${PIPESTATUS[0]} -ne 0 ]; then
echo "MySQL dump failed"
exit 1
fi