導讀
“在騰訊雲團隊協助下,經過7x24小時的努力,微盟資料已經全面找回,并且公布商家賠償計劃。”--刷爆朋友圈的資訊
科技界的圈子并不小,承包朋友圈,也可承包微網誌熱搜。微網誌字數有限,僅僅能看到事情結果。朋友圈的公衆号有詳細叙述事情起伏經過,看得人心潮澎湃。
騰訊雲團隊在此次資料恢複中表現的硬核技術實力,是我們透過衆多公關文必須看到的東西。抛開一個個租賃的計費賬單,這也是雲廠商真正的價值所在之一。
當然,令人激動的消息并不是事情的結束。微盟事件後,所有人開始積極思考,該做哪些事情提高系統資料安全。
權限賬戶分級、資料容災、法律嚴懲等讨論近期也是不少。
思路推進,自然考慮使用各雲廠商提供的各種資料安全解決方法。
例如騰訊雲資料庫,本身提供高可用架構的同時,也提供完備的周期自動備份方案。如果使用的是雲資料庫,那麼日常備份的任務無需額外的操作。
目前最完備的系統安全方案中,一定有一條最安全的方法,是保證雲資料庫每日全量與增量資料完整備份的同時,儲存1到n份副本到本地或其他儲存媒體中,并且定期檢查備份可用,做周期的備份恢複演習。
當然各雲廠商已經烙好了大餅,切好了小塊,最後每一口還是要自己張嘴吃的。
本文解決其中一個最葉子結點的小事情,以實作能夠儲存多份備份副本到本地或其他存儲媒體中。
即以雲資料庫CDB for Mysql 為例,通過雲API 接口實作自動下載下傳全量備份檔案和binlog日志到本地伺服器。
實作方法大綱如下:
1. 安裝開發工具集(SDK)環境
2. 使用api 3.0 explorer 調用騰訊雲API 3.0 接口,快速生成SDK代碼
3. 編寫語言腳本,截取有效備份檔案下載下傳連結
4. 調用接口實作下載下傳
文末提供python 代碼示例,供騰訊雲使用者參考使用。
下面通過示例闡述操作步驟,示例語言python。
步驟 1.安裝SDK,可選語言包括Python,Java,PHP,Go等。下載下傳連結請檢視:https://github.com/TencentCloud
點選連結下載下傳 Tencent Cloud SDK 3.0 for Python 的zip 包,并且根據頁面提示的步驟安裝SDK環境。 https://github.com/TencentCloud/tencentcloud-sdk-python
root@VM_93_197_centos ~# mkdir /sdk_python
root@VM_93_197_centos ~# cd /sdk_python
root@VM_93_197_centos sdk_python# ls
tencentcloud-sdk-python-master.zip
root@VM_93_197_centos sdk_python# unzip tencentcloud-sdk-python-master.zip
root@VM_93_197_centos sdk_python# cd tencentcloud-sdk-python-master/
root@VM_93_197_centos tencentcloud-sdk-python-master# python setup.py install
步驟 2. 使用騰訊雲API 3.0 explorer 工具,調用接口DescribeBackups(查詢資料備份檔案清單)獲得備份檔案的内/外網下載下傳位址,并生成SDK可執行代碼。
在騰訊雲提供的API 3.0 explorer 工具頁面,根據提示資訊,填寫必要參數,即可線上調用測試參數和接口。
雲API 3.0 explorer 位址:https://console.cloud.tencent.com/api/explorer?Product=cdb&Version=2017-03-20&Action=DescribeBackups&SignVersion=
接口DescribeBackups 參考文檔:https://cloud.tencent.com/document/product/236/15842
參數說明 (官方文檔中亦有):
個人秘鑰:安全憑證,包括 SecretId 和 SecretKey
Region: 執行個體所在地域,例如華東地域(上海)
InstanceId: 執行個體的辨別ID,例如cdb-abcdefg
Offset: 偏移量,例如0
Limit: 分頁大小,例如1
注意事項:
使用者必須嚴格保管安全憑證,避免洩露,否則将危及财産安全。如已洩漏,請立刻禁用該安全憑證。
附:申請安全憑證的具體步驟如下:
- 登入 騰訊雲管理中心控制台。
- 前往 雲API密鑰 的控制台頁面。
- 在 雲API密鑰 頁面,單擊【建立密鑰】即可以建立一對密鑰。
使用API 3.0 explorer 操作圖示:

點選【線上調用】測試參數和接口
點選【代碼生成】快速獲得可執行代碼
步驟 3.
複制雲api explorer 生成的python 代碼到SDK環境中,不需要任何改動,運作即可獲得接口DescriberBackups 的調用結果。
root@VM_93_197_centos v20190711# python /API/Mysql/backup.py
{"TotalCount": 14, "RequestId": "050382e1-exxx-41cb-a510-xxxxx89c", "Items": [{"Status": "SUCCESS", "FinishTime": "2020-02-28 09:06:21", "Name": "xxxxxxbackup_20200228000511.xb", "Creator": "SYSTEM", "IntranetUrl": "https://mysql-database-backup-sh-xxxxxxxxFoctet-stream", "InternetUrl": "https://mysql-database-backup-sh-xxxxxxxFoctet-stream", "Date": "2020-02-28 09:06:17", "BackupId": 157109760, "Type": "physical", "Size": 1818642237}]}
前3步均不需要任何代碼能力,簡單實用。
步驟4.
需要編寫腳本實作自動下載下傳。
基本實作方法為:雲api 接口輸出json格式,包含備份檔案的下載下傳連結intraneturl。通過python腳本截取有效的url, 并且通過調用wget 進行下載下傳。更多檔案路徑和備份檔案管理的邏輯可以根據需要修改添加。
步驟5. 重複1-4步,實作binlog檔案檢視以及下載下傳。
其中使用接口DescribeBinlogs(查詢二進制日志備份檔案清單)獲得binlog檔案的内/外網下載下傳位址
接口DescribeBinlogs 參考文檔:https://cloud.tencent.com/document/api/236/15843
本文提供完整的python 代碼示例,供參考。可實作下載下傳備份檔案以及binlog日志。完整代碼請檢視文末。
代碼使用方法:
1. 修改secretId、secredKey、region、instanceIds清單,其中多個instanceId使用逗号分隔。
參數i,j 設定每個執行個體下載下傳的備份數量。例如,設定i=0,j=2 即下載下傳每個執行個體最新的2個備份/binlog增量日志。
2. 在備份目錄下運作腳本,即可下載下傳多個執行個體的最新備份檔案/日志binlog檔案到目前目錄下。
示例:
root@VM_93_197_centos CDB# pwd
/Backups/CDB
root@VM_93_197_centos CDB# python /API/Mysql/cdb_backup.py
start to download the backup of cdb-xxxx
100% .......................................................................... 1179758 / 1179758
...
root@VM_93_197_centos CDB# ls -lrt
total 4640
-rw-r--r-- 1 root root 1180947 Mar 3 23:35 cdb182xx_backup_20200303000425.xb
-rw-r--r-- 1 root root 1180950 Mar 3 23:35 cdb182xx_backup_20200302000428.xb
-rw-r--r-- 1 root root 1179759 Mar 3 23:35 cdb184xx_backup_20200303000458.xb
-rw-r--r-- 1 root root 1179758 Mar 3 23:35 cdb184xx_backup_20200302000459.xb
root@VM_93_197_centos CDB#
root@VM_93_197_centos CDB# python /API/Mysql/cdb_binlog.py
start to download the backup of cdb-xxx
100% ........................................................................ 353 / 353
...
root@VM_93_197_centos CDB# ls -lrt
total 16
-rw-r--r-- 1 root root 353 Mar 4 00:21 cdb184xx_binlog_mysqlbin.000002
-rw-r--r-- 1 root root 241 Mar 4 00:21 cdb184xx_binlog_mysqlbin.000003
-rw-r--r-- 1 root root 848 Mar 4 00:21 louxxx_binlog_mysqlbin.000016
-rw-r--r-- 1 root root 848 Mar 4 00:21 louxxx_binlog_mysqlbin.000015
Python 完整代碼示例
///cdb_backup.python 完整代碼
# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cdb.v20170320 import cdb_client, models
import json
import os
import wget
# Please modify with your own Key.
secretId=""
secretKey=""
# The region of your instance locates in.
region=""
# Seperate instance IDs with comma ","
instanceIds = ["",""
# defind how many backups to get,i=offset,j=limit
i =
j =
# Get the result of DescriberBackup
def GetUrlofBackups(each_instance):
try:
cred = credential.Credential(secretId,secretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "cdb.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = cdb_client.CdbClient(cred, region, clientProfile)
req = models.DescribeBackupsRequest()
params = {"InstanceId":each_instance,"Offset":i,"Limit":j}
param = json.dumps(params)
req.from_json_string(param)
resp = client.DescribeBackups(req)
url_output = resp.to_json_string()
result = json.loads(url_output)
return result
except TencentCloudSDKException as err:
print(err)
# start to download the backups of each instance
for ID in instanceIds:
try:
backupresult = GetUrlofBackups(ID)
for each_result in backupresult["Items"]:
url = each_result["IntranetUrl"]
notification="start to download the backup of " +ID
print(notification)
wget.download(url)
except TencentCloudSDKException as err:
print(err)
///cdb_binlog.python 完整代碼
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cdb.v20170320 import cdb_client, models
import wget
import json
import os
# Please modify with your own Key.
secretId=""
secretKey=""
# The region of your instance locates in.
region=""
# Seperate instance IDs with comma ","
instanceIds = ["",""]
# defind how many backups to get,i=offset,j=limit
i =
j =
# Get the result of DescribeBinlogs
def GetUrlofBinlog(each_instance):
try:
cred = credential.Credential(secretId,secretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "cdb.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = cdb_client.CdbClient(cred, region, clientProfile)
req = models.DescribeBinlogsRequest()
params = {"InstanceId":each_instance,"Offset":i,"Limit":j}
param = json.dumps(params)
req.from_json_string(param)
resp = client.DescribeBinlogs(req)
DescribeBinlog = resp.to_json_string()
result = json.loads(DescribeBinlog)
return result
except TencentCloudSDKException as err:
print(err)
# start to download the binlog of each instance
for ID in instanceIds:
try:
binlogresult = GetUrlofBinlog(ID)
for each_result in binlogresult["Items"]:
url = each_result["IntranetUrl"]
notification="start to download the backup of " +ID
print(notification)
wget.download(url)
except TencentCloudSDKException as err:
print(err)