Created
October 3, 2013 14:23
-
-
Save alekseyev/6810696 to your computer and use it in GitHub Desktop.
Fabric script to transfer DB dump from remote server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import time | |
from fabric.api import local, run, cd, env, task | |
from fabric.operations import get | |
from fabric.context_managers import cd, prefix | |
from settings import DATABASES, MEDIA_ROOT | |
# May be there is a better way to define host | |
env.use_ssh_config = True | |
env.path = '/path/to/dir/with/manage.py' | |
env.hosts = '42.42.42.42' | |
env.user = 'fortytwocc' | |
ENV_COMMAND = 'source ../../.env/bin/activate' | |
# TODO: Think how to get it from production settings | |
DB_NAME = 'bpc' | |
DB_USER = 'postgres' | |
DUMP_FILENAME = 'dump.tar' | |
@task | |
def manage(command): | |
with cd(env.path), prefix(ENV_COMMAND): | |
run('python manage.py {0}'.format(command)) | |
@task | |
def lmanage(command): | |
""" | |
Local manage | |
""" | |
local('python manage.py {0}'.format(command)) | |
def create_dump(): | |
""" | |
Method to create DB dump on production | |
""" | |
dump_cmd = 'pg_dump -U {0} -i -b {1} | gzip > {2}.gz'.format( | |
DB_USER, DB_NAME, DUMP_FILENAME | |
) | |
with cd(env.path): | |
run(dump_cmd) | |
@task | |
def load_dump(): | |
""" | |
Method to load generated in 'create_dump' file to | |
local database | |
""" | |
database = DATABASES['default'] | |
# Cleaning database | |
recreate_params = [ | |
'-U {}'.format(database['USER']), | |
] | |
if database['HOST'] != '': | |
recreate_params.append('-h {}'.format(database['HOST'])) | |
local('psql {0} -c "DROP DATABASE {1};"'.format( | |
" ".join(recreate_params), database['NAME']) | |
) | |
local('psql {0} -c "CREATE DATABASE {1};"'.format( | |
" ".join(recreate_params), database['NAME']) | |
) | |
params = [ | |
'-d {}'.format(database['NAME']), # DB name | |
'-U {}'.format(database['USER']), # DB user | |
] | |
if database['HOST'] != '': | |
params.append('-h {}'.format(database['HOST'])) | |
#params.append(DUMP_FILENAME) | |
load_command = 'gunzip {0}.gz --stdout | psql {1}'.format(DUMP_FILENAME, " ".join(params)) | |
local(load_command) | |
local('cd .. && make migrate') | |
lmanage('loaddata admin_user.json') | |
last_upate_file = os.path.join( | |
os.path.dirname(os.path.abspath(__file__)), | |
'last_db_update.txt' | |
) | |
if os.path.exists(last_upate_file): | |
os.remove(last_upate_file) | |
with open(last_upate_file, 'w') as f: | |
f.write(time.strftime('%X %x')) | |
@task | |
def update_database(): | |
last_upate_file = os.path.join( | |
os.path.dirname(os.path.abspath(__file__)), | |
'last_db_update.txt' | |
) | |
if os.path.exists(last_upate_file): | |
os.remove(last_upate_file) | |
with open(last_upate_file, 'w') as f: | |
f.write('In Progress') | |
create_dump() | |
local_dump = os.path.join( | |
os.path.dirname(os.path.abspath(__file__)), | |
DUMP_FILENAME | |
) | |
dump = os.path.join(env.path, DUMP_FILENAME) | |
get(dump + '.gz', local_dump + '.gz') | |
with cd(env.path): | |
run('rm {}.gz'.format(dump)) | |
load_dump() | |
local('rm {}.gz'.format(local_dump)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment