mirror of
https://gitlab.sectorq.eu/jaydee/omv_backup.git
synced 2025-07-01 15:48:33 +02:00
lala
This commit is contained in:
294
omv_backup_v2.py
294
omv_backup_v2.py
@ -300,161 +300,161 @@ if _BACKUP:
|
||||
|
||||
while True:
|
||||
directory = '/backups/'
|
||||
count = len(fnmatch.filter(os.listdir(directory), '*.*'))
|
||||
print('File Count:', count)
|
||||
time.sleep(10)
|
||||
continue
|
||||
|
||||
count = len(fnmatch.filter(os.listdir(directory), '.*'))
|
||||
|
||||
|
||||
# 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}
|
||||
print('File Count:', count)
|
||||
if count == 0:
|
||||
time.sleep(10)
|
||||
continue
|
||||
else:
|
||||
# iterate over files in
|
||||
# that directory
|
||||
for filename in os.scandir(directory):
|
||||
if filename.is_file():
|
||||
print(filename.path)
|
||||
continue
|
||||
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.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()
|
||||
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"
|
||||
cmnd = "ssh root@omv.home.lan 'mkdir -p " + NEW_BACKUP_DIR + "'"
|
||||
|
||||
if _EXECUTE:
|
||||
status, output = subprocess.getstatusoutput(cmnd)
|
||||
print("Create backup dir")
|
||||
print(cmnd)
|
||||
|
||||
|
||||
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
|
||||
#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:
|
||||
print("Backup host down " )
|
||||
hostup = False
|
||||
|
||||
|
||||
|
||||
try:
|
||||
url = "http://m-server.home.lan:8123/api/webhook/-0eWYFhSTzdusAO8jwQS9t1AT?mode=off"
|
||||
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}"
|
||||
|
||||
x = requests.post(url)
|
||||
ans = "y"
|
||||
print(cmnd)
|
||||
print("Sync files")
|
||||
if _TEST:
|
||||
|
||||
|
||||
ans = input("continue?") or "n"
|
||||
if ans == "y" and _EXECUTE:
|
||||
|
||||
print(x.text)
|
||||
except:
|
||||
pass
|
||||
|
||||
# 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