-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
110 lines (82 loc) · 1.81 KB
/
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
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
root_backup_dir='/var/data/backup'
mysql_user=
mysql_pw=
ignored_dbs="information_schema performance_schema mysql"
# params from call
type=$1
versions=$2
# check parameters
if [ ! $1 ]; then
echo "backup dir has to be defined" >&2;
exit 1;
fi
if [ ! $2 ]; then
echo "backups count has to be defined" >&2;
exit 1;
fi
backup_dir="$root_backup_dir/$1"
versions=$2
if [ ! -d ${dir} ]; then
echo "backup_dir $dir does not exist" >&2;
exit 1;
fi
if ! echo ${versions} | egrep -q '^[0-9]+$'; then
echo "backups count should be number" >&2;
exit 1
fi
if [ ${versions} -lt 1 ]; then
echo "versions count (second parameter) should be greater than 0" >&2;
exit 1;
fi
#########
# create tmp dir and dump
now=`date +%Y-%m-%d_%H:%M:%S`
tmp_dir="/tmp/${now}"
mkdir ${tmp_dir}
cd ${tmp_dir}
dbs=`mysql -u${mysql_user} -p${mysql_pw} -Bse "show databases"`
for db in ${dbs}
do
for ignored in ${ignored_dbs}
do
if [ ${ignored} = ${db} ]; then
continue 2;
fi
done
echo "dumping DB $db";
mysqldump -u${mysql_user} -p${mysql_pw} ${db} > ${db}.sql
done
######
# zip
echo "zipping backup"
cd /tmp
zip_file="${now}.zip"
zip -r ${zip_file} "${now}"
echo "removing tmp file"
rm -r "${now}"
#######
# copy backup
dir="$backup_dir/$zip_file"
echo "coping backup to ${dir}"
cp ${zip_file} ${dir}
last_path="$root_backup_dir/last.zip"
echo "moving backup to $last_path"
mv ${zip_file} ${last_path}
#########
# remove old backups
backup_count=`ls ${backup_dir}/* | wc -l`
to_delete=$(($backup_count-$versions-1))
if [ ${to_delete} -lt 1 ]; then
exit;
fi;
echo "Deleting $to_delete of $backup_count existing backups";
i=0
for f in ${backup_dir}/*
do
if [ ${i} -eq ${to_delete} ]; then
break
fi
echo "Deleting ${f}"
rm ${f}
i=$((i+1))
done