-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_db.sh
More file actions
executable file
·133 lines (112 loc) · 2.94 KB
/
Copy pathbackup_db.sh
File metadata and controls
executable file
·133 lines (112 loc) · 2.94 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
# author: Georg Wallisch
# contact: gw@phpco.de
# copyright: Copyright © 2018 by Georg Wallisch
# credits: Georg Wallisch
# date: 2025/02/07
# deprecated: False
# email: gw@phpco.de
# license: GPL
# maintainer: Georg Wallisch
# status: beta
DB_USER=root
BACKUP_PREFIX=db
SQL_SUFFIX=sql
BACKUP_SUFFIX=$SQL_SUFFIX.gz
COMPRESSOR=/usr/bin/gzip
BACKUPS=5
USAGE="USAGE: sudo $0 /path/to/backup Database1 [[Database2] ..]"
SUCCESS_CNT=0
ERROR_CNT=0
echo -e "\n*************************"
echo -e "* DB Backup Script v1.4 *"
echo -e "*************************\n"
echo -e "Starting backup on $(date)\n"
if [ $(id -u) -ne 0 ]; then
echo -e "DB Backup script must be run as root."
echo -e "${USAGE}\n"
exit 1
fi
if [ "$1" == '' ]; then
echo -e "Backup path not specified."
echo -e "${USAGE}\n"
exit 1
fi
BACKUP_PATH=$1
shift
echo -e "Backup path is set to ${BACKUP_PATH}"
if [ ! -d "$BACKUP_PATH" ]; then
echo "${BACKUP_PATH} does not exist, creating.."
mkdir -p $BACKUP_PATH
if [ ! -d "$BACKUP_PATH" ]; then
echo "Could not create ${BACKUP_PATH}!"
exit 1
fi
fi
if [ $# -eq 0 ]; then
echo -e "No Database(s) specified!\n"
echo -e "${USAGE}\n"
exit 1
fi
while [ $# -ne 0 ] # ne = not equal
do
echo "Making Backup for DB $1"
SQLFILE="${BACKUP_PREFIX}_${1}.0.${SQL_SUFFIX}"
FILE="${BACKUP_PREFIX}_${1}.0.${BACKUP_SUFFIX}"
echo -e "Dumping DB $1 into $SQLFILE"
mysqldump -u $DB_USER $1 > $BACKUP_PATH/$SQLFILE
if [ $? -ne 0 ]; then
echo -e "Error while dumping DB $1!"
if [ -f $BACKUP_PATH/$SQLFILE ]; then
rm -f $BACKUP_PATH/$SQLFILE
fi
((ERROR_CNT++))
else
echo -e "Compressing ${SQLFILE}.."
$COMPRESSOR -fq $BACKUP_PATH/$SQLFILE
if [ $? -ne 0 ]; then
echo -e "Error while compressing ${SQLFILE}!"
((ERROR_CNT++))
else
((SUCCESS_CNT++))
counter=$BACKUPS
PREVFILE="${BACKUP_PREFIX}_${1}.${counter}.${BACKUP_SUFFIX}"
if [ -f $BACKUP_PATH/$PREVFILE ]; then
if [ -w $BACKUP_PATH/$PREVFILE ]; then
echo -e "Removing oldest file $PREVFILE"
rm -f $BACKUP_PATH/$PREVFILE
if [ $? -ne 0 ]; then
echo -e "Error while removing oldest file $PREVFILE!"
fi
else
echo -e "Cannot remove oldest file $PREVFILE! No write permission!"
fi
fi
((counter--))
until [ $counter -lt 0 ]
do
FILE="${BACKUP_PREFIX}_${1}.${counter}.${BACKUP_SUFFIX}"
if [ -f $BACKUP_PATH/$FILE ]; then
if [ -w $BACKUP_PATH/$FILE ]; then
echo -e "Moving $FILE to $PREVFILE"
mv -n $BACKUP_PATH/$FILE $BACKUP_PATH/$PREVFILE
if [ $? -ne 0 ]; then
echo -e "Error while moving $FILE to $PREVFILE!"
fi
else
echo -e "Cannot move $FILE to $PREVFILE! No write permission!"
fi
fi
PREVFILE=$FILE
((counter--))
done
fi
fi
shift # next DB
done
echo -e "Successfully finished ${SUCCESS_CNT} DB Backup(s) by $(date)!\n"
if [ $ERROR_CNT -gt 0 ]; then
echo -e "But encountered ${ERROR_CNT} error(s)!\n"
exit 1
fi
exit 0