-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_backup.sh
executable file
·82 lines (67 loc) · 1.74 KB
/
db_backup.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
scriptname=db_backup.sh
DB_USER=root
BACKUP_PREFIX=db_
BACKUP_PATH=$PWD
BACKUPS=5
SUCCESS_CNT=0
ERROR_CNT=0
echo -e "\n*************************"
echo -e "* DB Backup Script v1.1 *"
echo -e "*************************\n"
if [ $(id -u) -ne 0 ]; then
echo -e "DB Backup script must be run as root. Try 'sudo ./$scriptname'\n"
exit 1
fi
if [ -d "$1" ]; then
BACKUP_PATH="$1"
shift
fi
echo -e "Backup path is set to $BACKUP_PATH\n"
if [ ! -d $BACKUP_PATH ]; then
mkdir -p $BACKUP_PATH
fi
while [ "$1" != '' ]
do
DB_NAME="$1"
shift
SQLFILE="${BACKUP_PREFIX}${DB_NAME}.sql"
SQLFILEPATH="${BACKUP_PATH}/${SQLFILE}"
echo -e "Dumping DB $DB_NAME into $SQLFILE"
mysqldump -u $DB_USER $DB_NAME > "$SQLFILEPATH"
if [ $? -gt 0 ]; then
echo -e "Error while dumping Database $DB_NAME"
rm -f "$SQLFILEPATH"
((ERROR_CNT++))
else
gzip -fq "$SQLFILEPATH"
if [ $? -gt 0 ]; then
echo -e "Error while gzipping file $SQLFILEPATH"
((ERROR_CNT++))
else
((SUCCESS_CNT++))
PREVFILE="$SQLFILE.gz"
for ((counter=1;counter<=$BACKUPS;counter++)); do
STAGEFILE="${BACKUP_PREFIX}${DB_NAME}.${counter}.sql.gz"
TMPFILE="${STAGEFILE}.tmp"
if [ -f "${BACKUP_PATH}/${PREVFILE}" ]; then
if [ -f "${BACKUP_PATH}/${STAGEFILE}" ]; then
if [ $counter -eq $BACKUPS ]; then
rm -f "${BACKUP_PATH}/${STAGEFILE}"
else
mv "${BACKUP_PATH}/${STAGEFILE}" "${BACKUP_PATH}/${TMPFILE}"
fi
fi
mv "${BACKUP_PATH}/${PREVFILE}" "${BACKUP_PATH}/${STAGEFILE}"
fi
PREVFILE="$TMPFILE"
done
fi
fi
done
echo -e "Successfully finished ${SUCCESS_CNT} DB Backup(s)!\n"
if [ $ERROR_CNT -gt 0 ]; then
echo -e "But encountered ${ERROR_CNT} error(s)!\n"
exit 1
fi
exit 0