-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-postgresql-wal-backup.py
More file actions
52 lines (45 loc) · 1.69 KB
/
verify-postgresql-wal-backup.py
File metadata and controls
52 lines (45 loc) · 1.69 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
#!/usr/bin/python
#verify postgresql wal backup.
#1) notify if old files still laying around
#2) notify if files not showing up or missing
import os
import time
import datetime
import smtplib
import socket
path = '/postgresql_wal_backup'
hoursback = 48;
hoursrecent = 12;
timebackdt = datetime.datetime.now() - datetime.timedelta(hours=hoursback)
timerecentdt = datetime.datetime.now() - datetime.timedelta(hours=hoursrecent)
hostname = socket.gethostname().split('.')[0]
files = sorted(os.listdir(path))
oldfiles = []
recentfiles = []
for file in files:
ctimedt = datetime.datetime.fromtimestamp(os.stat(path + '/' + file).st_ctime)
filestats = str(ctimedt) + ' | ' + file
if ctimedt < timebackdt:
oldfiles.append(str(timebackdt) + ' | ' + filestats)
elif ctimedt > timerecentdt:
recentfiles.append(str(timerecentdt) + ' | ' + filestats)
#mail headers
msg = "From: backup@pelagodesign.com\r\n"
msg = msg + "To: backup@pelagodesign.com\r\n"
msg = msg + "Subject: PostgreSQL WAL Backup Issue on " + hostname + "\r\n"
msg = msg + "\r\n"
#if we found any old files, email a message
sendmail = False
if len(oldfiles) > 0:
msg = msg + "There are old WAL files older than " + str(hoursback) + " hours ago in the backup that should not be there.\n"
msg = msg + "\n".join(oldfiles)
sendmail = True
elif len(recentfiles) == 0:
msg = msg + "Recent WAL files from the last " + str(hoursrecent) + " hours are missing.\n"
sendmail = True
if sendmail == True:
if hostname == 'd1fr':
mailserver = smtplib.SMTP('192.168.132.78')
else:
mailserver = smtplib.SMTP('10.186.33.143')
mailserver.sendmail("backup@pelagodesign.com", "backup@pelagodesign.com", msg)