Skip to content

Instantly share code, notes, and snippets.

@agaerig
Created March 26, 2013 20:21
Show Gist options
  • Select an option

  • Save agaerig/5248855 to your computer and use it in GitHub Desktop.

Select an option

Save agaerig/5248855 to your computer and use it in GitHub Desktop.
@task
@runs_once
def export(*tables):
download_path = prompt('Where should I save the DB dump? [default: .]')
# use defaul database
db = DATABASES['default']
run('rm -f /tmp/production-*.sql*')
dump_filename = 'production-dump.%s.sql' % str(time.time()).split('.')[0]
remote_dump_path = os.path.join('/tmp', dump_filename)
local_dump_path = os.path.realpath(os.path.join(download_path, dump_filename)) + '.bz2'
# dump the database
print cyan('Dumping database on server')
run('mysqldump -h %s -u %s --quick --password %s %s %s > %s ' % (
db['HOST'], db['USER'], db['PASSWORD'], db['NAME'],
' '.join(tables), remote_dump_path))
print cyan('bzipping dump')
# let's be reasonable here
run('bzip2 ' + remote_dump_path)
print cyan('Downloading file to ' + local_dump_path)
# download the reasonable dump
local('scp ubuntu@%s:%s.bz2 %s' % (env.host, remote_dump_path, local_dump_path))
print cyan('Cleaning up the server')
# cleanup!
run('rm '+ remote_dump_path + '.bz2')
print blue('Download can be found here: '+ local_dump_path)
print blue('Done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment