一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Python 自动备份和下载WordPress + MySQL 的 fabric 脚本

时间:2015-03-08 编辑:简简单单 来源:一聚教程网

我的博客搭建在一个KVM VPS上,今天也写了一个fabfile来dump数据库、打包WordPress目录,并下载到本地。fabfile代码如下:

 代码如下 复制代码
#!/usr/bin/python
# use Fabric to manage all the hosts in perf env.
# usage: fab -f vps_fabfile.py download_backup
# author: Jay
 
from fabric.context_managers import cd
#from fabric.context_managers import settings
from fabric.operations import *
from fabric.api import *
from datetime import datetime
 
env.hosts = 'smilejay.com'
env.port = 22
env.user = 'root'
env.password = '1234'
 
 
@task
def download_backup():
    # backup my WP file and database, download them to the local machine
    dt = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    local_dir = '/home/jay/backup'
    with cd('/tmp'):
        nginx = '/usr/share/nginx'
        wp_root = '/usr/share/nginx/html'
        exclude = 'html/wp-content/cache'
        bk_name = 'wp_%s.tar.gz' % dt
        clean = 'rm -f wp*.tar.gz'
        mysql = 'mysqldump -uroot -p1234 -A > %s/mysql-dump.sql' % wp_root
        tar = 'tar -zcf %s -C %s html --exclude=%s' % (bk_name, nginx, exclude)
        run(clean)
        run(mysql)
        run(tar)
        get(bk_name, '%s/%s' % (local_dir, bk_name))



Github地址:https://github.com/smilejay/python/blob/master/py2014/vps_fabfile.py
当然,我一般也会使用BackWPup插件来备份WordPress;刚好发现,前段时间使用Nginx替代Apache后,BackWPup运行时仍然要写“/var/www/html/wp-content/backwpup-logs/”目录,所以有个权限问题,最近两个月都是运行失败了。后来对这个目录开放了写权限就没问题了。

热门栏目