2、计划任务
root@DBSlave:~# cat /etc/cron.d/general #mysql backup 0 6 * * * root python /scripts/mysql_slave_backup.py 0 12 * * * root python /scripts/mysql_slave_backup.py 0 18 * * * root python /scripts/mysql_slave_backup.py3、rsync配置
root@DBSlave:~# cat /etc/rsyncd.conf uid = 0 gid = 0 use chroot = yes address = "当前主机公网地址" port = 8638 log file = /var/log/rsync.log pid file = /var/run/rsync.pid hosts allow = "只允许某个IP连接" [databases] path = /data/backup/ comment = databases read only = yes dont compress = *.gz *.bz2 *.zip # 只允许remoteuser用户 auth users = remoteuser secrets file = /etc/rsyncd_users.db root@DBSlave:~# cat /etc/rsyncd_users.db # 格式: 用户名:密码 remoteuser:password二、 本地备份主机配置
1、创建rsync密码文件
root@localhost:~# cat /etc/server.pass remoteuser:password2、 同步脚本
root@localhost:~# cat /scripts/backup.sh #!/bin/bash SSH=$(which ssh) Logs_Dir="/Backup/logs.txt" Rsync=$(which rsync) Project="databases" Dest="/Backup/" IF_DEL_FILE=$(find ${Dest} -type f -mtime +36 -name "*" | wc -l) DEL_FILE=$(find ${Dest} -type f -mtime +36 -name "*") # 删除旧备份 RMOLD(){ if [ "${IF_DEL_FILE}" -gt "0" ] then for filename in ${DEL_FILE} do rm -f ${filename} done rmdir ${Dest}* # 删除空目录 fi } # 执行同步命令 Backup(){ echo "### ---------- datetime : `date +%F-%H-%M-%S` ---------- ###" >> ${Logs_Dir} echo "# start rsync" >> ${Logs_Dir} ${Rsync} -azH --password-file=/etc/server.pass --bwlimit=300 --port=8638 remoteuser@数据库rsync监听的IP地址::${Project} ${Dest} &>> ${Logs_Dir} echo "### end rsync ---------- dateime : `date +%F-%H-%M-%S` ---------- ###" >> ${Logs_Dir} echo -e "\n\n\n\n\n" >> ${Logs_Dir} RMOLD } # 判断如果当前正在同步则不再执行同步命令 IFProcess(){ ps -ef | grep "${Rsync} -azH --password-file=/etc/server.pass --bwlimit=300 --port=8638 remoteuser@数据库rsync监听的IP地址::${Project}" | grep -v "grep" &> /dev/null if [[ "$?" == 0 ]] then exit 0 else Backup fi } IFProcess3、计划任务
root@localhost:~# cat /etc/cron.d/general 01 23 * * * root /bin/sh /scripts/backup.shLinux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx