forked from heyman/postgresql-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore.py
More file actions
73 lines (61 loc) · 1.78 KB
/
Copy pathrestore.py
File metadata and controls
73 lines (61 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
import os
import subprocess
import sys
from datetime import datetime
# Read secret from file
def read_secret(secret_name):
try:
f = open('/run/secrets/' + secret_name, 'r')
except EnvironmentError:
return ''
else:
with f:
return f.readline().strip()
BACKUP_DIR = os.environ["BACKUP_DIR"]
DB_NAME = os.environ["DB_NAME"]
DB_PASS = os.environ.get("DB_PASS", read_secret('db_password'))
DB_USER = os.environ["DB_USER"]
DB_HOST = os.environ["DB_HOST"]
file_name = sys.argv[1]
backup_file = os.path.join(BACKUP_DIR, file_name)
def cmd(command):
try:
subprocess.check_output([command], shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
sys.stderr.write("\n".join([
"Command execution failed. Output:",
"-"*80,
e.output,
"-"*80,
""
]))
raise
def backup_exists():
return os.path.exists(backup_file)
def restore_backup():
if not backup_exists():
sys.stderr.write("Backup file doesn't exists!\n")
sys.exit(1)
# restore postgres-backup
cmd("env PGPASSWORD=%s pg_restore -Fc -h %s -U %s -d %s %s" % (
DB_PASS,
DB_HOST,
DB_USER,
DB_NAME,
backup_file,
))
def log(msg):
print "[%s]: %s" % (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), msg)
def main():
start_time = datetime.now()
if backup_exists():
log("Backup file already exists in filesystem %s" % backup_file)
log("Restoring database")
restore_backup()
log("Restore complete, took %.2f seconds" % (datetime.now() - start_time).total_seconds())
else:
log("Backup does not exist")
exit 1
if __name__ == "__main__":
main()