通过腾讯COS备份网站数据

为什么选用COS

腾讯有一个东西叫做:COS,也就是对象存储,其实就和以前使用的七牛存储差不多

不过使用COS作为备份还是有优点的,首先康康的网站架设在腾讯云上,只要COS建立的是同一地区的,那么备份时自动使用内网访问,速度快啊!

而且就算VPS不是在腾讯云上,对于COS的免费版本来说也是可以不花钱的,因为入流量是免费的,不区分内外网啊,就算出流量也有每月10G的免费额度,完全够用了!

创建COS

登录 https://console.cloud.tencent.com/cos 腾讯云COS管理控制台,先创建一个Bucket

01.jpg

创建完成后,左侧点击“密钥管理”,根据自己的Bucket分配域名获取相关密钥

02.jpg

安装Python2.7

由于官方sdk运行环境为Python 2.7,所以这里需要自行安装Python 2.7环境

更改脚本

使用下面脚本,将自己的配置信息填进去,地域标识那里,不清楚的请看这里:https://cloud.tencent.com/document/product/436/6224#.E9.80.82.E7.94.A8.E4.BA.8E-json-api,看其中“适用于 JSON API”的说明

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import commands
import tarfile

from datetime import date,datetime,timedelta

from qcloud_cos import CosClient
from qcloud_cos import UploadFileRequest

# Qcloud COS
AppID = 123456789 # APPID
SecretID = u'xxxxxx' # SecretID
SecretKey = u'xxxxx' # SecretKey
Bucket = u'xxxx' # Bucket名称
Region = 'xx' # COS所属地域

# Backup
ToDay = date.today()
OldDay = date.today() - timedelta(5)
BackDir = '/data/backup' # 备份保存路径
LogFile = BackDir + '/backup.log'
DBUser = 'root' # 数据库用户名
DBPass = 'root' # 数据库密码
SQLFile = 'DB_' + str(ToDay.year) + str(ToDay.month) + str(ToDay.day) + '.sql'
DBFile = ('/DB_' + str(ToDay.year) + str(ToDay.month) + str(ToDay.day) + '.tgz').decode('utf-8')
DBOld = BackDir + '/DB_' + str(OldDay.year) + str(OldDay.month) + str(OldDay.day) + '.tgz'
WebDir = '/data/wwwroot' # 网站文件根目录
WebFile = ('/Web_' + str(ToDay.year) + str(ToDay.month) + str(ToDay.day) + '.tgz').decode('utf-8')
WebOld = BackDir + '/Web_' + str(OldDay.year) + str(OldDay.month) + str(OldDay.day) + '.tgz'

print('Checking backup directory...')
try:
    if not os.path.isdir(BackDir):
        os.mkdir(BackDir)
except IOError, err:
    print err
    sys.exit()

print('Delete old backup data...')
try:
    if os.path.isfile(DBOld):
        os.remove(DBOld)

    if os.path.isfile(WebOld):
        os.remove(WebOld)
except IOError, err:
    print err
    sys.exit()

print('Compress your website directory...')
try:
    tar = tarfile.open(BackDir + WebFile, 'w:gz')
    pre_len = len(os.path.dirname(WebDir))
    for root, dir, files in os.walk(WebDir):
        for file in files:
            fullpath = os.path.join(root, file)
            arcname = fullpath[pre_len:].strip(os.path.sep)
            tar.add(fullpath, arcname)
    tar.close()
except IOError, err:
    print err
    sys.exit()

print('Export your databases...')
try:
    cmd = '/usr/local/bin/mysqldump -u' + DBUser + ' -p' + DBPass + ' --all-databases > ' + BackDir + '/' + SQLFile # 若您的mysqldump路径是其他的,请自行更改
    h = commands.getstatusoutput(cmd)
    if h[0] != 0:
        print('Export failed.')
        sys.exit()
    else:
        tar = tarfile.open(BackDir + '/' + DBFile, 'w:gz')
        tar.add(BackDir + '/' + SQLFile, SQLFile)
        tar.close()
        os.remove(BackDir + '/' + SQLFile)
except IOError, error:
    print error
    sys.exit()

print('Prepare upload your backup data to qcloud...')
cos_client = CosClient(AppID, SecretID, SecretKey, Region)
db_request = UploadFileRequest(Bucket, DBFile, BackDir + DBFile, (str(date.today()) + ' backup databases by usebsd.com').decode('utf-8'), 0)
db_ret = cos_client.upload_file(db_request)
print 'Databases file %s upload to bucket %s : %s' % (DBFile, Bucket, db_ret.get('message'))

web_request = UploadFileRequest(Bucket, WebFile, BackDir + WebFile, (str(date.today()) + ' backup website by usebsd.com').decode('utf-8'), 0)
web_ret = cos_client.upload_file(web_request)
print 'Website file %s upload to bucket %s : %s' % (WebFile, Bucket, web_ret.get('message'))

print 'Backup finished...'

创建定时任务

0 2 * * * /usr/local/bin/python /data/backup.py

最后修改:2019 年 04 月 27 日 10 : 28 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论