Python 自動備份和下載WordPress + MySQL 的 fabric 指令碼

來源:互聯網
上載者:User

我的部落格搭建在一個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 <smile665@gmail.com>
 
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/”目錄,所以有個許可權問題,最近兩個月都是運行失敗了。後來對這個目錄開放了寫入權限就沒問題了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.