mariabackup 全量+增量备份脚本+rsync
mariadbbackup.sh
#!/bin/bash ########################################################################### # File Name: mariadbbackup.sh # Author: Paavo # Description: Database backup script # Created Time: 2020/05/01 # Last update time 2020/05/01 #Usage: mariadbbackup.sh <fullbackup|incbackup> ########################################################################### #################### Definition path ################## day=` date +%d` time =$( date +%F_%H:%M) daytime=$( date +%Y%m%d) #数据库base目录 mysqlBaseDir= /app/mariadb #备份目录 BackupDir= /app/backup_mariadb #记录增量备份基于上一个目录的文件 LsnName=${BackupDir} /lsnTag #mysql用户账号密码(base64) mysqlUser= "cm9vdA==" mysqlPassword= "ZHJlYW10ZWNoSVQyMDE1QCkhXg==" ip=`ip add | grep "inet " | grep -vE "127.0.0.1|10.|:" | awk '{print $4}' ` #################### create path #################### if [ ! -d ${BackupDir} ]; then mkdir $BackupDir -p fi #################### mariadbbackup #################### startTime=$( date +%s) #删除备份目录中的所有目录除了某个传入的目录 delete_dir (){ if [ $ # -ne 1 ];then exit 1 fi for i in $( ls ${BackupDir} | grep - v -E "$1|lsnTag" ) do rm -rf ${BackupDir}/${i} done } #检查是否有rsync进程在同步备份目录的数据 check_rsync(){ if ps -elf | grep rsync | grep -q ${BackupDir}; then return 1 else return 0 fi } #备份的实际步骤 dump_mariadb () { if [ $ # -ne 1 ];then exit 1 fi case $1 in "fullbackup" ) FullDir=${BackupDir} /Full- ${daytime} mkdir -p ${FullDir} #备份到本地 ${mysqlBaseDir} /bin/mariabackup --defaults- file =${mysqlBaseDir} /my .cnf --backup --user=$( echo -n ${mysqlUser} |base64 -d) --password= "$(echo -n ${mysqlPassword} |base64 -d)" --port=3306 --target- dir =${FullDir} 2> /app/mysqldump_error .log if [ $? - eq 0 ]; then endTime=$( date +%s) echo "${time}:Full Backup MariaDB OK $[ endTime - startTime ]s" >> /tmp/checksqlback .log echo ${FullDir} > ${LsnName} #同步到备份服务器 rsync -azP --no-iconv ${FullDir} rsync_backup@10.24.67.6::backup24 --password- file = /etc/rsync .password 2>> /tmp/rsync .log if [ $? - eq 0 ]; then echo "${time}: ${FullDir} sync succesful" >> /tmp/checksqlback .log fi #检查是否有其他未跑完的同步进程 check_rsync if [ $? - eq 0 ]; then exceptDir=$( cat ${LsnName}) delete_dir ${exceptDir ##*/} fi else echo "${time}:Full Backup MariaDB Failed" >> /tmp/checksqlback .log fi ;; "incbackup" ) #判断incremental-basedir标记文件是否存在内容 if [ -s ${LsnName} ]; then BaseDir=$( cat ${LsnName}) IncDir=${BackupDir} /Inc- ${daytime} mkdir -p ${IncDir} ${mysqlBaseDir} /bin/mariabackup --defaults- file =${mysqlBaseDir} /my .cnf --backup --user=$( echo -n ${mysqlUser} |base64 -d) --password=$( echo -n ${mysqlPassword} |base64 -d) --port=3306 --target- dir =${IncDir} --incremental-basedir=${BaseDir} 2> /app/mysqldump_error .log if [ $? - eq 0 ]; then endTime=$( date +%s) echo "${time}:Inc Backup MariaDB OK $[ endTime - startTime ]s" >> /tmp/checksqlback .log echo ${IncDir} > ${LsnName} #同步到备份服务器 rsync -azP --no-iconv ${IncDir} rsync_backup@10.24.67.6::backup24 --password- file = /etc/rsync .password 2>> /tmp/rsync .log if [ $? - eq 0 ]; then echo "${time}: ${IncDir} sync succesful" >> /tmp/checksqlback .log fi #检查是否有其他未跑完的同步进程 check_rsync if [ $? - eq 0 ]; then exceptDir=$( cat ${LsnName}) delete_dir ${exceptDir ##*/} fi else echo "${time}:Inc Backup MariaDB Failed" >> /tmp/checksqlback .log fi else echo "${time}:Inc Backup MariaDB Failed ,${LsnName} 不存在或无记录值." >> /tmp/checksqlback .log fi ;; *) echo "${time}:参数有问题!" >> /tmp/checksqlback .log exit 1 ;; esac } dump_mariadb $1 |