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