最近用WordPress建了个个人博客,本着数据无价的原则编写了本脚本对数据库和网址目录进行备份并上传到百度网盘里
话不多说,下面就该Python脚本的程序依赖、代码说明及使用方法进行叙述
一、程序依赖
1.安装Python Requests 库
2.安装bypy(这是一个pytnon的百度网盘客户端)pip install requests
关于bypy的详细说明可以看(https://github.com/houtianze/bypy)pip install bypy
3.初始化bypy并授权
会显示如下信息,访问红色框的网址就可以进行授权bypy info
登陆自己的百度账号得到授权码
将该授权码复制到终端里,并回车就授权成功了
至此相关程序依赖搭建完毕
二、代码说明
该脚本的代码主要分为三块:数据库备份函数、网站目录备份函数和打包上传函数。下面进行说明
1.初始化函数
最前面的两行global是对一些全局变量进行声明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"
用户需要根据自己需要修改备份文件存放路径、网站根目录路径、云端备份目录、数据库地址、数据库用户、数据库密码和备份数据库的表名等内容。如果有多个表需要备份,则可以在BACKUP_PATH路径下新建一个dbnames.txt文件并将表名按行存放
2.数据库备份函数
最终的数据库备份文件*.sql会被放在一个以备份时间命名的文件夹内(如:/backup/20170425221512)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"
3.网站目录备份函数
我的网站根目录是/var/www/html/,我们将对该目录下的所有文件进行备份
最终会在备份文件里打包一个Web.zip的文件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)
4.打包上传函数
使用bypy的upload函数,他的具体使用方法可以查看我的博客
该函数主要有两个功能,一是把备份目录进行打包最终文件如下图,二是把打包文件上传到百度网盘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
输入
按Esc输入:wq再回车就保存退出了30 3 * * * python /backup/WPbackup.py
输入,则可以查看当前定时任务
crontab -l
四、效果展示
到目前为止,该脚本从程序依赖到使用方法已经弄完了,下面进行一下效果展示
1.本地备份目录
2.云端效果
3.代码运行效果
最终终端效果
动图(好吧,图片太大了传不上来)
欢迎访问我的个人博客