forked from agmangas/mongo-backup-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
63 lines (47 loc) · 1.65 KB
/
run.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Script that runs a MongoDB backup job periodically according
to the interval defined by the INTERVAL_NAME environment variable.
"""
import time
import datetime
import subprocess
import functools
import traceback
import os
import schedule
BACKUP_SCRIPT_PATH = "/app/backup.sh"
ENV_BACKUP_INTERVAL = "BACKUP_INTERVAL"
ENV_BACKUP_TIME = "BACKUP_TIME"
TIME_FORMAT = "%H:%M"
def catch_exceptions(job_func):
@functools.wraps(job_func)
def wrapper(*args, **kwargs):
try:
job_func(*args, **kwargs)
except:
print(traceback.format_exc())
return wrapper
@catch_exceptions
def backup_job():
print("Executing backup at {}".format(datetime.datetime.now().isoformat()))
backup_result = subprocess.check_output([BACKUP_SCRIPT_PATH])
print(backup_result)
print("Backup finished at {}".format(datetime.datetime.now().isoformat()))
if __name__ == "__main__":
print("Starting periodic MongoDB backup at {}".format(datetime.datetime.now().isoformat()))
try:
interval_days = int(os.environ.get(ENV_BACKUP_INTERVAL))
except:
raise ValueError("Undefined or invalid var: {}".format(ENV_BACKUP_INTERVAL))
try:
backup_time = os.environ.get(ENV_BACKUP_TIME)
datetime.datetime.strptime(backup_time, TIME_FORMAT)
except:
raise ValueError("Undefined or invalid var: {}".format(ENV_BACKUP_TIME))
print("Executing backups every {} day/s at {}".format(interval_days, backup_time))
schedule.every(interval_days).days.at(backup_time).do(backup_job)
while True:
schedule.run_pending()
time.sleep(1)