最近用WordPress建了个个人博客,本着数据无价的原则编写了本脚本对数据库和网址目录进行备份并上传到百度网盘里

话不多说,下面就该Python脚本的程序依赖、代码说明及使用方法进行叙述

一、程序依赖

1.安装Python Requests 库

pip install requests
pip install bypy
bypy info
def init():
    global BACKUP_PATH, WP_PATH, DB_HOST, DB_USER, DB_NAME
    global DB_USER_PASSWORD, DATETIME, TODAYBACKUPPATH
    # 需要修改部分
    BACKUP_PATH = '/backup/'  # 备份文件存放路径
    WP_PATH = '/var/www/html/'  # 网站根目录路径
    BaiDu_Path = '/WPbackup/'  # 云端备份目录
    DB_HOST = 'localhost'  # 数据库地址,默认本地
    DB_USER = 'root'  # 数据库用户
    DB_USER_PASSWORD = 'zxc123...'  # 数据库密码
    DB_NAME = 'wordpress'  # 备份数据库的表名
    # 需要备份多个表,表名以列方式放在dbnames.txt内
    # DB_NAME = BACKUP_PATH+'/dbnames.txt'
    ################################
    # 得到当前时间像 "20170425221512"
    DATETIME = time.strftime('%Y%m%d%H%M%S')
    TODAYBACKUPPATH = BACKUP_PATH + DATETIME
    # 创建备份目录
    print "creating backup folder"
    if not os.path.exists(TODAYBACKUPPATH):
        os.makedirs(TODAYBACKUPPATH)
    print "init finished"
def DBbackup():
    global BACKUP_PATH, WP_PATH, DB_HOST, DB_USER, DB_NAME
    global DB_USER_PASSWORD, DATETIME, TODAYBACKUPPATH

    # 判断备份方式
    print "checking for databases names file."
    if os.path.exists(DB_NAME):
        file1 = open(DB_NAME)
        multi = 1
        print "Starting backup of all dbs listed in file " + DB_NAME
    else:
        print "Databases file not found..."
        print "Starting backup of database " + DB_NAME
        multi = 0

    # 开始备份数据库
    if multi:    # 以 dbnames.txt 内容进行备份
        in_file = open(DB_NAME, "r")
        flength = len(in_file.readlines())
        in_file.close()
        p = 1
        dbfile = open(DB_NAME, "r")

        while p <= flength:
            db = dbfile.readline()  # reading database name from file
            db = db[:-1]  # deletes extra line
            dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
            os.system(dumpcmd)
            p = p + 1
        dbfile.close()
    else:
        db = DB_NAME
        dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
        os.system(dumpcmd)
    print "Mysql Backup script completed"
def Webbackup():
    global BACKUP_PATH, WP_PATH, DB_HOST, DB_USER, DB_NAME
    global DB_USER_PASSWORD, DATETIME, TODAYBACKUPPATH

    # 备份网站目录
    print "Start Backup Web"
    # 开始备份WordPress目录
    dumpcmd = "cd " + TODAYBACKUPPATH
    os.system(dumpcmd)
    dumpcmd = "zip -r " + TODAYBACKUPPATH + "/Web.zip " + WP_PATH
    os.system(dumpcmd)


def upload2baiduyun():
    global BACKUP_PATH, WP_PATH, DB_HOST, DB_USER
    global DB_USER_PASSWORD, DATETIME, TODAYBACKUPPATH,  BaiDu_Path
    # 对备份文件进行打包
    print "start zip"
    dumpcmd = "zip -r " + BACKUP_PATH + DATETIME + ".zip " + TODAYBACKUPPATH
    os.system(dumpcmd)
    # 上传到百度云
    print "start backup"
    dumpcmd = "bypy -v upload " + BACKUP_PATH + DATETIME + ".zip" + " /WPbackup/"
    os.system(dumpcmd)
    print "backup over"

#!/usr/bin/env python
# coding=utf-8
import os, time, datetime
import bypy
global BACKUP_PATH, WP_PATH, DB_HOST, DB_USER, DB_NAME
global DB_USER_PASSWORD, DATETIME, TODAYBACKUPPATH,  BaiDu_Path
def init():
    global BACKUP_PATH, WP_PATH, DB_HOST, DB_USER, DB_NAME
    global DB_USER_PASSWORD, DATETIME, TODAYBACKUPPATH
    # 需要修改部分
    BACKUP_PATH = '/backup/'  # 备份文件存放路径
    WP_PATH = '/var/www/html/'  # 网站根目录路径
    BaiDu_Path = '/WPbackup/'  # 云端备份目录
    DB_HOST = 'localhost'  # 数据库地址,默认本地
    DB_USER = 'root'  # 数据库用户
    DB_USER_PASSWORD = 'zxc123...'  # 数据库密码
    DB_NAME = 'wordpress'  # 备份数据库的表名
    # 需要备份多个表,表名以列方式放在dbnames.txt内
    # DB_NAME = BACKUP_PATH+'/dbnames.txt'
    ################################
    # 得到当前时间像 "20170425221512"
    DATETIME = time.strftime('%Y%m%d%H%M%S')
    TODAYBACKUPPATH = BACKUP_PATH + DATETIME
    # 创建备份目录
    print "creating backup folder"
    if not os.path.exists(TODAYBACKUPPATH):
        os.makedirs(TODAYBACKUPPATH)
    print "init finished"
def DBbackup():
    global BACKUP_PATH, WP_PATH, DB_HOST, DB_USER, DB_NAME
    global DB_USER_PASSWORD, DATETIME, TODAYBACKUPPATH

    # 判断备份方式
    print "checking for databases names file."
    if os.path.exists(DB_NAME):
        file1 = open(DB_NAME)
        multi = 1
        print "Starting backup of all dbs listed in file " + DB_NAME
    else:
        print "Databases file not found..."
        print "Starting backup of database " + DB_NAME
        multi = 0

    # 开始备份数据库
    if multi:    # 以 dbnames.txt 内容进行备份
        in_file = open(DB_NAME, "r")
        flength = len(in_file.readlines())
        in_file.close()
        p = 1
        dbfile = open(DB_NAME, "r")

        while p <= flength:
            db = dbfile.readline()  # reading database name from file
            db = db[:-1]  # deletes extra line
            dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
            os.system(dumpcmd)
            p = p + 1
        dbfile.close()
    else:
        db = DB_NAME
        dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
        os.system(dumpcmd)
    print "Mysql Backup script completed"

def Webbackup():
    global BACKUP_PATH, WP_PATH, DB_HOST, DB_USER, DB_NAME
    global DB_USER_PASSWORD, DATETIME, TODAYBACKUPPATH

    # 备份网站目录
    print "Start Backup Web"
    # 开始备份WordPress目录
    dumpcmd = "cd " + TODAYBACKUPPATH
    os.system(dumpcmd)
    dumpcmd = "zip -r " + TODAYBACKUPPATH + "/Web.zip " + WP_PATH
    os.system(dumpcmd)
def upload2baiduyun():
    global BACKUP_PATH, WP_PATH, DB_HOST, DB_USER
    global DB_USER_PASSWORD, DATETIME, TODAYBACKUPPATH,  BaiDu_Path
    # 对备份文件进行打包
    print "start zip"
    dumpcmd = "zip -r " + BACKUP_PATH + DATETIME + ".zip " + TODAYBACKUPPATH
    os.system(dumpcmd)
    # 上传到百度云
    print "start backup"
    dumpcmd = "bypy -v upload " + BACKUP_PATH + DATETIME + ".zip" + " /WPbackup/"
    os.system(dumpcmd)
    print "backup over"
if __name__ == '__main__':
    init()
    DBbackup()
    Webbackup()
    upload2baiduyun()
python /backup/WPbackup.py
crontab  -e

30 3 * * * python /backup/WPbackup.py
crontab -l