diff --git a/omv_backup.py b/omv_backup.py index 20dad72..c5d2b99 100644 --- a/omv_backup.py +++ b/omv_backup.py @@ -12,6 +12,7 @@ import os import re import platform import requests +import fnmatch from wakeonlan import send_magic_packet pid = os.getpid() @@ -63,6 +64,7 @@ for o, a in opts: elif o in ("-r", "--restore"): _RESTORE = True _APP = a + print("RESTORE") elif o in ("-b", "--backup"): _BACKUP = True elif o in ("-d", "--dry"): @@ -163,13 +165,14 @@ while not is_port_open(BACKUP_HOST, port): print(f"Port {port} on {BACKUP_HOST} is open.") print("Starting") - +print(_RESTORE) if _RESTORE: + print("Starting Restore") if _APP == "all": _APP = ["nginx","ha","gitlab","mailu","bitwarden","esphome","grafana","ingluxdb","kestra","matter-server","mosquitto","octoprint","octoprint2","pihole","unify_block","webhub"] else: _APP = _APP.split(",") - + for app in _APP: topic = "sectorq/omv/restore/{}".format(app) @@ -179,12 +182,17 @@ if _RESTORE: client.publish(topic, json.dumps(msg)) client.disconnect() now = datetime.datetime.now() + DATETIME = now.strftime("%Y-%m-%d_%H-%M-%S") BACKUP_HOST = "root@omv.home.lan" BACKUP_DEVICE = "/srv/dev-disk-by-uuid-2f843500-95b6-43b0-bea1-9b67032989b8" BACKUP_DIR = f"/backup/{host}" - NEW_BACKUP_DIR = f"/backup/m-server/docker_data/latest/{app}" - DATETIME = now.strftime("%Y-%m-%d_%H-%M-%S") - SOURCE_DIR = f"/share/docker_data/" + if app == "fail2ban": + print("?>?????") + NEW_BACKUP_DIR = f"/backup/m-server/fail2ban/latest/" + SOURCE_DIR = f"/etc/fail2ban" + else: + NEW_BACKUP_DIR = f"/backup/m-server/docker_data/latest/{app}" + SOURCE_DIR = f"/share/docker_data/" if _FIRST: BACKUP_PATH="{}/initial".format(BACKUP_DIR) else: @@ -245,6 +253,19 @@ if _RESTORE: print("Start docker") cmnd = "docker start heimdall" status, output = subprocess.getstatusoutput(cmnd) + if app == "fail2ban": + print("Stopping docker") + cmnd = f"rsync -avz --delete rsync://{BACKUP_HOST}{NEW_BACKUP_DIR} {SOURCE_DIR}" + ans = "y" + print(cmnd) + print("Sync files") + if _TEST: + ans = input("continue?") or "n" + if ans == "y" and _EXECUTE: + status, output = subprocess.getstatusoutput(cmnd) + print("Start docker") + cmnd = "docker start heimdall" + status, output = subprocess.getstatusoutput(cmnd) elif app == "nginx": print("Stopping docker") cmnd = "docker stop nginx-app-1" @@ -314,148 +335,164 @@ if _RESTORE: if _BACKUP: - print("Backup") - for b in backups[host]: - topic = "sectorq/omv/backups/{}".format(b.lower()) - if not backups[host][b]["active"]: - print("Backup {} is not active!".format(b)) - client.connect(broker,1883,60) - msg = {"status":"inactive","bak_name":b,"start_time":"inactive","end_time":"inactive","progress":0} + + while True: + directory = '/backups/' + count = len(fnmatch.filter(os.listdir(directory), '*.*')) + print('File Count:', count) + time.sleep(10) + continue + + + + # iterate over files in + # that directory + for filename in os.scandir(directory): + if filename.is_file(): + + print(filename.path) + print("Backup") + for b in backups[host]: + topic = "sectorq/omv/backups/{}".format(b.lower()) + if not backups[host][b]["active"]: + print("Backup {} is not active!".format(b)) + client.connect(broker,1883,60) + msg = {"status":"inactive","bak_name":b,"start_time":"inactive","end_time":"inactive","progress":0} - client.publish(topic, json.dumps(msg)) + client.publish(topic, json.dumps(msg)) + client.disconnect() + continue + + SOURCE_DIR = backups[host][b]["source"] + now = datetime.datetime.now() + BACKUP_HOST = "root@omv.home.lan" + BACKUP_DEVICE = "/srv/dev-disk-by-uuid-2f843500-95b6-43b0-bea1-9b67032989b8" + BACKUP_DIR = f"/backup/{host}/{b}" + NEW_BACKUP_DIR = f"{BACKUP_DEVICE}/backup/{host}/{b}" + DATETIME = now.strftime("%Y-%m-%d_%H-%M-%S") + if _FIRST: + BACKUP_PATH="{}/initial".format(BACKUP_DIR) + else: + BACKUP_PATH="{}/{}".format(BACKUP_DIR, DATETIME) + LATEST_LINK="{}/latest".format(BACKUP_DIR) + FULL_BACKUP_LATEST = f"{NEW_BACKUP_DIR}/latest" + LATEST_LINK = f"/{host}/{b}/latest" + + + msg = {"status":"started","bak_name":b,"start_time":DATETIME,"end_time":"in progress", "progress":0} + client.connect(broker,1883,60) + client.publish(topic, json.dumps(msg)) + client.disconnect() + + cmnd = "ssh root@omv.home.lan 'mkdir -p " + NEW_BACKUP_DIR + "'" + + if _EXECUTE: + status, output = subprocess.getstatusoutput(cmnd) + print("Create backup dir") + print(cmnd) + + + #cmnd = "rsync -av --delete {}/ --link-dest {} --exclude=\".cache\" {}".format(SOURCE_DIR, LATEST_LINK, BACKUP_PATH) + if _FIRST: + cmnd = f"rsync -avz --delete {SOURCE_DIR} --exclude=\"gitlab/logs/prometheus\" --exclude=\"home-assistant.log\" --exclude=\"gitlab/logs/*\" --exclude=\"esphome/config/.esphome\" --exclude=\".cache\" --exclude=\".git\" --exclude=\"var_lib_motioneye\" rsync://{BACKUP_HOST}{BACKUP_PATH}" + else: + cmnd = f"rsync -avz --delete {SOURCE_DIR} --link-dest {LATEST_LINK} --exclude=\"gitlab/logs/prometheus\" --exclude=\"home-assistant.log\" --exclude=\"gitlab/logs/*\" --exclude=\"esphome/config/.esphome\" --exclude=\".cache\" --exclude=\".git\" --exclude=\"var_lib_motioneye\" rsync://{BACKUP_HOST}{BACKUP_PATH}" + + ans = "y" + print(cmnd) + print("Sync files") + if _TEST: + + + ans = input("continue?") or "n" + if ans == "y" and _EXECUTE: + + # rsync --info=progress2 -avz --delete /share/docker_data/ --link-dest /m-server/docker_data/latest --exclude="gitlab/data/" --exclude="esphome/config/.esphome" --exclude="gitlab/logs/prometheus" --exclude=".cache" --exclude=".git" --exclude="var_lib_motioneye" /m-server/m-server/docker_data/newone1 + + + # input("????") + + status, output = subprocess.getstatusoutput(cmnd) + + cmnd = f"ssh root@omv.home.lan 'rm -rf {FULL_BACKUP_LATEST}'" + + #print(cmnd) + print("Removing latest link") + # input("????") + if _EXECUTE: + status, output = subprocess.getstatusoutput(cmnd) + if _FIRST: + cmnd = f"ssh root@omv.home.lan 'cd {NEW_BACKUP_DIR}; ln -s initial latest'" + else: + cmnd = f"ssh root@omv.home.lan 'cd {NEW_BACKUP_DIR}; ln -s {DATETIME} latest'" + print("Creating new latest link") + #print(cmnd) + # input("????") + if _EXECUTE: + status, output = subprocess.getstatusoutput(cmnd) + + + #Remove old + print("Removing old dirs") + # input("????") + #cmnd = "find {} -maxdepth 1 -type d -mtime +30 -exec rm -rf {{}} \;".format(BACKUP_DIR) + cmnd = f"cd {NEW_BACKUP_DIR} find ./ -maxdepth 1 -type d -mmin +30 -exec rm -rf {{}} \\;" + #print(cmnd) + # input("????") + if _EXECUTE: + status, output = subprocess.getstatusoutput(cmnd) + now = datetime.datetime.now() + ENDTIME = now.strftime("%Y-%m-%d_%H:%M:%S") + msg = {"status":"finished","bak_name":b,"start_time":DATETIME,"end_time":ENDTIME,"progress":0} + client.connect(broker,1883,10) + client.publish(topic, json.dumps(msg)) + client.disconnect() + + print("Getting size of FS") + #cmnd = "du -h --max-depth=0 {}".format(BACKUP_FS) + cmnd = "ssh root@omv.home.lan 'df -h /srv/dev-disk-by-uuid-2f843500-95b6-43b0-bea1-9b67032989b8|awk '\\''{ print $3 }'\\''|tail -1'" + print(cmnd) + status, output = subprocess.getstatusoutput(cmnd) + used_space = (output.split())[0] + now = datetime.datetime.now() + ENDJOB = now.strftime("%Y-%m-%d_%H:%M:%S") + print("Size : {}".format(used_space)) + print("Sending finished status") + msg = {"mode":_MODE,"status":"finished","bak_name":"complete","start_time":STARTTIME,"end_time":ENDJOB,"progress":0,"used_space":used_space} + print(msg) + client.connect(broker,1883,10) + client.publish(topic_sum, json.dumps(msg)) client.disconnect() - continue - SOURCE_DIR = backups[host][b]["source"] - now = datetime.datetime.now() - BACKUP_HOST = "root@omv.home.lan" - BACKUP_DEVICE = "/srv/dev-disk-by-uuid-2f843500-95b6-43b0-bea1-9b67032989b8" - BACKUP_DIR = f"/backup/{host}/{b}" - NEW_BACKUP_DIR = f"{BACKUP_DEVICE}/backup/{host}/{b}" - DATETIME = now.strftime("%Y-%m-%d_%H-%M-%S") - if _FIRST: - BACKUP_PATH="{}/initial".format(BACKUP_DIR) - else: - BACKUP_PATH="{}/{}".format(BACKUP_DIR, DATETIME) - LATEST_LINK="{}/latest".format(BACKUP_DIR) - FULL_BACKUP_LATEST = f"{NEW_BACKUP_DIR}/latest" - LATEST_LINK = f"/{host}/{b}/latest" + if _MODE == "auto": + hostup = True + cmnd = "ssh root@omv.home.lan 'systemctl suspend &'" + status, output = subprocess.getstatusoutput(cmnd) + while hostup: + #HOST_UP = os.system(f"ping -c 1 -w 2 omv.home.lan") == 0 + cmnd = f"ping -c 1 -w 2 {BACKUP_HOST}" + status, output = subprocess.getstatusoutput(cmnd) + # print(status) + # print(output) + + + if status == 0: + print(f"Backup host up, waiting - {n}\r", end="") + time.sleep(5) + n += 1 + else: + print("Backup host down " ) + hostup = False + + + + try: + url = "http://m-server.home.lan:8123/api/webhook/-0eWYFhSTzdusAO8jwQS9t1AT?mode=off" + x = requests.post(url) - msg = {"status":"started","bak_name":b,"start_time":DATETIME,"end_time":"in progress", "progress":0} - client.connect(broker,1883,60) - client.publish(topic, json.dumps(msg)) - client.disconnect() - - cmnd = "ssh root@omv.home.lan 'mkdir -p " + NEW_BACKUP_DIR + "'" - - if _EXECUTE: - status, output = subprocess.getstatusoutput(cmnd) - print("Create backup dir") - print(cmnd) - - - #cmnd = "rsync -av --delete {}/ --link-dest {} --exclude=\".cache\" {}".format(SOURCE_DIR, LATEST_LINK, BACKUP_PATH) - if _FIRST: - cmnd = f"rsync -avz --delete {SOURCE_DIR} --exclude=\"gitlab/logs/prometheus\" --exclude=\"home-assistant.log\" --exclude=\"gitlab/logs/*\" --exclude=\"esphome/config/.esphome\" --exclude=\".cache\" --exclude=\".git\" --exclude=\"var_lib_motioneye\" rsync://{BACKUP_HOST}{BACKUP_PATH}" - else: - cmnd = f"rsync -avz --delete {SOURCE_DIR} --link-dest {LATEST_LINK} --exclude=\"gitlab/logs/prometheus\" --exclude=\"home-assistant.log\" --exclude=\"gitlab/logs/*\" --exclude=\"esphome/config/.esphome\" --exclude=\".cache\" --exclude=\".git\" --exclude=\"var_lib_motioneye\" rsync://{BACKUP_HOST}{BACKUP_PATH}" - - ans = "y" - print(cmnd) - print("Sync files") - if _TEST: - - - ans = input("continue?") or "n" - if ans == "y" and _EXECUTE: - - # rsync --info=progress2 -avz --delete /share/docker_data/ --link-dest /m-server/docker_data/latest --exclude="gitlab/data/" --exclude="esphome/config/.esphome" --exclude="gitlab/logs/prometheus" --exclude=".cache" --exclude=".git" --exclude="var_lib_motioneye" /m-server/m-server/docker_data/newone1 - - - # input("????") - - status, output = subprocess.getstatusoutput(cmnd) - - cmnd = f"ssh root@omv.home.lan 'rm -rf {FULL_BACKUP_LATEST}'" - - #print(cmnd) - print("Removing latest link") - # input("????") - if _EXECUTE: - status, output = subprocess.getstatusoutput(cmnd) - if _FIRST: - cmnd = f"ssh root@omv.home.lan 'cd {NEW_BACKUP_DIR}; ln -s initial latest'" - else: - cmnd = f"ssh root@omv.home.lan 'cd {NEW_BACKUP_DIR}; ln -s {DATETIME} latest'" - print("Creating new latest link") - #print(cmnd) - # input("????") - if _EXECUTE: - status, output = subprocess.getstatusoutput(cmnd) - - - #Remove old - print("Removing old dirs") - # input("????") - #cmnd = "find {} -maxdepth 1 -type d -mtime +30 -exec rm -rf {{}} \;".format(BACKUP_DIR) - cmnd = f"cd {NEW_BACKUP_DIR} find ./ -maxdepth 1 -type d -mmin +30 -exec rm -rf {{}} \\;" - #print(cmnd) - # input("????") - if _EXECUTE: - status, output = subprocess.getstatusoutput(cmnd) - now = datetime.datetime.now() - ENDTIME = now.strftime("%Y-%m-%d_%H:%M:%S") - msg = {"status":"finished","bak_name":b,"start_time":DATETIME,"end_time":ENDTIME,"progress":0} - client.connect(broker,1883,10) - client.publish(topic, json.dumps(msg)) - client.disconnect() - - print("Getting size of FS") - #cmnd = "du -h --max-depth=0 {}".format(BACKUP_FS) - cmnd = "ssh root@omv.home.lan 'df -h /srv/dev-disk-by-uuid-2f843500-95b6-43b0-bea1-9b67032989b8|awk '\\''{ print $3 }'\\''|tail -1'" - print(cmnd) - status, output = subprocess.getstatusoutput(cmnd) - used_space = (output.split())[0] - now = datetime.datetime.now() - ENDJOB = now.strftime("%Y-%m-%d_%H:%M:%S") - print("Size : {}".format(used_space)) - print("Sending finished status") - msg = {"mode":_MODE,"status":"finished","bak_name":"complete","start_time":STARTTIME,"end_time":ENDJOB,"progress":0,"used_space":used_space} - print(msg) - client.connect(broker,1883,10) - client.publish(topic_sum, json.dumps(msg)) - client.disconnect() - - if _MODE == "auto": - hostup = True - cmnd = "ssh root@omv.home.lan 'systemctl suspend &'" - status, output = subprocess.getstatusoutput(cmnd) - while hostup: - #HOST_UP = os.system(f"ping -c 1 -w 2 omv.home.lan") == 0 - cmnd = f"ping -c 1 -w 2 {BACKUP_HOST}" - status, output = subprocess.getstatusoutput(cmnd) - # print(status) - # print(output) - - - if status == 0: - print(f"Backup host up, waiting - {n}\r", end="") - time.sleep(5) - n += 1 - else: - print("Backup host down " ) - hostup = False - - - - try: - url = "http://m-server.home.lan:8123/api/webhook/-0eWYFhSTzdusAO8jwQS9t1AT?mode=off" - - x = requests.post(url) - - print(x.text) - except: - pass - - \ No newline at end of file + print(x.text) + except: + pass + + \ No newline at end of file