Backup redis and upload to S3

The company's database uses redis, and the current backup strategy is daily backup and hourly backup. The backup file is placed directly in a folder on the database server. One problem with this is that if the cloud host is recycled or accidentally deleted, the backup data will be lost, posing a security risk. Therefore, two solutions are now adopted: scheduled backup + uploading backup files to AWS S3.

​Note : Since we are using the AWS cloud host, we can just install awscli directly. There is no guarantee whether the cloud hosts of other manufacturers will work.

install aws-cli

#安装awscli
yum install  awscli
#配置aws config
aws configure 

#Here you need to enter the corresponding Access key ID and Secret access key as prompted.
Insert image description here

#新建文件
vim test.txt
#上传测试
aws s3 cp test.txt s3://backup

Upload successful
Insert image description here

Write redis daily backup script

vim redis_rdb_copy_hourly.sh

#!/bin/sh 
cur_date=`date +%Y%m%d`
rm -rf /home/redis/snapshotting/$cur_date
mkdir /home/redis/snapshotting/$cur_date
cp /opt/module/redis/bin/dump.rdb /home/redis/snapshotting/$cur_date
#上传到S3桶
aws s3 cp /home/redis/snapshotting/$cur_date/dump.rdb s3://backup/redis/$cur_date/

del_date=`date -d -1month +%Y%m%d`
#只保留1个月以内的备份文件
rm -rf /home/redis/snapshotting/$del_date

Write redis hourly backup script

vim redis_rdb_copy_daily.sh

#!/bin/sh 
cur_date=`date +%Y%m%d%k`
rm -rf /home/redis/snapshotting/$cur_date
mkdir /home/redis/snapshotting/$cur_date
cp /opt/module/redis/bin/dump.rdb /home/redis/snapshotting/$cur_date

#上传到S3桶
aws s3 cp /home/redis/snapshotting/$cur_date/dump.rdb s3://backup/redis/$cur_date/

del_date=`date -d -48hour +%Y%m%d%k`
#只保留48小时内的小时备份文件
rm -rf /home/redis/snapshotting/$del_date

File authorization
chmod 777 redis_rdb_copy_daily.sh
chmod 777 redis_rdb_copy_hourly.sh

Join a scheduled task

crontab -e

0 * * * * sh /home/redis/copy/redis_rdb_copy_hourly.sh
0 0 * * * sh /home/redis/copy/redis_rdb_copy_daily.sh

After waiting for the scheduled task to be executed, go to the AWS background to verify whether the upload is successful. It should be noted that the permissions of the S3 account must be configured properly.

Note: This solution is also applicable to other scenarios that require backup to S3 cloud storage.

Guess you like

Origin blog.csdn.net/XX777666/article/details/113179738