考虑到数据的重要性,数据库需要定期的备份,一个比较好的方案记录一下。
采用的备份工具为innobackupex,安装方法可以自行搜索一下,或者我之后写一篇文章,也说下mysql的增量和全量备份原理及方法。
备份计划:
周六进行全量备份,其余时间增量备份,增量的基础是前一天的增量备份,特殊的是周天是对周六的全量备份进行增量。
以下是我编写的两个shell脚本,特此分享出来,不足之处可以在下方留言
备份脚本
运行方法 :sh backup_mysql.sh 1 (这里的1代表一天一次增量备份)此脚本更改好相应的参数以后即可直接运行,按照备份计划跑的。
定时任务设置:每天凌晨选一个时间运行一次,脚本会判断是周几然后执行增量或者全量备份。
#!/bin/bash
#@author:puxu
#
backupdir="/root/backup"
dbuser="root"
dbpassword="" #数据库密码
db_defaults_file="/etc/my.cnf"
full_backup_dir="$backupdir/fullback"
insert_backup_dir="$backupdir/insert_back"
weekday=`date +%w`
insert_back_num=$1 #增量备份几天一次
now=`date +'%Y-%m-%d %H:%M:%S'`
logdir="$backupdir/log"
if [ ! -d $full_backup_dir ]; then
mkdir -p $full_backup_dir
fi
if [ ! -d $insert_backup_dir ]; then
mkdir -p $insert_backup_dir
fi
if [ ! -d $logdir ]; then
mkdir -p $logdir
fi
function full_back()
{
innobackupex --defaults-file=$db_defaults_file --user=$dbuser --password=$dbpassword $full_backup_dir |gzip > /root/mysql_backup/`date +%Y-%m-%d_%H-%M-%S`.tar.gz
if [ $? -eq 0 ];then
echo "$now --Backup--Info-- Fullbackup completed OK!"
return 0
else
echo "$now --Backup--Error-- Fullbackup is Fail: innobackupex commond run error!"
return 2
fi
}
function insert_back()
{
count=`ls $full_backup_dir|wc -l`
if [ $count -eq 0 ]; then
full_back
if [ $? -ne 2 ];then
full_last_filename=$(find $full_backup_dir -name "`date +%Y-%m-%d*`" -print|awk -F / '{print $NF}')
echo "$now --Backup--Info-- Base dir: $full_backup_dir/$full_last_filename, Insertbackup is running !"
innobackupex --defaults-file=$db_defaults_file --user=$dbuser --password=$dbpassword --incremental-basedir=$full_backup_dir/$full_last_filename --incremental $insert_backup_dir
if [ $? -eq 0 ];then
echo "$now --Backup--Info-- Insertbackup completed OK!"
else
echo "$now --Backup--Error-- InsertBackup is Fail: innobackupex commond run error!"
fi
else
return 0
fi
else
timestamp_now=`date +%s`
timestamp_back=`expr $timestamp_now - 3600 \* 24 \* $insert_back_num`
strftime_back=`date -d @$timestamp_back "+%Y-%m-%d"`
insert_last_filename=$(find $insert_backup_dir -name "$strftime_back*" -print|awk -F / '{print $NF}')
insert_count=$(find $insert_backup_dir -name "$strftime_back*" -print|awk -F / '{print $NF}' | wc -l)
if [ $insert_count -ne 0 ];then
echo "$now --Backup--Info-- Base dir: $insert_backup_dir/$insert_last_filename, Insertbackup is running !"
innobackupex --defaults-file=$db_defaults_file --user=$dbuser --password=$dbpassword --incremental-basedir=$insert_backup_dir/$insert_last_filename --incremental $insert_backup_dir
if [ $? -eq 0 ];then
echo "$now --Backup--Info-- Insertbackup completed OK!"
else
echo "$now --Backup--Error-- InsertBackup is Fail: innobackupex commond run error!"
fi
else
echo "$now --Backup--Error-- InsertBackup is Fail: Last $insert_back_num day insert file do not exist!"
return 0
fi
fi
}
function main()
{
if [ $weekday -eq 6 ];then
full_back
elif [ $weekday -eq 7 ]; then
timestamp_now=`date +%s`
timestamp_back=`expr $timestamp_now - 3600 \* 24 \* 1`
strftime_back=`date -d @$timestamp_back "+%Y-%m-%d"`
full_last_filename=$(find $full_backup_dir -name "$strftime_back*" -print|awk -F / '{print $NF}')
full_count=$(find $full_backup_dir -name "$strftime_back*" -print|awk -F / '{print $NF}' | wc -l)
if [ $full_count -ne 0 ];then
echo "$now --Backup--Info-- Base dir: $full_backup_dir/$full_last_filename, Insertbackup is running !"
innobackupex --defaults-file=$db_defaults_file --user=$dbuser --password=$dbpassword --incremental-basedir=$full_backup_dir/$full_last_filename --incremental $insert_backup_dir
if [ $? -eq 0 ];then
echo "$now --Backup--Info-- Insertbackup completed OK!"
else
echo "$now --Backup--Error-- InsertBackup is Fail: innobackupex commond run error!"
fi
else
echo "$now --Backup--Error-- InsertBackup is Fail: Last $insert_back_num day fullback file do not exist!"
return 0
fi
else
insert_back
fi
}
main "$@" >> $logdir/backup_mysql_`date +%Y%m%d`.log
备份数据删除脚本(备份数据需要存放多久自己设定):
运行方法:sh backup_mysql_delete.sh 7 (7代表删除7天前的备份数据)
定时任务设置:每天运行一次此脚本
#!/bin/bash
#@author:puxu
backupdir="/root/backup"
logdir="$backupdir/log"
now=`date +'%Y-%m-%d %H:%M:%S'`
full_backup_dir="$backupdir/fullback"
insert_backup_dir="$backupdir/insert_back"
if [ ! -d $logdir ]; then
mkdir -p $logdir
fi
function rm_backup()
{
timestamp_now=`date +%s`
rm_num=$1
timestamp_rm=`expr $timestamp_now - 3600 \* 24 \* $rm_num`
strftime_rm=`date -d @$timestamp_rm "+%Y-%m-%d"`
ls $full_backup_dir | grep "$strftime_rm" > /dev/null
if [ $? -eq 0 ]; then
filename=$full_backup_dir/$strftime_rm*
echo "$now --delete--info-- $filename will rm..."
rm -rf $full_backup_dir/$strftime_rm*
if [ $? -eq 0 ]; then
echo "$now --Delete--Info-- rm successful!"
else
echo "$now --Delete--Error-- rm fail!"
fi
else
echo "$now --Delete--Info-- $strftime_rm 日期无 full backup 文件!"
fi
ls $insert_backup_dir | grep "$strftime_rm" > /dev/null
if [ $? -eq 0 ]; then
insert_filename=$insert_backup_dir/$strftime_rm*
echo "$now --Delete--Info-- $insert_filename will rm..."
rm -rf $insert_backup_dir/$strftime_rm*
if [ $? -eq 0 ]; then
echo "$now --Delete--Info-- rm successful!"
else
echo "$now --Delete--Error-- rm fail!"
fi
else
echo "$now --Delete--Info-- $strftime_rm 日期无 insert backup 文件!"
fi
}
function main()
{
rm_backup $1
}
main "$@" >> $logdir/delete_mysql_`date +%Y%m%d`.log
网友评论