1、 To write MariaDB And Redis Database backup script

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
# Add execute permission
chmod +x /sas/jumpserver/tools/db-backup.sh

2、 Configure scheduled tasks

vi /etc/crontab
# database backup task
0 2 * * * root /sas/jumpserver/tools/db-backup.sh