Mysql本地备份及远程备份
一.需求:
1,每天6点备份mysql数据;
2,为节省空间,删除超过3个月的所有备份数据;
3,删除超过7天的备份数据,保留3个月里的 10号 20号 30号的备份数据。
本地IP 192.168.1.40
异地IP 192.168.1.39
二.根据需求写出shell脚本:
#!/bin/bash
mysqldump -uroot -p123 --all-databases >/tmp/mysqlbak/`date +%Y%m%d`.sql
find /tmp/mysqlbak/`date +%Y%m%d`.sql -mtime +7 -name ‘*[0-9].sql‘ -exec rm -rf {}\;
find /tmp/mysqlbak/`date +%Y%m%d`.sql -mtime +92 -name ‘*.sql‘ -exec rm -rf {} \;
scp /tmp/mysqlbak/`date +%Y%m%d`.sql 192.168.1.39:/tmp
scp是需要密码验证的:所以我们需要配置密钥验证免密码传输数据:
192.168.1.40机器操作:
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key(/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in/root/.ssh/id_rsa.
Your public key has been saved in/root/.ssh/id_rsa.pub.
The key‘s randomart image is:
+--[ RSA 2048]----+
| +=.. |
| . . .+. |
| o ..+. |
| . =.. |
| o S + E |
| * o o |
| . B . |
| . * |
| o |
+-----------------+
# scp -r id_rsa.pub 192.168.1.39:/root/.ssh/authorized_keys
40主机完成同样步骤
三.加入计划任务:
#crontab –e
添加如下:
* 6 * * * /root/backupmysql.sql
意思为每天早晨6点对数据库进行备份
/root/backup_mysql.sh为脚本所在路径,脚本要注意加上执行权限。
本文出自 “10696204” 博客,请务必保留此出处http://10706204.blog.51cto.com/10696204/1693813
原文地址:http://10706204.blog.51cto.com/10696204/1693813