1、编写MariaDB及Redis数据库备份脚本
vi /sas/jumpserver/tools/db-backup.sh
#!/bin/bash
MariadbPath=/sas/db-backup/mariadb/
RedisPath=/sas/db-backup/redis/
SaveDays=7
echo "[$(date +"%Y-%m-%d %H:%M:%S")] ============ Start database backup task ============" &>> /var/log/db-backup.log
# mariadb backup
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Start mariadb backup task ..." &>> /var/log/db-backup.log
mysqldump -uroot -pXXXXXXXX jumpserver >$MariadbPath$(date +"%Y%m%d-%H%M%S").sql
if [ $? -eq 0 ]; then
# only delete old backup files when new backup success !
ExistBackupFiles=`ls $MariadbPath | wc -l`
if [ $ExistBackupFiles -gt $SaveDays ]; then
# only delete old backup files when exist backup files more then 7 days !
# mariadb backup files clean
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Start mariadb backup files clean task ..." &>> /var/log/db-backup.log
find $MariadbPath -type f -mtime +$SaveDays | xargs rm -rvf &>> /var/log/db-backup.log
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Mariadb backup files clean task finished ! " &>> /var/log/db-backup.log
fi
fi
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Mariadb backup task finished ! " &>> /var/log/db-backup.log
# redis backup
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Start redis backup task ..." &>> /var/log/db-backup.log
redis-cli -a xxxxxxxx SAVE &>> /var/log/db-backup.log
cp -v /ssd/redis/dump.rdb $RedisPath$(date +"%Y%m%d-%H%M%S").rdb &>> /var/log/db-backup.log
if [ $? -eq 0 ]; then
# only delete old backup files when new backup success !
ExistBackupFiles=`ls $RedisPath | wc -l`
if [ $ExistBackupFiles -gt $SaveDays ]; then
# only delete old backup files when exist backup files more then 7 days !
# redis backup files clean
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Start redis backup files clean task ..." &>> /var/log/db-backup.log
find $RedisPath -type f -mtime +$SaveDays | xargs rm -rvf &>> /var/log/db-backup.log
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Redis backup files clean task finished ! " &>> /var/log/db-backup.log
fi
fi
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Redis backup task finished ! " &>> /var/log/db-backup.log
# 添加执行权限
chmod +x /sas/jumpserver/tools/db-backup.sh
2、配置计划任务
vi /etc/crontab
# database backup task
0 2 * * * root /sas/jumpserver/tools/db-backup.sh
Jumpserver高可用集群部署:(八)MariaDB及Redis数据库定时备份