mirror of
https://gitlab.sectorq.eu/jaydee/omv_backup.git
synced 2025-07-02 08:08:32 +02:00
lala
This commit is contained in:
327
omv_backup.py
327
omv_backup.py
@ -12,6 +12,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import platform
|
import platform
|
||||||
import requests
|
import requests
|
||||||
|
import fnmatch
|
||||||
from wakeonlan import send_magic_packet
|
from wakeonlan import send_magic_packet
|
||||||
pid = os.getpid()
|
pid = os.getpid()
|
||||||
|
|
||||||
@ -63,6 +64,7 @@ for o, a in opts:
|
|||||||
elif o in ("-r", "--restore"):
|
elif o in ("-r", "--restore"):
|
||||||
_RESTORE = True
|
_RESTORE = True
|
||||||
_APP = a
|
_APP = a
|
||||||
|
print("RESTORE")
|
||||||
elif o in ("-b", "--backup"):
|
elif o in ("-b", "--backup"):
|
||||||
_BACKUP = True
|
_BACKUP = True
|
||||||
elif o in ("-d", "--dry"):
|
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(f"Port {port} on {BACKUP_HOST} is open.")
|
||||||
|
|
||||||
print("Starting")
|
print("Starting")
|
||||||
|
print(_RESTORE)
|
||||||
if _RESTORE:
|
if _RESTORE:
|
||||||
|
print("Starting Restore")
|
||||||
if _APP == "all":
|
if _APP == "all":
|
||||||
_APP = ["nginx","ha","gitlab","mailu","bitwarden","esphome","grafana","ingluxdb","kestra","matter-server","mosquitto","octoprint","octoprint2","pihole","unify_block","webhub"]
|
_APP = ["nginx","ha","gitlab","mailu","bitwarden","esphome","grafana","ingluxdb","kestra","matter-server","mosquitto","octoprint","octoprint2","pihole","unify_block","webhub"]
|
||||||
else:
|
else:
|
||||||
_APP = _APP.split(",")
|
_APP = _APP.split(",")
|
||||||
|
|
||||||
|
|
||||||
for app in _APP:
|
for app in _APP:
|
||||||
topic = "sectorq/omv/restore/{}".format(app)
|
topic = "sectorq/omv/restore/{}".format(app)
|
||||||
@ -179,12 +182,17 @@ if _RESTORE:
|
|||||||
client.publish(topic, json.dumps(msg))
|
client.publish(topic, json.dumps(msg))
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
|
DATETIME = now.strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
BACKUP_HOST = "root@omv.home.lan"
|
BACKUP_HOST = "root@omv.home.lan"
|
||||||
BACKUP_DEVICE = "/srv/dev-disk-by-uuid-2f843500-95b6-43b0-bea1-9b67032989b8"
|
BACKUP_DEVICE = "/srv/dev-disk-by-uuid-2f843500-95b6-43b0-bea1-9b67032989b8"
|
||||||
BACKUP_DIR = f"/backup/{host}"
|
BACKUP_DIR = f"/backup/{host}"
|
||||||
NEW_BACKUP_DIR = f"/backup/m-server/docker_data/latest/{app}"
|
if app == "fail2ban":
|
||||||
DATETIME = now.strftime("%Y-%m-%d_%H-%M-%S")
|
print("?>?????")
|
||||||
SOURCE_DIR = f"/share/docker_data/"
|
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:
|
if _FIRST:
|
||||||
BACKUP_PATH="{}/initial".format(BACKUP_DIR)
|
BACKUP_PATH="{}/initial".format(BACKUP_DIR)
|
||||||
else:
|
else:
|
||||||
@ -245,6 +253,19 @@ if _RESTORE:
|
|||||||
print("Start docker")
|
print("Start docker")
|
||||||
cmnd = "docker start heimdall"
|
cmnd = "docker start heimdall"
|
||||||
status, output = subprocess.getstatusoutput(cmnd)
|
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":
|
elif app == "nginx":
|
||||||
print("Stopping docker")
|
print("Stopping docker")
|
||||||
cmnd = "docker stop nginx-app-1"
|
cmnd = "docker stop nginx-app-1"
|
||||||
@ -314,148 +335,164 @@ if _RESTORE:
|
|||||||
|
|
||||||
|
|
||||||
if _BACKUP:
|
if _BACKUP:
|
||||||
print("Backup")
|
|
||||||
for b in backups[host]:
|
while True:
|
||||||
topic = "sectorq/omv/backups/{}".format(b.lower())
|
directory = '/backups/'
|
||||||
if not backups[host][b]["active"]:
|
count = len(fnmatch.filter(os.listdir(directory), '*.*'))
|
||||||
print("Backup {} is not active!".format(b))
|
print('File Count:', count)
|
||||||
client.connect(broker,1883,60)
|
time.sleep(10)
|
||||||
msg = {"status":"inactive","bak_name":b,"start_time":"inactive","end_time":"inactive","progress":0}
|
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()
|
client.disconnect()
|
||||||
continue
|
|
||||||
|
|
||||||
SOURCE_DIR = backups[host][b]["source"]
|
if _MODE == "auto":
|
||||||
now = datetime.datetime.now()
|
hostup = True
|
||||||
BACKUP_HOST = "root@omv.home.lan"
|
cmnd = "ssh root@omv.home.lan 'systemctl suspend &'"
|
||||||
BACKUP_DEVICE = "/srv/dev-disk-by-uuid-2f843500-95b6-43b0-bea1-9b67032989b8"
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
BACKUP_DIR = f"/backup/{host}/{b}"
|
while hostup:
|
||||||
NEW_BACKUP_DIR = f"{BACKUP_DEVICE}/backup/{host}/{b}"
|
#HOST_UP = os.system(f"ping -c 1 -w 2 omv.home.lan") == 0
|
||||||
DATETIME = now.strftime("%Y-%m-%d_%H-%M-%S")
|
cmnd = f"ping -c 1 -w 2 {BACKUP_HOST}"
|
||||||
if _FIRST:
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
BACKUP_PATH="{}/initial".format(BACKUP_DIR)
|
# print(status)
|
||||||
else:
|
# print(output)
|
||||||
BACKUP_PATH="{}/{}".format(BACKUP_DIR, DATETIME)
|
|
||||||
LATEST_LINK="{}/latest".format(BACKUP_DIR)
|
|
||||||
FULL_BACKUP_LATEST = f"{NEW_BACKUP_DIR}/latest"
|
if status == 0:
|
||||||
LATEST_LINK = f"/{host}/{b}/latest"
|
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}
|
print(x.text)
|
||||||
client.connect(broker,1883,60)
|
except:
|
||||||
client.publish(topic, json.dumps(msg))
|
pass
|
||||||
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
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user