To help prevent data loss, Everyone should back up your data, such as WordPress websites’ content, MySQL databases, to a file. When your data become corrupted or lost, you can restore it from a backup file.
Project url: https://github.com/tencentyun/cos-python-sdk/
Step 1 - System Environment Preparation - Install Tencent Cloud Python SDK Plugin
Install pip
apt-get install python-pip
Update pip
pip install --upgrade pip
Install Tencent cloud COS SDK plugin
pip install qcloud_cos_v4
Save following code as cos.upload.py then upload to server
# -*- coding: utf-8 -*-
# Upload File To Qcloud COS
from qcloud_cos import CosClient
from qcloud_cos import UploadFileRequest
import sys
region = "shanghai" #替换为COS所在区域,可选shanghai(华东)/guangzhou(华南)/tianjin(华北)/chengdu(西南)
#脚本需要传入6个参数
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" % sys.argv[0])
exit()
#认证和上传
cos_client = CosClient(appid, secret_id, secret_key)
request = UploadFileRequest(bucket, '/%s/%s' % ( domain, fileName ), filePath)
request.set_insert_only(0)
upload_file_ret = cos_client.upload_file(request)
print 'The File %s Upload to Bucket %s : %s ' % ( filePath , bucket , upload_file_ret.get('message') )
How to use
python /root/cos.upload.py appid secret_id secret_key Bucket_name Website_domain /data/jackiesung_1.zip
Step 2 - Periodical Script
7-day automatically backup
#!/bin/bash
###################################################################
# 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=n
args=$#
isDel=${!args}
# 设置压缩包解压密码
mypassword=passwd
test -f /etc/profile && . /etc/profile >/dev/null 2>&1
baseDir=$(cd $(dirname $0) && pwd)
zip --version >/dev/null || yum install -y zip
ZIP=$(which zip)
TODAY=`date +%u`
PYTHON=$(which python)
MYSQLDUMP=$(which mysqldump)
# 新增的COS上传文件函数,请按照实际情况修改appID,认证KEY、认证密钥和Bucket名称!!!
uploadToCOS()
{
$PYTHON $baseDir/cos.upload.py appid secret_id secret_key Bucket名称 $1 $2
if [[ $? -eq 0 ]] && [[ "$isDel" == "y" ]]
then
test -f $2 && rm -f $2
fi
}
printHelp()
{
clear
printf '
=====================================Help infomation=========================================
1. Use For Backup database:
The $1 must be [db]
$2: [domain]
$3: [dbname]
$4: [mysqluser]
$5: [mysqlpassword]
$6: [back_path]
$7: [isDel]
For example:./backup.sh db jackiesung.com jackiesung_db jackiesung passwd /data
2. Use For Backup webfile:
The $1 must be [file]:
$2: [domain]
$3: [site_path]
$4: [back_path]
$5: [isDel]
For example:./backup.sh file jackiesung.com /home/wwwroot/www.jackiesung.com /data
=====================================End of Hlep==============================================
'
exit 0
}
backupDB()
{
domain=$1
dbname=$2
mysqluser=$3
mysqlpd=$4
back_path=$5
test -d $back_path || (mkdir -p $back_path || echo "$back_path not found! Please CheckOut Or feedback to zhangge.net..." && exit 2)
cd $back_path
#如果是要备份远程MySQL,则修改如下语句中localhost为远程MySQL地址
$MYSQLDUMP -hlocalhost -u$mysqluser -p$mysqlpd $dbname --skip-lock-tables --default-character-set=utf8 >$back_path/$domain\_db_$TODAY\.sql
test -f $back_path/$domain\_db_$TODAY\.sql || (echo "MysqlDump failed! Please CheckOut Or feedback to zhangge.net..." && exit 2)
$ZIP -P$mypassword -m $back_path/$domain\_db_$TODAY\.zip $domain\_db_$TODAY\.sql && \
uploadToCOS $domain $back_path/$domain\_db_$TODAY\.zip
}
backupFile()
{
domain=$1
site_path=$2
back_path=$3
test -d $site_path || (echo "$site_path not found! Please CheckOut Or feedback to zhangge.net..." && exit 2)
test -d $back_path || (mkdir -p $back_path || echo "$back_path not found! Please CheckOut Or feedback to zhangge.net..." && exit 2)
test -f $back_path/$domain\_$TODAY\.zip && rm -f $back_path/$domain\_$TODAY\.zip
$ZIP -P$mypassword -9r $back_path/$domain\_$TODAY\.zip $site_path && \
uploadToCOS $domain $back_path/$domain\_$TODAY\.zip
}
while [ $1 ]; do
case $1 in
'--db' | 'db' )
backupDB $2 $3 $4 $5 $6
exit
;;
'--file' | 'file' )
backupFile $2 $3 $4
exit
;;
* )
printHelp
exit
;;
esac
done
printHelp
Save the above code as backup.sh then upload it to server, add cronjob
0 2 * * 1 /bin/bash /root/backup.sh db jackiesung.com sqlname sqluser sqlpasswd /data > /dev/null 2>&1
0 3 * * 1 /bin/bash /root/backup.sh file jackiesung.com /home/wwwroot/www.jackiesung.com /data > /dev/null 2>&1
Restart crontab
service crond restart
/etc/init.d/cron restart
Grant permission and run
chmod +x backup.sh
bash backup.sh
If you encounter the prompt bin / sh ^ M: bad interpreter: No such file or directory
Run following code to check file format and you'll see
fileformat=dos
or
fileformat=unix
Set file format with following code
:set ff=unix
or
:set fileformat=unix
* Conclusion
In the blog post, we discuss the process of periodically backing up a WordPress website auto. This process entails backing up the MySQL database as well as the site files.
0 Comments