前文后补
SDK 脚本
1.环境
OSS 的 Python SDK 需要用到 oss2 插件,所以我们先安装一下。
如果服务器上已经安装了 pip 工具,可直接执行如下命令安装 oss2 插件
pip install oss2
若没有,则复制以下命令行到服务器上执行安装:
cd /tmp
wget -O master.zip https://codeload.github.com/aliyun/aliyun-oss-python-sdk/zip/master --no-check-certificate
tar zxf master.zip
cd aliyun-oss-python-sdk-master && python setup.py install && echo "Oss2 install OK" || \
echo "Oss2 install failed"
如果没有安装tar,用zip代替
zip -o master.zip
2.备份脚本
# -*- coding: utf-8 -*-
from __future__ import print_function
import os, sys
import oss2
#
# 百分比显示回调函数
#
def percentage(consumed_bytes, total_bytes):
if total_bytes:
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
print('\r{0}% '.format(rate), end=filePath)
sys.stdout.flush()
# 脚本需要传入5个参数
if ( len(sys.argv) > 5 ):
AccessKeyId = sys.argv[1]
AccessKeySecret = sys.argv[2]
Endpoint = sys.argv[3]
Bucket = sys.argv[4]
filePath = sys.argv[5]
fileName = filePath.split("/")[-1]
else:
print("Example: %s AccessKeyId AccessKeySecret Endpoint Bucket /data/backup.zip" % sys.argv[0])
exit()
# OSS认证并开始上传
auth = oss2.Auth(AccessKeyId , AccessKeySecret)
bucket = oss2.Bucket(auth, Endpoint, Bucket)
oss2.resumable_upload(bucket, fileName, filePath, progress_callback=percentage)
print('\rUpload %s to OSS Success!' % filePath)
使用方法:将上述代码保存为 oss.upload.py,并上传到服务器
有了上传脚本,结合七天循环备份脚本,实现循环备份到 OSS。
3.脚本
#!/bin/sh
###################################################################
# Web Backup version 1.0.3 Author: Jager <ge@zhangge.net> #
# For more information please visit https://zhangge.net/5111.html #
#-----------------------------------------------------------------#
# Copyright ©2016 zhangge.net. All rights reserved. #
###################################################################
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)
# 新增的OSS上传文件函数,请按照实际情况修改参数!
uploadToOSS()
{
$PYTHON $baseDir/oss.upload.py 认证KEY 认证密钥 oss-cn-qingdao-internal.aliyuncs.com Bucket名称 $1
}
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]
For example:./backup.sh db zhangge.net zhangge_db zhangge 123456 /home/wwwbackup/zhangge.net
2. Use For Backup webfile:
The $1 must be [\file]:
$2: [domain]
$3: [site_path]
$4: [back_path]
For example:./backup.sh file zhangge.net /home/wwwroot/zhangge.net /home/wwwbackup/zhangge.net
=====================================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
$MYSQLDUMP -u$mysqluser -p$mysqlpd $dbname --skip-lock-tables --default-character-set=binary >$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 -Pmypassword -m $back_path/$domain\_db_$TODAY\.zip $domain\_db_$TODAY\.sql && \
uploadToOSS $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 -Pmypassword -9r $back_path/$domain\_$TODAY\.zip $site_path && \
uploadToOSS $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
使用方法:
将上述代码作如下修改:
I、根据实际情况修改上述代码中的 OSS 上传函数代码,比如密钥对和 Bucket 名称(参考前文)
II、替换代码中的 mypassword 为自己设置的压缩包密码,不修改的话压缩文件解压密码为 mypassword
然后,将代码保存为 backup.sh,上传到服务器(建议存放到和前文 python 脚本的相同目录),比如 /data/backup.sh
4.添加定时任务
#编辑crontab
[root@AlyServer ~]# crontab -e
#然后添加如下内容:
#备份数据库(参数依次为:db、域名、数据库名称、数据库用户名、对应密码、备份路径)
10 3 * * * bash /data/backup.sh db zhangge.net zhangge root 123456 /home/wwwbackup/zhangge.net >/dev/null 2>&1
#备份网站文件(参数依次为:file、域名、网站根目录、备份路径)
15 3 * * * bash /data/backup.sh file zhangge.net /home/wwwroot/zhangge.net /home/wwwbackup/zhangge.net >/dev/null 2>&1
#按下键盘esc,输入 :wq 保存crontab即可