基于XtraBackup做的定时任务,每周日全备、1-6增备。
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# Backup Plan 0 3 * * * /bin/sh /root/database_backup.sh Backup >/dev/null 2>&1 $ cat database_backup.sh #!/bin/bash # Author:Byrd # Version:0.1 # Site:note.t4x.org # Contact:root#t4x.org PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin export PATH backup_dir="/backup/mysql" restore_dir="$2" mysql_cnf="/etc/my.cnf" mysql_user="root" mysql_password="admin" mysql_sock="/usr/local/mysql/tmp/mysql.sock" backup_week_year=$(date +%U) backup_day_week=$(date +%u) backup_application="/usr/local/bin/innobackupex" return_value=0 if [ $# -lt "1" ];then echo "arg error!" fi full_backup(){ if [ ! -d ${backup_dir}/${backup_week_year}/$(date +%F)_0 ]; then mkdir ${backup_dir}/${backup_week_year}/$(date +%F)_0 -p ${backup_application} --defaults-file={mysql_cnf} --no-timestamp --user ${mysql_user} --password ${mysql_password} --socket=${mysql_sock} ${backup_dir}/${backup_week_year}/$(date +%F)_0 >/dev/null 2>&1 if [ `echo $?` -ne ${return_value} ];then echo "Backup Error" > /tmp/MySQL_Innodb_backupex.txt fi fi } incremental_backup(){ if [ ! -d ${backup_dir}/${backup_week_year}/$(date +%F)_${backup_day_week} ]; then mkdir ${backup_dir}/${backup_week_year}/$(date +%F)_${backup_day_week} -p if [ ! -d ${backup_dir}/${backup_week_year}/$(date -d "1 day ago" +%F)_$(($backup_day_week - 1)) ]; then ${backup_application} --defaults-file={mysql_cnf} --no-timestamp --user ${mysql_user} --password ${mysql_password} --socket=${mysql_sock} ${backup_dir}/${backup_week_year}/$(date +%F)_${backup_day_week} >/dev/null 2>&1 if [ `echo $?` -ne ${return_value} ];then echo "Backup Error" > /tmp/MySQL_Innodb_backupex.txt fi else ${backup_application} --defaults-file={mysql_cnf} --no-timestamp --user ${mysql_user} --password ${mysql_password} --socket=${mysql_sock} --incremental --incremental-basedir=${backup_dir}/${backup_week_year}/$(date -d "1 day ago" +%F)_$(($backup_day_week - 1)) ${backup_dir}/${backup_week_year}/$(date +%F)_${backup_day_week} >/dev/null 2>&1 if [ `echo $?` -ne ${return_value} ];then echo "Backup Error" > /tmp/MySQL_Innodb_backupex.txt fi fi fi } case $1 in Backup) if [ ${backup_day_week} -eq "7" ]; then full_backup else incremental_backup fi ;; *) echo "Usage:$0(Backup|Recovery)" ;; esac |
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!