使用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脚本,脚本每周六作一次完整备份,其它时间每天作一次增量备份。
-
#!/bin/bash
-
#检查命令是否执行成功
-
if_sucess(){
-
local command="$1"
-
$command
-
if [ $? -ne 0 ];then
-
echo "error."
-
touch $error_lock_file
-
exit 1
-
fi
-
}
-
#检查是否存在锁文件,如果存在就退出。
-
check_locked(){
-
if [ -f "$error_lock_file" ];then
-
echo "error_lock_file found"
-
exit 1
-
fi
-
}
-
-
#压缩备份一周的完整与增量备份
-
compress_old_backup(){
-
if_sucess "tar czf $old_backup_file_temp $full_backup_dir $incr_backup_dir"
-
rm -f $old_backup_file
-
if_sucess "mv $old_backup_file_temp $old_backup_file"
-
rm -rf $full_backup_dir $incr_backup_dir/*
-
}
-
#定义相关变量
-
backup_base=/data/mysql_backup
-
full_backup_dir=$backup_base/centos_full_backup
-
incr_backup_dir=$backup_base/centos_incr
-
sub_incr_dir=$(date +%w)
-
old_backup_file=$backup_base/old/centos_old.tar.gz
-
old_backup_file_temp=$backup_base/old/centos_old_temp.tar.gz
-
user=www.centos.bz
-
password=123456
-
defaults_file=/etc/mysql/my.cnf
-
include_db="centos.*|mysql.*"
-
error_lock_file=$backup_base/error.locked
-
-
#程序从这里开始
-
check_locked
-
mkdir -p $incr_backup_dir $backup_base/old
-
-
#周六就作完整备份,其它时间增量备份。
-
if [ $sub_incr_dir -eq 6 ];then
-
[ -d "$full_backup_dir" ] && compress_old_backup
-
if_sucess "innobackupex --user=$user --password=$password --defaults-file=$defaults_file --no-timestamp --include=$include_db $full_backup_dir"
-
echo "incr_base_dir=$full_backup_dir" > $full_backup_dir/incr_base_dir.txt
-
else
-
[ -f "$full_backup_dir/incr_base_dir.txt" ] && . $full_backup_dir/incr_base_dir.txt || exit 1
-
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"
-
echo "$incr_base_dir=$incr_backup_dir/$sub_incr_dir" > $full_backup_dir/incr_base_dir.txt
-
fi
转载请标明文章来源:《https://www.centos.bz/2013/09/innobackupex-auto-backup-with-shell/》