-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_autobk_maintenance.py
executable file
·65 lines (57 loc) · 2.27 KB
/
op_autobk_maintenance.py
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
#!/usr/local/bin/python3
import os as mOS
import sys as mSys
from datetime import timedelta, datetime, date, time, timezone
from mysql.connector import MySQLConnection
from lib_autobk import *
###############################
# SQL Formatting
sSqlGetExpired = """
SELECT bk.kSelf, bk.kDevice, bk.sFile, dev.sName
FROM Backup bk
LEFT JOIN Device dev ON bk.kDevice=dev.kSelf
WHERE bk.tExpires<=%s"""
sSqlDelBackup = "DELETE FROM Backup WHERE kSelf=%s"
sSqlDelVersion = "DELETE FROM BackupVersion WHERE kBackup=%s"
###############################
try:
# Configuration and Logging
(oINI, oLog, sAt) = LoadConfig('op-maintenance')
dnIniDb = { 'autocommit': True }
dnIniDb['user'] = oINI.get('Database', 'Usr', fallback='root')
dnIniDb['password'] = oINI.get('Database', 'Pwd', fallback='')
dnIniDb['database'] = oINI.get('Database', 'DB', fallback='AutoBk')
dnIniDb['host'] = oINI.get('Database', 'Host', fallback='localhost')
sIniDirectory = oINI.get('Backups', 'Directory', fallback='/backups')
oLog.info(sAt, 'state', 'running')
# Connect to database
oCnx = MySQLConnection(**dnIniDb)
oCursor = oCnx.cursor(named_tuple=True, buffered=True)
oLog.info(sAt, 'DB', 'ready')
# Get current date/time for this cycle
tNow = datetime.now(tz=timezone.utc).astimezone()
# Purge expired backups
oCursor.execute(sSqlGetExpired, (tNow,))
oLog.info(sAt, 'expiring', oCursor.rowcount)
for oBk in oCursor.fetchall():
try:
# Calculate filepath and remove file
sBkDir = sSubPath.format(sIniDirectory, str(oBk.kDevice).zfill(10))
sBkPath = sSubPath.format(sBkDir, oBk.sFile)
oLog.info(sAt, 'delete', sBkPath)
if (mOS.path.isfile(sBkPath)): mOS.remove(sBkPath)
# Remove DB entry
oCursor.execute(sSqlDelVersion, (oBk.kSelf,))
oCursor.execute(sSqlDelBackup, (oBk.kSelf,))
oLog.info(sAt, 'purge', 'complete')
except Exception as oErr:
# Error caught so we can continue with next in list (in case of permissions issues, etc...)
oLog.error(sAt, 'purge', oErr)
# Database cleanup
oCursor.close()
oCnx.close()
oLog.info(sAt, 'state', 'complete')
except Exception as oErr:
if (oLog is not None): oLog.error(sAt, 'unknown', oErr)
print(str(oErr)) # used by calling thread to get error message
mSys.exit(1)