備份本地庫到遠端Ucloud雲存儲

備份本地mysql數據到Ucloud存儲,支持STANDARD, IA, ARCHIVE 標準存儲,低頻存儲(IA)或者冷存儲(ARCHIVE) 這3中存儲類型mysql

#注意若是,欲使用低頻存儲(IA)或者冷存儲(ARCHIVE),請在命令參數storageclass中指定,支持三種值:STANDARD, IA, ARCHIVE
#注意若是,備份時指定了storageclass參數爲ARCHIVE,須要提早對該文件restore
./filemgr-linux64 --action restore --bucket <bucketName> --key <backupKey>linux

1、演示環境:

centos7.6 X86_64位最小化安裝
mysql爲5.7.28 二進制版本安裝
提早在服務器本地安裝filemgr命令
安裝配置過程參考:
http://www.javashuo.com/article/p-xlcdbtif-dg.htmlredis

提早在服務器上準備好連接Ucloud存儲的配置文件:
[root@mysql-redis105 ~]# cat /data/soft/linux64/beijing-config.cfg
{
"public_key" : "TOKEN_3816a393-5f22-4bc4-a0a4-71a6a2e4",
"private_key" : "af6ecb83-6f34-4bc0-9bcb-880100db5",
"proxy_host" : "www.cn-bj.ufileos.com",
"api_host" : "api.spark.ucloud.cn"
}算法

2、本地mysqldump邏輯備份到遠程:
# 全庫備份sql

mysqldump -A | ./filemgr-linux64 --action stream-upload --bucket <bucketName> --key <all-backupKey> --file stdin --threads <threads> --retrycount <retry> --storageclass <storage-class>

#分庫備份數據庫

mysqldump -B database1 database2 | ./filemgr-linux64 --action stream-upload --bucket <bucketName> --key <part-backupKey> --file stdin --threads <threads> --retrycount <retry> --storageclass <storage-class>

說明:備份文件存儲在ucloud存儲的文件 wbvs_dbbak 下的2020-05-24.db.sql文件 wbvs_dbbak 這個文件要提早在Ucloud控制檯提早建立centos

備份多個庫到遠程存儲:api

[root@mysql-redis linux64]# mysqldump -uroot -p'rRt&8UiJpN3' -B test00001db gogs |filemgr -config /data/soft/linux64/beijing-config.cfg --action stream-upload --bucket wbtdsp-cdn-beijing --key wbvs_dbbak/2020-05-24.db.sql  --file stdin  --retrycount 10 --storageclass IA
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 
2020/05/24 14:35:03.016310 [INFO]StreamUpload blk size:  4194304
2020/05/24 14:35:03.034545 [INFO]part[1] size[46797]
2020/05/24 14:35:03.607199 [INFO]part[0] upload done [ok]
2020/05/24 14:35:03.791041 [INFO]StreamUpload success!!!

恢復遠程存儲備份的文件2020-05-24.db.sql 到本地庫中:服務器

[root@mysql-redis ~]#  filemgr -config /data/soft/linux64/beijing-config.cfg --action stream-download --bucket wbtdsp-cdn-beijing --key wbvs_dbbak/2020-05-24.db.sql --file stdout  --threads 1 --retrycount 2|mysql 
ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.

恢復報錯,緣由是本地服務器庫開啓了Gtid,備份本地數據庫並存儲到遠程雲存儲時,mysqldump沒有關閉關閉gtid,導致gtid信息存儲到了備份的庫文件2020-05-24.db.sql 中,因此在恢復2020-05-24.db.sql 數據到本地庫時,會把2020-05-24.db.sql 文件中的gtid信息也恢復到本地庫裏面,從而致使報錯ide

下面是正確mysqldump邏輯備份和恢復的姿式:

mysqldump -uroot -p'rRt&8UiJpN3' -B test00001db gogs --single-transaction --set-gtid-purged=OFF |filemgr -config /data/soft/linux64/beijing-config.cfg --action stream-upload --bucket wbtdsp-cdn-beijing --key wbvs_dbbak/`date +%F`.db.sql    --retrycount 10 --storageclass IA

提示:在mysqldump的過程當中 --file stdin 參數可加可不加

[root@mysql-redis ~]#  mysqldump -uroot -p'rRt&8UiJpN3' -B test00001db gogs --single-transaction --set-gtid-purged=OFF |filemgr -config /data/soft/linux64/beijing-config.cfg --action stream-upload --bucket wbtdsp-cdn-beijing --key wbvs_dbbak/`date +%F`.db.sql    --file stdin  --retrycount 10 --storageclass IA
mysqldump: [Warning] Using a password on the command line interface can be insecure.
2020/05/25 16:35:20.193048 [INFO]StreamUpload blk size:  4194304
2020/05/25 16:35:20.208247 [INFO]part[1] size[46445]
2020/05/25 16:35:21.713947 [INFO]part[0] upload done [ok]
2020/05/25 16:35:21.879204 [INFO]StreamUpload success!!!

回覆到線程服務器庫裏面:

[root@mysql-redis ~]# filemgr -config /data/soft/linux64/beijing-config.cfg --action stream-download --bucket wbtdsp-cdn-beijing --key wbvs_dbbak/`date +%F`.db.sql --file stdout  --threads 1 --retrycount 2 2>./error.log  |mysql 
[root@mysql-redis ~]# echo $?
0
恢復完成:
[root@mysql-redis ~]# mysql -e "show databases"|egrep 'gogs|test00001db'
gogs
test00001db

3、壓縮備份到遠程存儲:

下面是官方給出的樣例:

#壓縮 備份和恢復
    #備份
    mysqldump -A | gzip | ./filemgr-linux64 --action stream-upload --bucket <bucketName> --key <all-backupKey> --file stdin --threads <threads> --retrycount <retry> --storageclass <storage-class>   
 #恢復
    ./filemgr-linux --action stream-download --bucket <bucketName> --key <all-backupKey> --threads <threads> --retrycount <retry> 2>./error.log | gzip -d | mysql

線上正確的姿式:

[root@mysql-redis ~]#  mysqldump -uroot -p'rRt&8UiJpN3' -B test00001db gogs --single-transaction --set-gtid-purged=OFF |gzip |filemgr -config /data/soft/linux64/beijing-config.cfg --action stream-upload --bucket wbtdsp-cdn-beijing --key wbvs_dbbak/`date +%F`.db.sql.gz   --file stdin  --retrycount 10 --storageclass IA
mysqldump: [Warning] Using a password on the command line interface can be insecure.
2020/05/25 16:54:13.321441 [INFO]StreamUpload blk size:  4194304
2020/05/25 16:54:13.324886 [INFO]part[1] size[7760]
2020/05/25 16:54:14.570615 [INFO]part[0] upload done [ok]
2020/05/25 16:54:14.844144 [INFO]StreamUpload success!!!

[root@mysql-redis ~]# filemgr -config /data/soft/linux64/beijing-config.cfg --action stream-download --bucket wbtdsp-cdn-beijing --key wbvs_dbbak/`date +%F`.db.sql.gz --file stdout  --threads 1 --retrycount 2 2>./error.log  |gzip -d|mysql

4、加密備份和恢復
下面是官方給出的樣例,加密備份是不正確的,測試過程當中一直雲存儲端一直報400錯誤,目前ucloud官網不支持下面的加密備份指令的,並且官網一直沒給出解決方案

備份,使用aes256,指定密碼文件key file在備份路徑中進行壓縮

mysqldump -A | openssl enc -e -aes256 -in - -out - -kfile <key file> | ./filemgr-linux64 --action stream-upload --bucket <bucketName> --key <all-backupKey> --file stdin --threads <threads> --retrycount <retry> --storageclass <storage-class>
# 恢復
./filemgr-linux --action stream-download --bucket <bucketName> --key <all-backupKey> --threads <threads> --retrycount <retry> 2>./error.log | openssl enc -d -aes256 -in - -out - -kfile <key file> | mysql

雖然官方給出上面的方式不支持,可是通過測試下面的方式是能夠實現的:

mysqldump -uroot -p'rRt&8UiJpN3' -B test00001db |gzip - | openssl enc -e -aes256  -a -salt   -k manager1    -out 2.sql.gz.aes

對上面的指令簡單描述:

-B  test00001db  指定備份單個庫
gzip -  對指定備份test00001db 庫進行壓縮備份
openssl enc -e -aes256  -a -salt  :採用openssl -aes256的加密算法方式對gzip 壓縮備份出來的內容進行加密
-k manager1  : 指定openssl 加密過程當中提示要輸入加密密碼的密碼文件。此文件manager1  存放加密密碼,在解密數據備份文件2.sql.gz.aes 時要用到,因此這個密碼文件千萬不能丟。
 -out 2.sql.gz.aes  :把壓縮備份出的數據內容通過加密後,輸出保存在文件2.sql.gz.aes  中。這個文件加密後內容就是一堆字符字母。
-kfile manager  指定加密過程當中存放密碼的文件

下面是正確的操做樣例:

加密:
root@mysql-redis scripts]# mysqldump -uroot -p'rRt&8UiJpN3' -B test00001db |gzip - | openssl enc -e -aes256  -a -salt   -kfile /data/scripts/manager  -out 3.sql.gz.aes
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 

[root@mysql-redis ~]# cat  /data/scripts/manager 
STDwdsuyd12345678
解密:
-kfile manager  指定解密過程當中存放密碼的文件:
openssl enc -d -aes256  -a -salt -kfile  /data/scripts/manager -in 3.sql.gz.aes  -out 3.sql.gz

把加密備份的文件遠程同步到ucloud雲存儲中:

mysqldump -uroot -p'rRt&8UiJpN3' -B test00001db |gzip - | openssl enc -e -aes256  -a -salt   -kfile /data/scripts/manager -out /data/backup/dbbackup/3.sql.gz.aes|filemgr -config /data/soft/linux64/beijing-config.cfg --action sync  --dir ./dbbackup   --trimpath /data/backup/ --bucket wbtdsp-cdn-beijing 
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 
2020/05/26 15:08:22.106045 [INFO]Syncing...
2020/05/26 15:08:22.387666 [INFO]   Sync Successed[ /data/backup/dbbackup/3.sql.gz.aes ] => wbtdsp-cdn-beijing : 3.sql.gz.aes
2020/05/26 15:08:22.410341 [INFO]   Sync Successed[ /data/backup/dbbackup/test/3.sql.gz.aes ] => wbtdsp-cdn-beijing : test/3.sql.gz.aes
========================
|      succ|      fail|
------------------------
|         2|         0|
========================

解密:把存放在雲存儲的文件3.sql.gz.aes 下載到服務器本地進行解密

下面是解密過程:

manager  文件爲存放加密過程的密碼
[root@mysql-redis ~]# cat  /data/scripts/manager 
STDwdsuyd12345678
-kfile manager  指定解密過程當中存放密碼的文件:
openssl enc -d -aes256  -a -salt -kfile /data/scripts/manager -in 3.sql.gz.aes  -out 3.sql.gz
 openssl enc -d -aes256  -a -salt -kfile /data/scripts/manager -in /data/backup/dbbackup/test/3.sql.gz.aes  -out 3.sql.gz

備份全庫機密後到遠程雲存儲:

[root@mysql-redis backup]#  mysqldump -uroot -p'rRt&8UiJpN3' -A |gzip - | openssl enc -e -aes256  -a -salt -kfile /data/scripts/manager -out /data/backup/dbbackup/test/3.sql.gz.aes&&filemgr -config /data/soft/linux64/beijing-config.cfg --action sync  --dir ./dbbackup   --trimpath /data/backup/ --bucket wbtdsp-cdn-beijing 
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 
2020/05/26 15:18:26.216807 [INFO]Syncing...
2020/05/26 15:18:29.047693 [INFO]   Sync Successed[ /data/backup/dbbackup/test/3.sql.gz.aes ] => wbtdsp-cdn-beijing : test/3.sql.gz.aes
========================
|      succ|      fail|
------------------------
|         1|         0|
========================

以上遠程備份本地mysql到雲存儲簡單介紹到此處,歡迎一塊兒交流學些

相關文章
相關標籤/搜索