使用shell脚本结合innobackupex自动备份mysql innodb数据库

kevin.Zhu 发布于:2013-1-16 12:46 分类:Mysql  有 8 人浏览,获得评论 0 条  


https://www.centos.bz/2013/09/innobackupex-auto-backup-with-shell/


上篇文章介绍了使用Xtrabackup备份mysql innodb数据库,这次给出一个自动备份的shell脚本,脚本每周六作一次完整备份,其它时间每天作一次增量备份。

  1. #!/bin/bash

  2. #检查命令是否执行成功

  3. if_sucess(){

  4. local command="$1"

  5. $command

  6. if [ $? -ne 0 ];then

  7. echo "error."

  8. touch $error_lock_file

  9. exit 1

  10. fi

  11. }

  12. #检查是否存在锁文件,如果存在就退出。

  13. check_locked(){

  14. if [ -f  "$error_lock_file" ];then

  15. echo "error_lock_file found"

  16. exit 1

  17. fi

  18. }

  19.  

  20. #压缩备份一周的完整与增量备份

  21. compress_old_backup(){

  22. if_sucess "tar czf $old_backup_file_temp $full_backup_dir $incr_backup_dir"

  23. rm -f $old_backup_file

  24. if_sucess "mv $old_backup_file_temp $old_backup_file"

  25. rm -rf $full_backup_dir $incr_backup_dir/*

  26. }

  27. #定义相关变量

  28. backup_base=/data/mysql_backup

  29. full_backup_dir=$backup_base/centos_full_backup

  30. incr_backup_dir=$backup_base/centos_incr

  31. sub_incr_dir=$(date +%w)

  32. old_backup_file=$backup_base/old/centos_old.tar.gz

  33. old_backup_file_temp=$backup_base/old/centos_old_temp.tar.gz

  34. user=www.centos.bz

  35. password=123456

  36. defaults_file=/etc/mysql/my.cnf

  37. include_db="centos.*|mysql.*"

  38. error_lock_file=$backup_base/error.locked

  39.  

  40. #程序从这里开始

  41. check_locked

  42. mkdir -p  $incr_backup_dir $backup_base/old

  43.  

  44. #周六就作完整备份,其它时间增量备份。

  45. if [ $sub_incr_dir -eq 6 ];then

  46. [ -d "$full_backup_dir" ] && compress_old_backup

  47. if_sucess "innobackupex --user=$user --password=$password  --defaults-file=$defaults_file --no-timestamp --include=$include_db  $full_backup_dir"

  48. echo "incr_base_dir=$full_backup_dir" > $full_backup_dir/incr_base_dir.txt

  49. else

  50. [ -f "$full_backup_dir/incr_base_dir.txt" ] && . $full_backup_dir/incr_base_dir.txt || exit 1

  51. if_sucess "innobackupex --user=$user --password=$password  --defaults-file=$defaults_file --incremental $incr_backup_dir/$sub_incr_dir --no-timestamp  --include=$include_db --incremental-basedir=$incr_base_dir"

  52. echo "$incr_base_dir=$incr_backup_dir/$sub_incr_dir" > $full_backup_dir/incr_base_dir.txt

  53. fi

转载请标明文章来源:《https://www.centos.bz/2013/09/innobackupex-auto-backup-with-shell/