-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql.sh
More file actions
48 lines (47 loc) · 1.13 KB
/
mysql.sh
File metadata and controls
48 lines (47 loc) · 1.13 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
#!/bin/bash
user="server username";
pass="server password";
dest="/backup/";
skip="mysql temp test information_schema performance_schema or_exception_database";
skiptable="database_name/exception_table_name"; #for example mydb/log cuase backup all table on mydb except log table.
today="$(date +"%Y_%m_%d")";
dest=${dest}${today}
if [ ! -d "$dest" ]; then
mkdir $dest
fi
dbs="$(mysql -u $user -p$pass -Bse 'show databases;')"
for db in $dbs
do
skipdb=-1
if [ "$skip" != "" ];
then
for i in $skip
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi
if [ "$skipdb" == "-1" ];then
file=${dest}/${db}_$(date +"%H_%M_%S")
if [ ! -d "$file" ]; then
mkdir $file
fi
tables="$(mysql --skip-column-names -u $user -p$pass $db -e 'show tables;')";
for table in $tables
do
skiptableflag=-1;
if [ "$skiptable" != "" ];
then
for j in $skiptable
do
[ "${db}/${table}" == "$j" ] && skiptableflag=1 || :
done
fi
if [ "$skiptableflag" == "-1" ];then
tmp_file="$file/$table.sql.gz"
mysqldump -u $user -p$pass $db $table | gzip > $tmp_file
#sleep 1
fi
done
fi
done
echo "$today-$(date +"%H_%M_%S") done!";