COSCLI 是腾讯云对象存储(Cloud Object Storage,COS)提供的客户端命令行工具。通过 COSCLI 工具,您可以通过简单的命令行指令对您 COS 中的对象(Object)实现批量上传、下载、删除等操作。
腾讯云对象存储COSCLI帮助文档:https://cloud.tencent.com/document/product/436/63143
下载系统对于的版本,放到自己想要的目录下,lnmp 军哥提供了网站和数据库自动备份到本机和远程ftp的脚本,下面的脚本主要是在它的基础上添加了上传到腾讯云cos的脚本。上传到阿里云的也差不多,具体查看阿里云对象存储文档。
按文档要求配置好COSCLI,之后复制以下脚本代码。
脚本:
点击查看完整内容
#!/usr/bin/env bash #Funciont: Backup website and mysql database #Author: licess #Website: https://lnmp.org #IMPORTANT!!!Please Setting the following Values! Backup_Home="/home/www/backup/" MySQL_Dump="/usr/local/mysql/bin/mysqldump" ######~Set Directory you want to backup~###### Backup_Dir=("/www/wwwroot/xxxxx.com") Web_Name="website_cn" ######~Set MySQL Database you want to backup~###### Backup_Database=("database-name") ######~Set MySQL UserName and password~###### MYSQL_UserName='' MYSQL_PassWord='' ######### cos setting Enable_COS=0 # 0: enable; 1: disable #coscli程序路径 COSCLI="/root/backup/coscli" # 需要备份到cos的路径 COS_Path="cos://backup-125112123/backup" ######### ######~Enable Ftp Backup~###### Enable_FTP=1 # 0: enable; 1: disable ######~Set FTP Information~###### FTP_Host='1.2.3.4' FTP_Username='vpser.net' FTP_Password='yourftppassword' FTP_Dir="backup" #Values Setting END! TodayWWWBackup=www-*-$(date +"%Y%m%d").tar.gz TodayDBBackup=db-*-$(date +"%Y%m%d").sql OldWWWBackup=www-*-$(date -d -3day +"%Y%m%d").tar.gz OldDBBackup=db-*-$(date -d -3day +"%Y%m%d").sql Backup_Dir() { Backup_Path=$1 Dir_Name=`echo ${Backup_Path##*/}` Pre_Dir=`echo ${Backup_Path}|sed 's/'${Dir_Name}'//g'` tar zcf ${Backup_Home}www-${Web_Name}-$(date +"%Y%m%d").tar.gz -C ${Pre_Dir} ${Dir_Name} } Backup_Sql() { ${MySQL_Dump} -u$MYSQL_UserName -p$MYSQL_PassWord $1 > ${Backup_Home}db-$1-$(date +"%Y%m%d").sql } OldWWWBackupCos=www-${Web_Name}-$(date -d -3day +"%Y%m%d").tar.gz OldDBBackupCos=db-${MYSQL_UserName}-$(date -d -3day +"%Y%m%d").sql if [ ! -f ${MySQL_Dump} ]; then echo "mysqldump command not found.please check your setting." exit 1 fi if [ ! -d ${Backup_Home} ]; then mkdir -p ${Backup_Home} fi if [ ${Enable_FTP} = 0 ]; then type lftp >/dev/null 2>&1 || { echo >&2 "lftp command not found. Install: centos:yum install lftp,debian/ubuntu:apt-get install lftp."; } fi echo "网站备份中..." for dd in ${Backup_Dir[@]};do Backup_Dir ${dd} done echo "数据库备份中..." for db in ${Backup_Database[@]};do Backup_Sql ${db} done echo "删除旧的文件..." rm -f ${Backup_Home}${OldWWWBackup} rm -f ${Backup_Home}${OldDBBackup} ${COSCLI} rm ${COS_Path}/${OldWWWBackupCos} -f ${COSCLI} rm ${COS_Path}/${OldDBBackupCos} -f if [ ${Enable_FTP} = 0 ]; then echo "Uploading backup files to ftp..." cd ${Backup_Home} lftp ${FTP_Host} -u ${FTP_Username},${FTP_Password} << EOF cd ${FTP_Dir} mrm ${OldWWWBackup} mrm ${OldDBBackup} mput ${TodayWWWBackup} mput ${TodayDBBackup} bye EOF echo "FTP上传完成操作" fi if [ ${Enable_COS} = 0 ]; then echo "开始上传文件到cos" ${COSCLI} cp ${Backup_Home} ${COS_Path} -r echo "上传完成" fi
也可以根据自己的需求开启或关闭对于上传的位置只需修改Enable_COS和Enable_FTP的值。
复制以上脚本,修改对应的路径信息,然后保存为backup.sh 并赋予它执行权限。
之后可以尝试运行,如果可以正常上传到腾讯云COS则说明ok了,然后在添加定时任务
以crontab为例:
00 00 * * * /home/user/backup.sh # 每天0点自动执行备份脚本
本文作者为Mr.Bai,转载请注明。