自动备份MYSQL数据库删除五天前的旧档和上传到其他服务器

来源:互联网 发布:安卓机推荐2017 知乎 编辑:程序博客网 时间:2024/04/27 20:02
参考以下网页:
http://blog.csdn.net/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 databasesand keep newest 5 days backup.
  3. #
  4. # db_user is mysql username
  5. # db_passwd is mysql password
  6. # db_host is mysql host
  7. # —————————–
  8. db_user="root"
  9. db_passwd="ocs"
  10. db_host="localhost"

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

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

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

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

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

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

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

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

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

  48. 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

0 0