Last active
February 7, 2018 15:57
-
-
Save marcelblanarik/463738b7689a099a6b402d73b4427c4c to your computer and use it in GitHub Desktop.
Python - PostgreSQL multiple DB backup script
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 psycopg2 | |
import time | |
import subprocess | |
import os | |
import glob | |
USER = "username" | |
PASS = "password" | |
HOST = "host" | |
DBNAME = "dbname" | |
BACKUP_DIR = "w:\\backups\\" | |
db_dumper = """ "c:\\program files\\PostgreSQL\\9.6\\bin\\pg_dump" -U %s -Z 9 -f %s -F c %s """ | |
users_dumper = """ "c:\\program files\\PostgreSQL\\9.6\\bin\\pg_dumpall" -U %s --globals-only -f %s """ | |
# Log every action into the LOG file | |
def log(string): | |
path = BACKUP_DIR + '\\backup.log' | |
with open(path,'a') as log_file: | |
log_file.write(time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) + ": " + str(string) + '\n') | |
log_file.close() | |
# Define number of days the backups will be stored in BACKUP_DIR (in this case set to 7 days) | |
days = time.time() - ( 60 * 60 * 24 * 7 ) | |
os.putenv('PGPASSWORD', PASS) | |
conn_string = "host=" + HOST + " dbname=" + DBNAME + " user=" + USER + " password=" + PASS | |
conn = psycopg2.connect(conn_string) | |
curs = conn.cursor() | |
curs.execute("select datname from pg_database") | |
dbs = curs.fetchall() | |
database_list = [] | |
# Define databases to backup | |
for db in dbs: | |
if str(db[0]) == "DB1" or str(db[0]) == "DB2": | |
database_list.append(str(db[0])) | |
# Delete old backup files first. | |
for database_name in database_list : | |
glob_list = glob.glob(BACKUP_DIR + database_name + '*' + '.backup') | |
for file in glob_list: | |
file_info = os.stat(file) | |
if file_info.st_ctime < days: | |
log("Deleting: %s" % file) | |
os.unlink(file) | |
else: | |
log("Keeping: %s" % file) | |
log("Backup files older than %s were deleted." % time.strftime('%c', time.gmtime(x_days_ago))) | |
# Now perform the backup. | |
for database_name in database_list : | |
log("dump started for %s" % database_name) | |
thetime = time.strftime("%Y-%m-%d-%H-%M") | |
db_file_name = database_name + "_" + thetime + ".backup" | |
users_file_name = "GLOBALS" + "_" + thetime + ".backup" | |
#Run the pg_dump command to get the DB backups | |
db_dump = db_dumper % (USER, BACKUP_DIR + db_file_name, database_name) | |
log(db_dump) | |
subprocess.call(db_dump,shell = True) | |
log("dump finished for %s" % database_name) | |
#Run the pg_dump command to the GLOBALs (users, groups, tablespaces) backup | |
log("dump started for GLOBALs") | |
users_dump = users_dumper % (USER, BACKUP_DIR + users_file_name) | |
log(users_dump) | |
subprocess.call(users_dump,shell = True) | |
log("dump finished for GLOBALs") | |
log("Backup job complete.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment