-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive.py
More file actions
95 lines (76 loc) · 2.6 KB
/
archive.py
File metadata and controls
95 lines (76 loc) · 2.6 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from datetime import datetime
import os
import tarfile
import time
from envparse import env
from pushover import Client
"""
Archives TARGET_DIR and sends notifications via Pushover.
"""
env.read_envfile()
TARGET_DIR = env('TARGET_DIR') # note that here TARGET_DIR is used as the SOURCE for the tarball
ARCHIVE_DIR = env('ARCHIVE_DIR')
DAYS_TO_RETAIN_ARCHIVES = env('DAYS_TO_RETAIN_ARCHIVES')
PUSHOVER_API_TOKEN = env('PUSHOVER_API_TOKEN')
PUSHOVER_USER_KEY = env('PUSHOVER_USER_KEY')
APP_NAME = env('APP_NAME')
def send_notification(title, msg):
client = Client(PUSHOVER_USER_KEY, api_token=PUSHOVER_API_TOKEN)
client.send_message(msg, title=title)
def run():
# make sure the target dir exists, exit if it doesn't
if not os.path.exists(TARGET_DIR):
msg = "TARGET_DIR {} doesn't exist!".format(TARGET_DIR)
print(msg)
send_notification(
'{} - ERROR'.format(APP_NAME),
msg
)
exit(1)
# make sure the archive dir exists, create if it doesn't
if not os.path.exists(ARCHIVE_DIR):
print("ARCHIVE_DIR {} doesn't exist. Creating ...".format(ARCHIVE_DIR))
os.mkdir(ARCHIVE_DIR)
# remove the old archives first since we may be tight on disk space
print('Removing old archives ...')
try:
n = time.time()
d = int(DAYS_TO_RETAIN_ARCHIVES)
for f in os.listdir(ARCHIVE_DIR):
f = os.path.join(ARCHIVE_DIR, f)
if os.stat(f).st_mtime <= n - d * 86400:
if os.path.isfile(f):
os.remove(f)
print('Old archives removed.')
except Exception as e:
# only alert if the archives couldn't be removed so we don't run out of disk space
msg = 'Removing old archives failed! {}'.format(str(e))
print(msg)
send_notification(
'{} - REMOVING ARCHIVES FAILED'.format(APP_NAME),
msg
)
# create the new archive
print('Creating archive ...')
try:
archive_file_path = '{}/{}_archive.tgz'.format(
ARCHIVE_DIR,
datetime.now().strftime('%Y%m%d%H%M%S')
)
with tarfile.open(archive_file_path, 'w:gz') as tar:
tar.add(TARGET_DIR, arcname=os.path.sep)
msg = 'Successful archive!'
print(msg)
send_notification(
'{} - ARCHIVE OK'.format(APP_NAME),
msg
)
except Exception as e:
msg = 'ERROR: {}'.format(str(e))
print(msg)
send_notification(
'{} - ARCHIVE FAILED'.format(APP_NAME),
msg
)
if __name__ == '__main__':
run()