自动备份MySQL数据库并删除五天前旧档和上传到其它服务器

本文介绍了一个Shell脚本,用于自动备份MySQL数据库,保留最近五天的备份,并将这些备份文件通过SCP命令上传到另一台服务器。脚本还包括了删除过期备份的功能。

分类: Mysql/postgreSQL

参考以下网页:
http://blog.youkuaiyun.com/daniel_ustc/article/details/9395971
http://www.opsers.org/server/linux-automatically-backup-the-mysql-shell-script-on-a-regular-basis.html

工作原理是使用mysql的mysqldump工具来导出数据库为.sql文件,然后将所有导出的文件打包归档。
然后我们在shell脚本中使用 scp命令把备份文件复制到另外一台备份机器。


一,备份脚本

点击(此处)折叠或打开

  1. #!/bin/sh 
  2. # mysql_backup.sh: backup mysql databases and keep newest 5 days backup. 

  3. # db_user is mysql username 
  4. # db_passwd is mysql password 
  5. # db_host is mysql host 
  6. # —————————– 
  7. db_user="root" 
  8. db_passwd="ocs" 
  9. db_host="localhost" 

  10. # the directory for story your backup file. 
  11. backup_dir="/home/backup/" 

  12. date format for backup file (dd-mm-yyyy) 
  13. time="$(date +"%Y-%m-%d_%H_%M_%S")"
  14. today="$(date +"%Y-%m-%d")"
  15. fpath=$backup_dir$today
  16. echo $fpath 
  17. if [ ! -d $fpath ];then
  18. mkdir $fpath
  19. fi 

  20. # mysql, mysqldump and some other bin's path 
  21. MYSQL="/usr/bin/mysql" 
  22. MYSQLDUMP="/usr/bin/mysqldump" 
  23. MKDIR="/bin/mkdir" 
  24. RM="/bin/rm" 
  25. MV="/bin/mv" 
  26. GZIP="/bin/gzip" 

  27. # the directory for story the newest backup 
  28. test ! -"$backup_dir/bk/" && $MKDIR "$backup_dir/bk/" 

  29. # check the directory for store backup is writeable 
  30. test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0 

  31. get all databases 
  32. all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')" 
  33. for db in $all_db 
  34. do 
  35. $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db --single-transaction | $GZIP -> "$fpath/$db.$time.gz" 
  36. done 

  37. #
  38. cd $backup_dir
  39. tar czf Mysql.$time.tar.gz $today
  40. rm -rf $today
  41. mv Mysql.$time.tar.gz $backup_dir/bk/

  42. #scp to other server
  43. scp $backup_dir/bk/Mysql.$time.tar.gz root@172.16.86.1:/var/www/html/work/bak/

  44. # delete the oldest backup 
  45. #find $backup_dir -type f -mtime +-name "*.gz" -exec rm -{} \; 
  46. find $backup_dir/bk -name "*.gz" -type f -mtime +-exec rm -{} \; > /dev/null 2>&1

  47. exit 0;

二,排程
由于机器本身没有安装cron服务,先透过yum先安装

# yum install vixie-cron
# yum install crontabs
说明:
vixie-cron软件包是cron的主程序;
crontabs软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。
安装vixie-cron时,一般会自动加载安装crontabs
加入开机自动启动:
#chkconfig --level 35 crond on
#/sbin/service crond start


三,设置排程
#vi /etc/crontab
#backup mysql db
3 0 * * * root /home/backup/mysql_backup.sh


四,设置scp
本机执行
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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 fingerprint is:
56:68:73:d4:be:a1:fe:4f:f6:9b:d9:a2:75:o1:d0:a0 root@ng
The key's randomart image is:
+--[ RSA 2048]----+
...
+-----------------+
#cd ~/.ssh
# scp id_rsa.pub root@172.16.1.1:/root/.ssh/authorized_keys


五,恢复数据库
恢复数据备份文件:
非压缩备份文件恢复:
#mysql -u root -p databasename < name2008010103.sql

从压缩文件直接恢复:
# zcat mrbs.2014-04-22_16_16_32.gz | mysql -uroot -ppassword -Dmrbs

其它问题:
# ./mysql_backup.sh 
mysqldump: Got error: 1044: Access denied for user 'root'@'localhost' to database 'information_schema' when using LOCK TABLES

指令增加如下:

$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db --single-transaction | $GZIP -9 > "$fpath/$db.$time.gz"

--single-transaction

--skip-lock-tables

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值