1. 程式人生 > 實用技巧 >MySQL 8.0.21 使用xtrabackup增量備份

MySQL 8.0.21 使用xtrabackup增量備份

#!/bin/env bash

# create user xtrabackup@127.0.0.1 identified by 'cruces';
# grant backup_admin,process,reload,lock tables,replication client,replication slave on *.* to xtrabackup@127.0.0.1;
# flush privilges;


base_dir=/backup
log_file=${base_dir}/xtrabackup.log
defaults_file=/etc/my.cnf
user=root
password
='' port=3306 function reorganize() { find $base_dir -maxdepth 1 ! -path $base_dir -mtime +30 -type d -exec rm -rf {} \; } function full_backup() { backup_dir=${base_dir}/`date +%F` mkdir $backup_dir xtrabackup --defaults-file=$defaults_file --backup --user=$user --password="
$password" --port=$port --target-dir=$backup_dir/base &>> $log_file reorganize } function incremental_backup() { day_of_week=`date +%u` echo $day_of_week backup_dir=${base_dir}/`date -d 'last monday' +%F` echo $backup_dir target_dir=${backup_dir}/incremental_$((day_of_week-1
)) echo $target_dir if [[ $day_of_week -eq 2 ]];then incremental_basedir=${backup_dir}/base echo $incremental_basedir else incremental_basedir=${backup_dir}/incremental_$((day_of_week-2)) if [ ! -d $incremental_basedir ];then incremental_basedir=${base_dir}/`date -d 'last monday' +%F`/base fi echo $incremental_basedir fi xtrabackup --defaults-file=$defaults_file --backup --user=$user --password="$password" --port=$port --target-dir=${target_dir} \ --incremental-basedir=${incremental_basedir} &>> $log_file } function alternate() { full_dir=${base_dir}/`date -d 'last monday' +%F`/base if [ ! -d ${full_dir} ];then echo -e '\n\n\n\n' >> $log_file echo "############################################################################" >> $log_file echo `date +"%F %T %u %A %B %j"` A New Round backup begin!!!!!!!!!!>> $log_file echo "############################################################################" >> $log_file echo "############################################################################" >> $log_file echo `date +"%F %T %u %A %B %j"` full backup starting >> $log_file echo "############################################################################" >> $log_file xtrabackup --defaults-file=$defaults_file --backup --user=$user --password="$pasword" --port=$port --target-dir=$full_dir &>> $log_file reorganize exit 0 fi } function main() { if [[ `date +%u` -eq 1 ]];then echo -e '\n\n\n\n' >> $log_file echo "############################################################################" >> $log_file echo `date +"%F %T %u %A %B %j"` A New Round backup begin!!!!!!!!!!>> $log_file echo "############################################################################" >> $log_file echo "############################################################################" >> $log_file echo `date +"%F %T %u %A %B %j"` full backup starting >> $log_file echo "############################################################################" >> $log_file full_backup else alternate echo -e '\n' >> $log_file echo "############################################################################" >> $log_file echo `date +"%F %T %u %A %B %j"` incremental backup starting >> $log_file echo "############################################################################" >> $log_file incremental_backup fi } main