为了防止数据丢失,每个人都应将您的数据(例如WordPress网站的内容,MySQL数据库)备份到文件中。当数据损坏或丢失时,您可以从备份文件中将其还原。
项目网址:https://github.com/tencentyun/cos-python-sdk/
步骤1-系统环境准备-安装腾讯云Python SDK插件
安装点子
apt-get install python-pip
更新点
pip install --upgrade pip
安装腾讯云COS SDK插件
pip install qcloud_cos_v4
将以下代码另存为cos.upload.py,然后上传到服务器
# -*- 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') )
如何使用
python /root/cos.upload.py appid secret_id secret_key Bucket_name Website_domain /data/jackiesung_1.zip
第2步-定期脚本
7天自动备份
#!/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
将上面的代码另存为backup.sh,然后将其上传到服务器,添加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
重新启动crontab
service crond restart /etc/init.d/cron restart
授予权限并运行
chmod +x backup.sh bash backup.sh
如果遇到提示bin / sh ^ M:解释错误:没有这样的文件或目录
vi backup.sh
运行以下代码检查文件格式,您将看到
fileformat=dos or fileformat=unix
使用以下代码设置文件格式
:set ff=unix or :set fileformat=unix
* 结论
在博客文章中,我们讨论了定期备份WordPress网站自动程序的过程。此过程需要备份MySQL数据库以及站点文件。
原创文章,作者:cheshirex,如若转载,请注明出处:https://www.homedt.net/20441.html