Skip to content

Instantly share code, notes, and snippets.

@rupesh92 rupesh92/rds_backup
Last active Nov 24, 2018

Embed
What would you like to do?
#!/usr/bin/python
from subprocess import PIPE,Popen
import shlex
SOURCE_HOST = 'SOURCE_HOST'
DESTINATION_HOST = 'DESTINATION_HOST'
DATABASES = [ [DBNAME, 'USERNAME', 'DBPASSWORD'],
[DBNAME, 'USERNAME', 'DBPASSWORD'],
[DBNAME, 'USERNAME', 'DBPASSWORD'],
[DBNAME, 'USERNAME', 'DBPASSWORD'],
[DBNAME, 'USERNAME', 'DBPASSWORD'],
]
def dump_table(host_name,database_name,user_name,database_password, file_name):
command = 'PGPASSWORD={0} pg_dump -h {1} -d {2} -U {3} -p 5432 -Fc -f {4}'\
.format(database_password, host_name,database_name,user_name, file_name, file_name)
p = Popen(command,shell=True)
return p.communicate()[0]
def restore_table(host_name,database_name,user_name,database_password, file_name):
print("Restoring {}".format(database_name))
#Remove the '<' from the pg_restore command.
command = 'PGPASSWORD={0} pg_restore -h {1} -d {2} -U {3} {4}'\
.format(database_password, host_name, database_name, user_name, file_name)
p = Popen(command,shell=True)
return p.communicate()[0]
def main():
for database in DATABASES:
response = dump_table(SOURCE_HOST,database[0],database[1],database[2], '/home/ec2-user/backup/db_undertaker/{}.sql'.format(database[0]))
print("response is {}".format(response))
for database in DATABASES:
response = restore_table(DESTINATION_HOST,database[0],database[1],database[2], '/home/ec2-user/backup/db_undertaker/{}.sql'.format(database[0]))
print("response is {}".format(response))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.