Skip to content

Instantly share code, notes, and snippets.

@llazzaro
Last active August 29, 2015 14:03
Show Gist options
  • Save llazzaro/051d5ca4ee78286e78cf to your computer and use it in GitHub Desktop.
Save llazzaro/051d5ca4ee78286e78cf to your computer and use it in GitHub Desktop.
Fabric script for Postgres backup
import os
import time
from fabric.contrib.files import exists
from fabric.api import (
env,
require,
run,
get,
sudo
)
def vps():
env.hosts = ['vps_address']
env.user = 'user'
def backup():
require('hosts', provided_by=[vps])
for host in env.hosts:
date = time.strftime('%Y%m%d%H%M%S')
fname = '/tmp/{host}-backup-{date}.xz'.format(**{
'host': host,
'date': date,
})
if exists(fname):
run('rm "{0}"'.format(fname))
sudo('cd; pg_dumpall | xz > {0}'.format(fname), user='postgres')
get(fname, os.path.basename(fname))
sudo('rm "{0}"'.format(fname), user='postgres')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment