-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresqlBackUp.py
More file actions
33 lines (27 loc) · 846 Bytes
/
Copy pathPostgresqlBackUp.py
File metadata and controls
33 lines (27 loc) · 846 Bytes
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
# import required modules
import os
import time
import datetime
import pipes
import gzip
#database info and backup file
BACKUP_DIR_NAME ='/postgresql_backups'
DBHOST = '127.0.0.1'
DBUSER = 'testuser'
DBUSERPASSWORD = 'F&9+7xZ`J%AXRgf'
DBNAME = 'test'
# Getting current DateTime
DATETIME = time.strftime('%Y%m%d-%H%M')
TODAYBACKUPPATH = BACKUP_DIR_NAME
# check if the backup folder exist or not
try:
os.stat(TODAYBACKUPPATH)
except:
os.mkdir(TODAYBACKUPPATH)
#starting backups
db = DBNAME
dumpcmd = "pg_dump -h " + DBHOST + " -U " + DBUSER + " -p " + DBUSERPASSWORD + " -d " + db + " -f " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.system(dumpcmd)
#gzipcmd = "gzip" + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
#os.system(gzipcmd)
print("Backup completed and have been created in" + TODAYBACKUPPATH + "directory")