In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to use object storage COS to back up the website on a regular basis. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Analysis of advantages
Private network transfer: like Aliyun OSS, Tencent Cloud COS also supports private network and public network file transfer. For Tencent Cloud servers, private network transfer is definitely the fastest and most stable backup solution!
Free solution: after taking a look at the pricing instructions of Tencent Cloud COS, I found that it is a perfect partner for backup websites, even taking advantage of a loophole (why do you say so?
Take a look at the pricing scheme:
①, inbound traffic is free.
Equivalent to our uploaded file traffic is free, do not distinguish between internal and external networks! I don't want to talk about it on the intranet. I know everything.
2. Prepare for ①, activate COS, and create Bucket
Access Tencent Cloud COS to activate the object storage service, and then create a Bucket as shown below:
III. Backup script
Life is short, I use Python, here continue to choose Python SDK to achieve
①, Environmental preparation
Install Tencent Cloud Python SDK plug-ins in the following order
# install pipyum install python-pip# upgrade pip (the possible version of yum is older) pip install-- upgrade pip# install Tencent Cloud COS SDK plug-in pip install qcloud_cos
If it's not Centos, please take care of it yourself. I won't repeat it here.
②, upload script
Refer to the official SDK documentation and write a simple enough upload script:
#-*-coding: utf-8-*-# UploadFile To Qcloud COSfrom qcloud_cos import CosClientfrom qcloud_cos import UploadFileRequestimport sys# script needs to pass 6 parameters if (len (sys.argv) > 5): appid = int (sys.argv [1]) secret_id = sys.argv [2] .decode ('utf-8') secret_key = sys.argv [3] .decode (' utf-8') bucket = sys.argv [ 4] .decode ('utf-8') domain = sys.argv [5] .decode (' utf-8') filePath = sys.argv [6] .decode ('utf-8') fileName = filePath.split ("/") [- 1] else: print ("Example: python% s appid secret_id secret_key Bucket zhangge.net / data/backup.zip" >
How to use it: save the above code as cos.upload.py and upload it to the server. Execute the following command to start uploading files to OSS:
Python / data/oss.upload.py appid secret_id secret_key Bucket name domain name / data/zhangge.net_1.zip
Where:
1. The three parameters are appid, authentication ID and authentication key, that is, the key information created and forgotten earlier.
The fourth parameter is the Bucket name created earlier, such as mybackup
The fifth parameter is the domain name that needs to be backed up (new: mainly to distinguish)
The sixth parameter is the absolute path of the local file to be uploaded
After execution, you can see the uploaded file in the Object interface of COS:
IV. Regular backup
With the upload script, continue to combine the seven-day cycle backup script shared by Zhang GE blog to achieve circular backup to COS, which is not only safe but also saves COS space.
① 、 Suitable for COS seven-day cycle backup script #! / bin/sh#### Web Backup version 1.0.0 Author: Jager # # For more information please visit https://zhangge.net/5117.html # #-# Copyright ©2016 zhangge.net. All rights reserved. # isDel=nargs=$#isDel=$ {! args} mypassword=123456test-f / etc/profile &. / etc/profile > / dev/null 2 > & 1BaseDirstly $(cd $(dirname $0) & & pwd) zip-- version > / dev/null | | yum install-y zipZIP=$ (which zip) TODAY= `date +% u`PYTHON = $(which python) MYSQLDUMP=$ (which mysqldump) # newly added COS file upload function, please modify appID according to the actual situation Authentication KEY, authentication key and Bucket name!! uploadToCOS () {$PYTHON $baseDir/cos.upload.py appID authentication KEY authentication key Bucket name $1 $2 if [[$?-eq 0]] & [[$isDel "> ②, usage
Modify the above code as follows:
I. Modify the COS upload function code in the above code according to the actual situation, such as key pair and Bucket name (see previous article)
II, replace the mypassword=123456 in the code to set the password for the compressed package. If you do not modify it, the decompression password for the compressed file is 123456.
Then, save the code as backup.sh and upload it to the server (it is recommended to store it in the same directory as the previous python script), such as / data/backup.sh. Finally, add a scheduled task as follows:
# Edit crontab [root@AlyServer ~] # crontab-e# and add the following: # backup database (parameters are: db, domain name, database name, database user name, corresponding password, backup path [optional: y delete local package]) 103 * bash / data/backup.sh db zhangge.net zhangge root 123456 / home/wwwbackup/zhangge.net > / dev/null 2 > & backup website files (parameters are: file, Domain name, website root directory, backup path [optional: y delete local package]) 15 3 * bash / data/backup.sh file zhangge.net / home/wwwroot/zhangge.net / home/wwwbackup/zhangge.net > / dev/null 2 > & press the keyboard esc Enter: wq to save crontab
After all, you can achieve local 7-day circular backup and COS remote backup!
At the end of the article, for the convenience of the vast number of code rookies, we provide a package and download of the scripts involved in this article:
5. Tidbits behind the scenes
When I was messing with SDK, I was abused by Tencent Cloud's documentation at first sight:
Just said how to install the SDK plug-in, and then directly to the def function, and then no more, in the end how to import this SDK? I was stunned.
Finally, the complete DEMO is found in github's simple.py:
Https://github.com/tencentyun/cos-python-sdk/blob/3.3/sample.py
The above content is how to use the object storage COS to back up the website on a regular basis. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.