|
| 1 | +#!/usr/bin/python |
| 2 | +# use Fabric to manage all the hosts in perf env. |
| 3 | +# usage: fab -f vps_fabfile.py download_backup |
| 4 | +# author: Jay <smile665@gmail.com> |
| 5 | + |
| 6 | +from fabric.context_managers import cd |
| 7 | +#from fabric.context_managers import settings |
| 8 | +from fabric.operations import * |
| 9 | +from fabric.api import * |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +env.hosts = 'smilejay.com' |
| 13 | +env.port = 22 |
| 14 | +env.user = 'root' |
| 15 | +env.password = '1234' |
| 16 | + |
| 17 | + |
| 18 | +@task |
| 19 | +def put_sshkey(): |
| 20 | + # add ssh public key of the master to remote slaves. |
| 21 | + with cd('/tmp'): |
| 22 | + put('id_rsa.pub.master', 'id_rsa.pub.master') |
| 23 | + put('add_sshkey.sh', 'add_sshkey.sh') |
| 24 | + run('bash add_sshkey.sh id_rsa.pub.master') |
| 25 | + |
| 26 | + |
| 27 | +@task |
| 28 | +def download_backup(): |
| 29 | + # backup my WP file and database, download them to the local machine |
| 30 | + dt = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") |
| 31 | + local_dir = '/home/jay/backup' |
| 32 | + with cd('/tmp'): |
| 33 | + nginx = '/usr/share/nginx' |
| 34 | + wp_root = '/usr/share/nginx/html' |
| 35 | + exclude = 'html/wp-content/cache' |
| 36 | + bk_name = 'wp_%s.tar.gz' % dt |
| 37 | + clean = 'rm -f wp*.tar.gz' |
| 38 | + mysql = 'mysqldump -uroot -p1234 -A > %s/mysql-dump.sql' % wp_root |
| 39 | + tar = 'tar -zcf %s -C %s html --exclude=%s' % (bk_name, nginx, exclude) |
| 40 | + run(clean) |
| 41 | + run(mysql) |
| 42 | + run(tar) |
| 43 | + get(bk_name, '%s/%s' % (local_dir, bk_name)) |
0 commit comments