备份本地库到远端ucloud云存储(代码片段)

author author     2022-12-14     278

关键词:

备份本地mysql数据到Ucloud存储,支持STANDARD, IA, ARCHIVE 标准存储,低频存储(IA)或者冷存储(ARCHIVE) 这3中存储类型

#注意如果,欲使用低频存储(IA)或者冷存储(ARCHIVE),请在命令参数storageclass中指定,支持三种值:STANDARD, IA, ARCHIVE
#注意如果,备份时指定了storageclass参数为ARCHIVE,需要提前对该文件restore
./filemgr-linux64 --action restore --bucket <bucketName> --key <backupKey>

一、演示环境:

centos7.6 X86_64位最小化安装
mysql为5.7.28 二进制版本安装
提前在服务器本地安装filemgr命令
安装配置过程参考:
https://blog.51cto.com/wujianwei/2497929

提前在服务器上准备好链接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"

二、本地mysqldump逻辑备份到远程:
# 全库备份

   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控制台提前创建

备份多个库到远程存储:

[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信息也恢复到本地库里面,从而导致报错

下面是正确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

三、压缩备份到远程存储:

下面是官方给出的样例:

#压缩 备份和恢复
    #备份
    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

四、加密备份和恢复
下面是官方给出的样例,加密备份是不正确的,测试过程中一直云存储端一直报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到云存储简单介绍到此处,欢迎一起交流学些

ucloud云存储filemgr命令介绍(代码片段)

官方文档:https://docs.ucloud.cn/ufile/tools/tools/tools_file一、创建存储空间ucloud广州地域二、公私钥地址具体配置公私钥内容如下:公钥:TOKEN_d2baa99-ba02-4570-a952-390f0e0fc27c私钥:d3f33c6b-6a4-4555-9517-b9c0c235a75f[root@localhostsoft]#cat/ 查看详情

git强制回滚代码(代码片段)

本地代码回滚&&强制更新远端代码如果已经提交代码到master分支,此时我提交错了,我需要回滚本地上一次的代码&&强制更新远端代码我们使用gitlog//查看已经提交的日志信息以及id15866…44a9是我第三次提交的... 查看详情

tortoisegittortoisegit将本地库push到远端

...:登录https://github.com/创建一个空项目。  第二步:在本地创建一个新项目,找到项目文件夹,进行gitcreateres 查看详情

本地git仓库和远端关联,创建一个新的项目提交到远端

...端项目     这个不用说了,在github或者本地的gitlab上创建,拿到ssh或者Https地址创建本地仓库     1.新建文件夹,在文件夹内打开终端     2.初始化本地的文件夹为一个git可以... 查看详情

持续集成-git版本回滚(代码片段)

分两种情况:1.本地已经gitadd,gitcommit,但没有gitpush,想要将本地的代码回滚到commit之前gitreset--hardHEAD撤销前一次commitgitreset--hardHEAD^撤销前前一次commitgitreset--hardcommitID撤销到指定commit版本gitreset--hardcommitID撤销到指定commit版本&nb... 查看详情

持续集成-git版本回滚(代码片段)

分两种情况:1.本地已经gitadd,gitcommit,但没有gitpush,想要将本地的代码回滚到commit之前gitreset--hardHEAD撤销前一次commitgitreset--hardHEAD^撤销前前一次commitgitreset--hardcommitID撤销到指定commit版本gitreset--hardcommitID撤销到指定commit版本&nb... 查看详情

git在本地备份与指定不需要管理文件(代码片段)

git在本地备份备份文件夹操作在本地备份文件夹克隆一个不带工作区的仓库:哑协议:gitclone--bare<workspace>/.gityourwork.gitgitclone--barefile:///<workspace>/.gityourwork.git工作文件操作若是没有关联远端仓库,在本地工作文件直接gitp... 查看详情

ucloud云存储filemgr命令介绍(代码片段)

官方文档:https://docs.ucloud.cn/ufile/tools/tools/tools_file一、创建存储空间ucloud广州地域二、公私钥地址具体配置公私钥内容如下:公钥:TOKEN_d2baa99-ba02-4570-a952-390f0e0fc27c私钥:d3f33c6b-6a4-4555-9517-b9c0c235a75f[root@localhostsoft]#cat/data/soft/linux6... 查看详情

阿里云oss对象存储内容增量备份到本地(代码片段)

#!/usr/bin/python3#-*-coding:utf-8-*-#key:OSS文件名称#bucket:存储空间名称importoss2auth=oss2.Auth(‘阿里id‘,‘密码‘)bucket=oss2.Bucket(auth,‘http://oss-cn-shenzhen.aliyuncs.com‘,‘bucket名称‘)#操作完文件自动关闭withopen(‘file.txt‘,‘r+‘)asf:#读取一... 查看详情

使用tar和rsync做简单的kvm备份(代码片段)

使用tar和rsync创建KVM实例的自动备份,并把它存储到远端服务器上。执行如下步骤:(1)创建备份的目录并切换到该目录[email protected]:~#mkdirbackup_kvm1&&cdbackup_kvm1[email protected]:~/backup_kvm1#(2)找到KVM客户机的镜像文件... 查看详情

git创建初始化项目的操作(代码片段)

...项目,如果项目添加了md文件,需要先gitpull拉去代码2、本地代码进行git初始化3、gitremoteaddorigin+远端地址4、gitpulloriginmaster--allow-unrelated-histories//把远程仓库和本地同步,消除差异,主要是创建的时候创建了read.md文件,所以去差... 查看详情

git日常使用命令(代码片段)

...iginmaster#提交到远端在新仓库中创建分支gitbranchnewbranch#在本地创建分支gitcheckoutnewbranch#切换到新分支gitpusho 查看详情

使用宝塔面板如何自动备份数据库和网站代码(代码片段)

...章来自于某框架的知名苦工仙士可的提问,你是怎么定时备份数据库的?基础操作是:宝塔自带的定时任务当中就有备份数据库和网站代码啊  然后就直接选择了备份到服务器磁盘把备份文件放在服务器上也是不太保险,... 查看详情

常用git命令

拉取远端仓库代码:如果本地已经存在文件夹,先cd进去,然后敲命令:gitfetch(作用是拉取远端仓库里的代码)gitmerge(作用是将远端仓库里的代码与本地仓库里的代码合并,如果有冲突会提示,这时候需要讲不正确的代码删除... 查看详情

git分支怎么切换到远端分支

...个人A,B用Git开发,A创建了一个分支test,然后提交,B这里本地只有master分支,看到B提交的test分支为远端分支,这时A想切换到B提交的test分支应该怎么操作?(一).创建本地分支gitcheckout-b新分支名。执行该指令后,会在本地创... 查看详情

uni-app云开发入门

...n(name:\'myCloudFunc\').then((res)=>console.log(res)),打印结果 本地云函数与远端云函数调试的区别云函数:一个后台接口与接口的实现。本地云函数调试是使用本地的接口查询逻辑,此时本地元函数逻辑与远端元函数可能不一样,可... 查看详情

03安装动手学深度学习v2将远端机器端口映射到本地端口(代码片段)

将远端机器端口映射到本地端口1在阿里云服务器2然后再到win10上,按下"田+R"3http://localhost:8888/tree前面的笔记可以查看:https://blog.csdn.net/qq_38689263/article/details/122153528?spm=1001.2014.3001.55011在阿里云服务器直 查看详情

最详细的cloneplugin介绍(代码片段)

...cloneplugin插件,利用克隆插件可以扩展实现:SQL命令进行备份。Slave节点快速搭建。MGR节点快速扩充。而克隆插件的基础功能,可以理解为:可以对本身的实例的InnoDB数据,备份到本服务器的指定目录中。(本地克隆:本地备份... 查看详情