forked from aaPanel/BaoTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup
195 lines (170 loc) · 5.32 KB
/
backup
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#备份网站
Backup_Site()
{
#准备必要信息
startTime=`date +%s`
myDate=`date +"%Y%m%d_%H%M%S"`
zipName=${1}_${myDate}.zip
backupDir=${backup_path}/site
fileName=$backupDir/$zipName
tmp=`$Sql_Exec -e "SELECT id,path FROM bt_sites WHERE name='$1'"`;
pid=`echo $tmp|awk '{print $3}'`
sitePath=`echo $tmp|awk '{print $4}'`
if [ "$pid" == '' ];then
endDate=`date +"%Y-%m-%d %H:%M:%S"`
log="网站[$1]不存在!"
echo "★[$endDate] $log"
echo '----------------------------------------------------------------------------'
exit
fi
if [ ! -d $backupDir ];then
mkdir -p $backupDir
fi
#取目录名称
oldIFS=$IFS
IFS=/
nameArr=($sitePath)
pathName=${nameArr[@]: -1};
IFS=$oldIFS
#压缩
cd $sitePath
cd ..
zip -r $fileName $pathName > /dev/null
if [ ! -f $fileName ];then
endDate=`date +"%Y-%m-%d %H:%M:%S"`
log="网站[$1]备份失败!"
echo "★[$endDate] $log"
echo '----------------------------------------------------------------------------'
exit
fi
#记录日志
endDate=`date +"%Y-%m-%d %H:%M:%S"`
endTime=`date +%s`
((outTime=($endTime-$startTime)))
log="网站[$1]备份成功,用时[$outTime]秒"
$Sql_Exec -e "INSERT INTO bt_backup(type,name,pid,filename,addtime) VALUES(0,'$zipName',$pid,'$fileName','$endDate')"
$Sql_Exec -e "INSERT INTO bt_logs(type,log,addtime) VALUES('计划任务','$log','$endDate')"
echo "★[$endDate] $log"
echo "|---保留最新的[$2]份备份"
echo "|---文件名:$fileName"
#清理多余备份
tmp=`$Sql_Exec -e "SELECT COUNT(*) FROM bt_backup WHERE type=0 AND pid=$pid"`;
count=`echo $tmp|awk '{print $2}'`
((sum=($count-$2)))
if [ $sum -gt 0 ];then
str=`$Sql_Exec -e "SELECT filename FROM bt_backup WHERE type=0 AND pid=$pid ORDER BY id ASC LIMIT $sum"`
str=`echo $str|sed "s#filename##"|sed "s# ##"`
oldIFS=$IFS
IFS=' '
arr=($str)
for s in ${arr[@]}
do
if [ -f $s ];then
rm -f $s
fi
echo "|---已清理过期备份文件:$s"
$Sql_Exec -e "DELETE FROM bt_backup WHERE filename='$s'" > /dev/null
done
IFS=$oldIFS
fi
echo '----------------------------------------------------------------------------'
}
#备份数据库
Backup_Database()
{
#准备必要信息
startTime=`date +%s`
mysqlRoot=`echo $select |grep mysql_root|awk '{print $3}'`
myDate=`date +"%Y%m%d_%H%M%S"`
sqlName=${1}_${myDate}.sql
zipName=${1}_${myDate}.zip
backupDir=${backup_path}/database
tmp=`$Sql_Exec -e "SELECT id FROM bt_databases WHERE name='$1'"`;
pid=`echo $tmp|awk '{print $2}'`
if [ "$pid" == '' ];then
endDate=`date +"%Y-%m-%d %H:%M:%S"`
log="数据库[$1]不存在!"
echo "★[$endDate] $log"
echo '----------------------------------------------------------------------------'
exit
fi
if [ ! -d $backupDir ];then
mkdir -p $backupDir
fi
#sed -i "s###"
isWrite=`cat /etc/my.cnf|grep 'user=root'`
if [ "${isWrite}" == '' ];then
echo -e "\n[mysqldump]\nuser=root\npassword=$mysqlRoot" >> /etc/my.cnf
else
sed -i "s#password=.*#password=$mysqlRoot#" /etc/my.cnf
fi
#导出数据库
cd $backupDir
/www/server/mysql/bin/mysqldump -uroot -p$mysqlRoot -R $1 > $sqlName
sed -i "/\[mysqldump\]/d" /etc/my.cnf
sed -i "/user=root/d" /etc/my.cnf
sed -i "/password=.*/d" /etc/my.cnf
sed -i "s/quick/\[mysqldump\]\nquick/" /etc/my.cnf
if [ ! -f $sqlName ];then
endDate=`date +"%Y-%m-%d %H:%M:%S"`
log="数据库[$1]备份失败!"
echo "★[$endDate] $log"
echo '----------------------------------------------------------------------------'
exit
fi
#压缩
zip -r $zipName $sqlName > /dev/null
rm -f $sqlName
#记录日志
endDate=`date +"%Y-%m-%d %H:%M:%S"`
endTime=`date +%s`
((outTime=($endTime-$startTime)))
fileName=$backupDir/$zipName
log="数据库[$1]备份成功,用时[$outTime]秒"
$Sql_Exec -e "INSERT INTO bt_backup(type,name,pid,filename,addtime) VALUES(1,'$zipName',$pid,'$fileName','$endDate')" > /dev/null
$Sql_Exec -e "INSERT INTO bt_logs(type,log,addtime) VALUES('计划任务','$log','$endDate')" > /dev/null
echo "★[$endDate] $log"
echo "|---保留最新的[$2]份备份"
echo "|---文件名:$fileName"
#清理多余备份
tmp=`$Sql_Exec -e "SELECT COUNT(*) FROM bt_backup WHERE type=1 AND pid=$pid"`;
count=`echo $tmp|awk '{print $2}'`
((sum=($count-$2)))
if [ $sum -gt 0 ];then
str=`$Sql_Exec -e "SELECT filename FROM bt_backup WHERE type=1 AND pid=$pid ORDER BY id ASC LIMIT $sum"`
str=`echo $str|sed "s#filename##"|sed "s# ##"`
oldIFS=$IFS
IFS=' '
arr=($str)
for s in ${arr[@]}
do
if [ -f $s ];then
rm -f $s
fi
echo "|---已清理过期备份文件:$s"
$Sql_Exec -e "DELETE FROM bt_backup WHERE filename='$s'" > /dev/null
done
IFS=$oldIFS
fi
echo '----------------------------------------------------------------------------'
}
#准备基础信息
action=$1
name=$2
num=$3
dbPwd=`cat /www/wwwroot/default/conf/sql.config.php |grep 'DB_PWD'|awk '{print $3}'|sed "s#'##g"|sed "s#,##g"`
dbName='bt_default'
Sql_Exec="/www/server/mysql/bin/mysql -u$dbName -p$dbPwd --default-character-set=utf8 -D $dbName"
select=`$Sql_Exec -e "SELECT mysql_root,backup_path FROM bt_config WHERE id=1"`
backup_path=`echo $select |grep backup_path|awk '{print $4}'`
case "${action}" in
'site')
Backup_Site $name $num
;;
'database')
Backup_Database $name $num
;;
esac