码迷,mamicode.com
首页 > 数据库 > 详细

Jumpserver高可用集群部署:(八)MariaDB及Redis数据库定时备份

时间:2020-11-07 15:35:29      阅读:18      评论:0      收藏:0      [点我收藏+]

标签:高可用集群   xxxx   任务   finish   start   more   备份   xxxxxx   计划   

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数据库定时备份

标签:高可用集群   xxxx   任务   finish   start   more   备份   xxxxxx   计划   

原文地址:https://blog.51cto.com/dusthunter/2547213

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!