-
Notifications
You must be signed in to change notification settings - Fork 27
/
sqlbackup.sh
125 lines (117 loc) · 4.4 KB
/
sqlbackup.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
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
#!/bin/bash
# This backup all databases and copy it to other computer
# By Alberto Lepe (www.alepe.com, www.support.ne.jp)
# Created: 17III2009
# Modified: 08X2014 (Up to N modifications)
# 21II2015 (skip "mysql")
# --------------------- CONFIG --------------
STOREIN="/backup/sql/"
#PREMOTE="remote_user@remote_host:${STOREIN}"
PREMOTE=""
RSYNCPR="--stats -irpt"
CHARSET="UTF8"
# To create special user for backup:
# GRANT LOCK TABLES, SHOW DATABASES, SHOW VIEW, SELECT ON *.* TO backup@localhost IDENTIFIED BY '*********';
USER="backup"
PASSWRD="************"
MYSQLPW=" -u${USER} -p${PASSWRD} "
MYSQLPM=" --hex-blob --comments=false --default-character-set=${CHARSET} "
LASTLOG=7
#Choose one option to select which DB(s) to backup (default=ALL):
#DBLIST=""
#DBFILE="${CONFIGS}sqldatabases"
#will generate separated files for each db. Can not be used with ALLDATA
SEPARATEDBAKUPS="yes"
###########################################################################
sqlBackup()
{
TMPFILE="${STOREIN}$1.tmp"
NEWFILE="${STOREIN}$1.sql"
if [ -f ${TMPFILE} ]; then # if file exists...
if [ -s ${TMPFILE} ]; then # and is not empty
TMPCKSM=$(cksum ${TMPFILE} | cut -d " " -f 1,2);
if [ -f "${NEWFILE}.gz" ]; then # check if the gziped file exists
NEWCKSM=$(zcat "${NEWFILE}.gz" | head -n1 | sed 's/[\/\*]//g');
if [ "$NEWCKSM" = "$TMPCKSM" ]; then # Are the same
#echo "No changes found. removing ${TMPFILE}"
rm ${TMPFILE}
else # There was an update
X=$(( LASTLOG - 1 ));
for (( b=X; b>=0; b-- )); do
t=$((b + 1));
BAKFROM="${STOREIN}$1.sql.${b}.gz";
BAKTO="${STOREIN}$1.sql.${t}.gz";
if [ -f "${BAKFROM}" ]; then
#echo "Moving ${BAKFROM} to ${BAKTO} [${b},${t}]";
mv ${BAKFROM} ${BAKTO}
#else
# echo "${BAKFROM} not found.";
fi
done
#echo "Moving ${NEWFILE}.gz to ${STOREIN}$1.sql.0.gz"
mv "${NEWFILE}.gz" "${STOREIN}$1.sql.0.gz"
#echo "Moving ${TMPFILE} to ${NEWFILE}"
mv ${TMPFILE} ${NEWFILE}
sed -i "1i/*${TMPCKSM}*/" ${NEWFILE}
gzip ${NEWFILE}
chmod 600 "${NEWFILE}.gz"
echo -ne " (Updated) "
fi
else #... if not, is the first time
echo "Creating ${NEWFILE}";
mv ${TMPFILE} ${NEWFILE}
sed -i "1i/*${TMPCKSM}*/" ${NEWFILE}
gzip ${NEWFILE}
chmod 600 "${NEWFILE}.gz"
echo -ne " (New) "
fi
fi
fi
echo "Done."
}
#Perform the DUMP
if [ $SEPARATEDBAKUPS = "yes" -a "$1" != "all" ]; then
if [ "$1" != "" ]; then
DATABASES=$1
elif [ ${DBLIST} ]; then
DATABASES=$(echo ${DBLIST} | tr ' ' '\n')
elif [ ${DBFILE} ]; then
if [ -s ${DBFILE} ]; then
DATABASES=$(cat ${DBFILE})
else
echo "Can not open file: ${DBFILE}. Please absolute or relative path of an existing file."
exit 1
fi
else
DATABASES=$(mysql ${MYSQLPW} -Bse 'show databases')
fi
for DB in $DATABASES
do
if [[ "$DB" != "mysql" && "$DB" != "information_schema" && "$DB" != "sys" && "$DB" != "performance_schema" ]]; then
echo -ne "Processing [$DB] ... "
mysqldump ${MYSQLPW} ${MYSQLPM} ${DB} -r ${STOREIN}$DB.tmp
sqlBackup $DB
fi
done
else
if [ "${DBLIST}" != "" -a "$1" != "all" ]; then
OPTIONS="--databases ${DBLIST}"
elif [ "${DBFILE}" != "" -a "$1" != "all" ]; then
if [ -s ${DBFILE} ]; then
OPTIONS="--databases $(tr '\n' ' ' < ${DBFILE})"
else
echo "Can not open file: ${DBFILE}. Please absolute or relative path of an existing file."
exit 1
fi
else
OPTIONS="--all-databases"
fi
echo -ne "Processing [$1] ... "
mysqldump ${MYSQLPW} ${MYSQLPM} ${OPTIONS} -r ${STOREIN}$1.tmp
sqlBackup "all"
fi
if [ "$PREMOTE" != "" ]; then
echo "Synchronizing... "
CMD="rsync ${RSYNCPR} ${STOREIN} ${PREMOTE} > sqlbackup.log"
eval $CMD
fi