cacti 数据库主备同步脚本

/usr/local/cacti/cacti.sql

#!/bin/bash

cacti 数据库主备同步脚本

Make by Fenei

E-Mail : [email protected]

Date : 07-Feb-2017

#-----------------------------------------------------

本脚本自动备份cacti数据,并通过SCP发送至远程服务器并远程执行数据库恢复操作

要求主服务器可免密钥登录节点服务器

本脚本会自动打包备份的数据库并保存1个月

USER=“cactiuser”
PASSWORD=“cactiuser”
DATABASE=“cacti”
MAIL="[email protected]"
#告警通知邮箱
BACKUP_DIR=/var/www/sqlbackup/
CANAME=cacti.sql
#备份文件存储路径
LOGFILE=/var/log/data_backup.log
#日志文件路径

DATE=date +%Y%m%d-%H%M
ARCHIVE= D A T E . s q l . t a r . g z O P T I O N S = " − u DATE.sql.tar.gz OPTIONS="-u DATE.sql.tar.gzOPTIONS="uUSER -p$PASSWORD $DATABASE --ignore-table=cacti.settings"

–ignore-table=cacti.settings 是备份时跳过settings表,防止各个CA settings 设置一样,可根据需求更改

#判断备份文件存储目录是否存在,否则创建该目录
if [ ! -d B A C K U P D I R ] t h e n m k d i r − p " BACKUP_DIR ] then mkdir -p " BACKUPDIR]thenmkdirp"BACKUP_DIR"
fi

#开始备份之前,将备份信息头写入日记文件
echo " ">> $LOGFILE
echo “--------------------” >> $LOGFILE

#切换至备份目录
cd $BACKUP_DIR
mysqldump $OPTIONS > B A C K U P D I R BACKUP_DIR BACKUPDIRCANAME
#判断数据库备份是否成功
if [[ $? == 0 ]]
then
tar czvf $ARCHIVE $CANAME >> KaTeX parse error: Expected 'EOF', got '&' at position 11: LOGFILE 2>&̲1 echo "[ARCHIVE] Backup Successful!" >> $LOGFILE
else
echo “Database Backup Fail!” >> KaTeX parse error: Expected 'EOF', got '#' at position 9: LOGFILE #̲备份失败后向管理者发送邮件提醒…DATABASE Daily Backup Fail" $MAIL
fi
echo “Backup Process Done”

备份文件为cacti.sql并远程备份到节点服务器

###############电信CA同步###############
scp -P 2141 B A C K U P D I R BACKUP_DIR BACKUPDIRCANAME [email protected]:/tmp/ >> $LOGFILE 2>&1
if [[ ? = = 0 ] ] t h e n e c h o " s c p P r o c e s s d o w n " s s h − t − p 2141 r o o t @ 192.168.1.2 " m y s q l − u ? == 0 ]] then echo "scp Process down" ssh -t -p 2141 [email protected] "mysql -u ?==0]]thenecho"scpProcessdown"sshtp2141root@192.168.1.2"mysqluUSER -p$PASSWORD $DATABASE ssh -t -p 2141 [email protected] “rm -f /tmp/cacti.sql”
echo “shell Process down!”
else
echo “Backup Alter Mail” |mail -s “电信CA数据库同步出错” $MAIL
fi
###############电信CA同步###############

rm -f B A C K U P D I R BACKUP_DIR BACKUPDIRCANAME
#删除31天以上的备份文件
#Cleaning
find $BACKUP_DIR -type f -mtime +31 -name “*.tar.gz” -exec rm -f {} ;
3.创建crond任务计划
echo “58 23 * * * root /var/www/sqlbackup.sh > /dev/null 2>&1” >> /etc/cron.d/backup
service crond restart

你可能感兴趣的:(如履薄冰)