首先,创建一个shell脚本:
Shell
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
|
#!/bin/bash
mysql_pwd="1234567890"
mysql_dump="/usr/local/mysql/bin/mysqldump"
cur_year=$(date +"%Y")
cur_month=$(date +"%m")
cur_day=$(date +"%d")
#dump_path="/data0/mysql_backup/$cur_year-$cur_month/$cur_day"
arr_databases=(
"db1"
"db2"
"db3"
"db4"
)
for cur_database in ${arr_databases[*]}; do
#mkdir backup path
dump_path="/data0/mysql_backup/$cur_database"
if [ ! -d "$dump_path" ]; then
mkdir -p "$dump_path"
fi
#backup database
$mysql_dump -uroot -p$mysql_pwd $cur_database | gzip > $dump_path/$cur_database-$cur_year-$cur_month-$cur_day.sql.gz
#Delete backup files 10 days ago
cd $dump_path
rm -rf `find . -name '*.sql.gz' -mtime 20`
done
|
可以保存到路径:/data0/scripts/backup_database.sh
1
2
3
|
#设置可执行权限
cd /data0/scripts/
chmod +x *
|
添加计划任务
1
2
3
4
5
6
|
vi /etc/crontab
#添加命令:
01 1 * * * root /data0/scripts/backup_database.sh #每天的01点01分执行
20 2 * * 0 root /data0/scripts/backup_database.sh #每周星期天的02点20分执行
#重启服务
/sbin/service crond restart
|
完事大吉!