Select Page

腾讯云COS 对象存储定时远程备份网站和数据库

腾讯云COS 对象存储定时远程备份网站和数据库
做站离不开备份,这个定时数据库备份脚本我个人也一直在,遇到突发情况也好有应对的措施恢复数据。

本脚本Github项目地址:https://github.com/tencentyun/cos-python-sdk/

腾讯云 COS 默认提供大陆的 50GB 免费存储空间,同时目前上传不计费,我们完全可以通过脚本自动定期备份网站数据到腾讯云。当意外出现时,我们可以迅速从备份里面恢复网站数据。

环境准备

安装腾讯云 Python SDK 插件

#安装pip
apt-get install python-pip

#升级pip(yum装的可能版本比较旧)
pip install --upgrade pip

#安装腾讯云COS SDK插件
pip install qcloud_cos_v4

上传脚本,将以下代码保存为 cos.upload.py,并上传到服务器

# -*- coding: utf-8 -*-
# Upload File To Qcloud COS

from qcloud_cos import CosClient
from qcloud_cos import UploadFileRequest
import sys

region = "shanghai" #替换为COS所在区域,可选shanghai(华东)/guangzhou(华南)/tianjin(华北)/chengdu(西南)
#脚本需要传入6个参数
if ( len(sys.argv) > 5 ):
appid = int(sys.argv[1])
secret_id = sys.argv[2].decode('utf-8')
secret_key = sys.argv[3].decode('utf-8')
bucket = sys.argv[4].decode('utf-8')
domain = sys.argv[5].decode('utf-8')
filePath = sys.argv[6].decode('utf-8')
fileName = filePath.split("/")[-1]
else:
print("Example: python %s appid secret_id secret_key Bucket zhangge.net /data/backup.zip" % sys.argv[0])
exit()

#认证和上传
cos_client = CosClient(appid, secret_id, secret_key)
request = UploadFileRequest(bucket, '/%s/%s' % ( domain, fileName ), filePath)
request.set_insert_only(0)
upload_file_ret = cos_client.upload_file(request)
print 'The File %s Upload to Bucket %s : %s ' % ( filePath , bucket , upload_file_ret.get('message') )

使用方法

  • 1~3 个参数是 appid、认证 ID 和认证密钥,也就是前文创建并备忘的密钥信息;
  • 第 4 个参数是前文创建的 Bucket 名称,比如 mybackup
  • 第 5 个参数是需要备份的域名(新增的:主要是为了区分下)
  • 第 6 个参数是要上传的本地文件的绝对路径

python /root/cos.upload.py appid secret_id secret_key Bucket名称 网站域名 /data/jackiesung_1.zip

定时脚本

腾讯云COS 七天循环备份脚本

#!/bin/bash
###################################################################
# Web Backup version 1.0.0 Author: Jager <ge@zhangge.net> #
# For more information please visit https://zhangge.net/5117.html #
#-----------------------------------------------------------------#
# Copyright ©2016 zhangge.net. All rights reserved. #
###################################################################

isDel=n
args=$#
isDel=${!args}
# 设置压缩包解压密码
mypassword=passwd

test -f /etc/profile && . /etc/profile >/dev/null 2>&1
baseDir=$(cd $(dirname $0) && pwd)
zip --version >/dev/null || yum install -y zip
ZIP=$(which zip)
TODAY=`date +%u`
PYTHON=$(which python)
MYSQLDUMP=$(which mysqldump)

# 新增的COS上传文件函数,请按照实际情况修改appID,认证KEY、认证密钥和Bucket名称!!!
uploadToCOS()
{
$PYTHON $baseDir/cos.upload.py appid secret_id secret_key Bucket名称 $1 $2
if [[ $? -eq 0 ]] && [[ "$isDel" == "y" ]]
then
test -f $2 && rm -f $2
fi
}

printHelp()
{
clear
printf '
=====================================Help infomation=========================================
1. Use For Backup database:
The $1 must be [db]
$2: [domain]
$3: [dbname]
$4: [mysqluser]
$5: [mysqlpassword]
$6: [back_path]
$7: [isDel]

For example:./backup.sh db jackiesung.com jackiesung_db jackiesung passwd /data

2. Use For Backup webfile:
The $1 must be [file]:
$2: [domain]
$3: [site_path]
$4: [back_path]
$5: [isDel]

For example:./backup.sh file jackiesung.com /home/wwwroot/www.jackiesung.com /data
=====================================End of Hlep==============================================

'
exit 0
}

backupDB()
{
domain=$1
dbname=$2
mysqluser=$3
mysqlpd=$4
back_path=$5
test -d $back_path || (mkdir -p $back_path || echo "$back_path not found! Please CheckOut Or feedback to zhangge.net..." && exit 2)
cd $back_path
#如果是要备份远程MySQL,则修改如下语句中localhost为远程MySQL地址
$MYSQLDUMP -hlocalhost -u$mysqluser -p$mysqlpd $dbname --skip-lock-tables --default-character-set=utf8 >$back_path/$domain\_db_$TODAY\.sql
test -f $back_path/$domain\_db_$TODAY\.sql || (echo "MysqlDump failed! Please CheckOut Or feedback to zhangge.net..." && exit 2)
$ZIP -P$mypassword -m $back_path/$domain\_db_$TODAY\.zip $domain\_db_$TODAY\.sql && \
uploadToCOS $domain $back_path/$domain\_db_$TODAY\.zip
}

backupFile()
{
domain=$1
site_path=$2
back_path=$3
test -d $site_path || (echo "$site_path not found! Please CheckOut Or feedback to zhangge.net..." && exit 2)
test -d $back_path || (mkdir -p $back_path || echo "$back_path not found! Please CheckOut Or feedback to zhangge.net..." && exit 2)
test -f $back_path/$domain\_$TODAY\.zip && rm -f $back_path/$domain\_$TODAY\.zip
$ZIP -P$mypassword -9r $back_path/$domain\_$TODAY\.zip $site_path && \
uploadToCOS $domain $back_path/$domain\_$TODAY\.zip
}

while [ $1 ]; do
case $1 in
'--db' | 'db' )
backupDB $2 $3 $4 $5 $6
exit
;;
'--file' | 'file' )
backupFile $2 $3 $4
exit
;;
* )
printHelp
exit
;;
esac
done
printHelp

使用方法

将上述代码保存为 backup.sh,上传到服务器(建议存放到和前文 python 脚本相同目录),然后根据实际情况修改,最后添加cron任务。

crontab -e

#然后添加如下内容

0 2 * * 1 /bin/bash /root/backup.sh db jackiesung.com sqlname sqluser sqlpasswd /data > /dev/null 2>&1

0 3 * * 1 /bin/bash /root/backup.sh file jackiesung.com /home/wwwroot/www.jackiesung.com /data > /dev/null 2>&1

#每周一的凌晨2点 自动定时执行命令

赋予脚本权限并执行

chmod +x backup.sh

bash backup.sh

如果遇到提示 bin/sh^M: bad interpreter: No such file or directory,那是因为sh脚本在windows系统下用记事本文件编写的。不同系统的编码格式引起的。

vi backup.sh

用命令修改文件格式
:set ff 或 :set fileformat
可看到如下信息
fileformat=dos 或 fileformat=unix

用命令修改文件格式
:set ff=unix 或 :set fileformat=unix

重启crontab

service crond restart

/etc/init.d/cron restart

参考链接

About The Author

Jackie Sung

Jackie Sung, also known by his nick-name KK, always strives for the best and learn from the best. Influenced by the age of Internet, for the last few years, in his spare time, he's been working and living as a Freelance Web Engineer/ Web Developer & Designer/ Amateur Photographer/ Husky Lover. The work he provides is of highest quality, fully-customized responsive, and tested in a wide range of devices, which typically covers both front-end (HTML5/ CSS3/ JavaScript) and back-end (WordPress as the CMS) responsibilities.

Leave a reply

电子邮件地址不会被公开。 必填项已用*标注

3 × 3 =

Pin It on Pinterest

Share This