转移阿里云RDS备份文件到OSS存储脚本
原创
转移阿里云RDS备份文件到OSS存储脚本
逻辑描述
1、首先清理7天前的备份(OSS中)
2、下载rds备份文件到本地
3、上传本地的rds备份文件到oss存储,并删除本地的rds备份文件
#!/bin/bash
# 设置日志文件路径
LOG_FILE="/aliyun/RDS/backups/log/backup_log_$(date +%Y%m%d).log"
# 计算7天前的日期
Date_Tag=$(date -d "7 days ago" +%Y%m%d)
# 删除7天前的备份文件
echo "Deleting backups from $Date_Tag" | tee -a $LOG_FILE
/usr/bin/ossutil64 rm oss://rds-backup-files-hz/ --include "*$Date_Tag*" -rf
if [ $? -eq 0 ]; then
echo "Successfully deleted backups from $Date_Tag" | tee -a $LOG_FILE
else
echo "Failed to delete backups from $Date_Tag" | tee -a $LOG_FILE
fi
# 清理临时文件夹
rm -rf /aliyun/RDS/backups/Tmp_Files/* /aliyun/RDS/backups/Tmp/* /aliyun/RDS/backups/Tmp_Oss/*
# 定义数据库实例ID列表
DBInstanceId_List='
xxxxxxxxxxxxxxxxxx01
xxxxxxxxxxxxxxxxxx02
xxxxxxxxxxxxxxxxxx03
xxxxxxxxxxxxxxxxxx04
'
# 遍历每个数据库实例ID
for ID in $DBInstanceId_List
do
echo "Processing DBInstanceId: $ID" | tee -a $LOG_FILE
# 获取最新的备份信息
aliyun rds DescribeBackups --DBInstanceId "$ID" > /aliyun/RDS/backups/Tmp/$ID
if [ $? -ne 0 ]; then
echo "Failed to get backup information for $ID" | tee -a $LOG_FILE
continue
fi
# 提取最新的备份下载URL和文件名
Download_Url=$(jq -r '.Items.Backup | sort_by(.BackupEndTime) | .[-1].BackupIntranetDownloadURL' /aliyun/RDS/backups/Tmp/$ID)
File_Name=$(jq -r '.Items.Backup | sort_by(.BackupEndTime) | .[-1].BackupIntranetDownloadURL' /aliyun/RDS/backups/Tmp/$ID | grep -oP '(?<=/)\w+_data_\d+_qp\.xb(?=\?)')
if [ -z "$Download_Url" ] || [ -z "$File_Name" ]; then
echo "Failed to extract download URL or file name for $ID" | tee -a $LOG_FILE
continue
fi
# 检查OSS中是否已存在该文件
Oss_File_Check=$(ossutil64 ls oss://rds-backup-files-hz/$ID/$File_Name | grep "Object Number is" | awk '{print $NF}')
if [ "$Oss_File_Check" == "0" ]; then
echo "File $File_Name does not exist in OSS, downloading..." | tee -a $LOG_FILE
wget -O "/aliyun/RDS/backups/Tmp_Files/$File_Name" "$Download_Url"
if [ $? -ne 0 ]; then
echo "Failed to download $File_Name" | tee -a $LOG_FILE
continue
fi
# 上传到OSS
ossutil64 cp "/aliyun/RDS/backups/Tmp_Files/$File_Name" oss://rds-backup-files-hz/$ID/ -u
if [ $? -ne 0 ]; then
echo "Failed to upload $File_Name to OSS" | tee -a $LOG_FILE
continue
fi
else
echo "File $File_Name already exists in OSS, skipping download and upload." | tee -a $LOG_FILE
fi
# 清理临时文件
rm -f /aliyun/RDS/backups/Tmp_Files/$File_Name /aliyun/RDS/backups/Tmp/$ID /aliyun/RDS/backups/Tmp_Oss/$ID
done
echo "Backup process completed." | tee -a $LOG_FILE

浙公网安备 33010602011771号