通过腾讯COS备份网站数据

3,333次阅读

共计 3048 个字符,预计需要花费 8 分钟才能阅读完成。

为什么选用 COS

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

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

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

创建 COS

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

通过腾讯 COS 备份网站数据

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

通过腾讯 COS 备份网站数据

安装 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

正文完
 0
Blood.Cold
版权声明:本站原创文章,由 Blood.Cold 于2019-04-29发表,共计3048字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。