MySQL简易备份

简易的 MySQL 数据全量日备

备份脚本

首先确定下目录

1
2
3
4
5
6
## 总目录
/mnt/data/backups
## 内容
- mysql-back.list 指定备份数据库名
- mysql_backups.sh 备份脚本
- mysqldump.log 备份日志

mysql-back.list

1
2
3
## 备份数据库名
test_db
test_db2

mysql_backups.sh

备份前推20天内的db数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
#This replication for Ali RDS
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin
export PATH

DAT=`date +%m%d`
HOR=`date +%H`
begin=`date +%F" "%T`
DIR=/mnt/data/backups/db

if [[ ! -d $DIR/$DAT ]];then
mkdir $DIR/$DAT
fi

for i in `cat /mnt/data/backups/mysql-back.list`
do
/bin/mysqldump -uroot -p密码 $i > $DIR/$DAT/$i-$DAT$HOR.sql
done
find /mnt/data/backups/db/* -mtime +20 -exec rm -rf {} \;
end=`date +%F" "%T`

echo "$begin dump mysql -------> $end has done" >> /mnt/data/backups/mysqldump.log

脚本执行

定时任务

vim /etc/crontab

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
01 11 * * * root sh /mnt/data/backups/mysql_backups.sh
手动执行

bash /mnt/data/backups/mysql_backups.sh

0%