mirror of
https://gitlab.sectorq.eu/jaydee/omv_backup.git
synced 2025-07-01 23:58:33 +02:00
added v3
This commit is contained in:
@ -93,7 +93,6 @@ try:
|
|||||||
except:
|
except:
|
||||||
client = mqtt_client.Client()
|
client = mqtt_client.Client()
|
||||||
client.username_pw_set(mqtt_username, mqtt_password)
|
client.username_pw_set(mqtt_username, mqtt_password)
|
||||||
client.connect(broker,1883,60)
|
|
||||||
|
|
||||||
|
|
||||||
backups = {
|
backups = {
|
||||||
@ -150,8 +149,13 @@ hm = socket.gethostbyaddr(BACKUP_HOST)
|
|||||||
|
|
||||||
print("Starting")
|
print("Starting")
|
||||||
print(_RESTORE)
|
print(_RESTORE)
|
||||||
|
def send_mqtt_message(msg):
|
||||||
|
try:
|
||||||
|
client.connect(broker,1883,60)
|
||||||
|
client.publish(topic, json.dumps(msg))
|
||||||
|
client.disconnect()
|
||||||
|
except:
|
||||||
|
print("Failed to send")
|
||||||
|
|
||||||
if _SYNC:
|
if _SYNC:
|
||||||
containers = ["HomeAssistant","webhub-web-1","heimdall","pihole","mosquitto-mosquitto-1","mailu3-redis-1","mailu3-webmail-1","mailu3-resolver-1","mailu3-antispam-1","mailu3-webdav-1","mailu3-smtp-1","mailu3-oletools-1","mailu3-front-1","mailu3-fetchmail-1","mailu3-imap-1","matter-server","piper-en","openwakeword","whisper-en","auth-worker-1","auth-server-1","auth-authentik_ldap-1","auth-redis-1","auth-postgresql-1","nginx-app-1"]
|
containers = ["HomeAssistant","webhub-web-1","heimdall","pihole","mosquitto-mosquitto-1","mailu3-redis-1","mailu3-webmail-1","mailu3-resolver-1","mailu3-antispam-1","mailu3-webdav-1","mailu3-smtp-1","mailu3-oletools-1","mailu3-front-1","mailu3-fetchmail-1","mailu3-imap-1","matter-server","piper-en","openwakeword","whisper-en","auth-worker-1","auth-server-1","auth-authentik_ldap-1","auth-redis-1","auth-postgresql-1","nginx-app-1"]
|
||||||
@ -191,15 +195,14 @@ if _RESTORE:
|
|||||||
#input("????")
|
#input("????")
|
||||||
else:
|
else:
|
||||||
_APP = _APP.split(",")
|
_APP = _APP.split(",")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for app in _APP:
|
for app in _APP:
|
||||||
topic = "sectorq/amd/restore/{}".format(app)
|
topic = "sectorq/amd/restore/{}".format(app)
|
||||||
client.connect(broker,1883,60)
|
|
||||||
msg = {"status":"inactive","bak_name":app,"start_time":"inactive","end_time":"inactive","progress":0}
|
msg = {"status":"inactive","bak_name":app,"start_time":"inactive","end_time":"inactive","progress":0}
|
||||||
|
send_mqtt_message(msg)
|
||||||
client.publish(topic, json.dumps(msg))
|
|
||||||
client.disconnect()
|
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
DATETIME = now.strftime("%Y-%m-%d_%H-%M-%S")
|
DATETIME = now.strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
BACKUP_HOST = f"root@amd.home.lan"
|
BACKUP_HOST = f"root@amd.home.lan"
|
||||||
@ -238,10 +241,7 @@ if _RESTORE:
|
|||||||
|
|
||||||
|
|
||||||
msg = {"status":"started","bak_name":app,"start_time":DATETIME,"end_time":"in progress", "progress":0}
|
msg = {"status":"started","bak_name":app,"start_time":DATETIME,"end_time":"in progress", "progress":0}
|
||||||
client.connect(broker,1883,60)
|
send_mqtt_message(msg)
|
||||||
client.publish(topic, json.dumps(msg),2,True)
|
|
||||||
client.disconnect()
|
|
||||||
|
|
||||||
print("Create backup dir")
|
print("Create backup dir")
|
||||||
print(cmnd)
|
print(cmnd)
|
||||||
|
|
||||||
@ -273,6 +273,10 @@ if _RESTORE:
|
|||||||
status, output = subprocess.getstatusoutput(cmnd)
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
# cmnd = "docker start heimdall"
|
# cmnd = "docker start heimdall"
|
||||||
# status, output = subprocess.getstatusoutput(cmnd)
|
# status, output = subprocess.getstatusoutput(cmnd)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if app == "ha":
|
if app == "ha":
|
||||||
print("Stopping docker")
|
print("Stopping docker")
|
||||||
cmnd = "docker stop heimdall"
|
cmnd = "docker stop heimdall"
|
||||||
@ -288,7 +292,7 @@ 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":
|
elif app == "fail2ban":
|
||||||
print("Stopping docker")
|
print("Stopping docker")
|
||||||
cmnd = f"rsync -avz --delete {BACKUP_HOST}:{BACKUP_DEVICE}{NEW_BACKUP_DIR} {SOURCE_DIR}"
|
cmnd = f"rsync -avz --delete {BACKUP_HOST}:{BACKUP_DEVICE}{NEW_BACKUP_DIR} {SOURCE_DIR}"
|
||||||
ans = "y"
|
ans = "y"
|
||||||
@ -301,7 +305,7 @@ 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)
|
||||||
elif app == "nginx":
|
elif app == "nginx1":
|
||||||
print("Stopping docker")
|
print("Stopping docker")
|
||||||
cmnd = "docker stop nginx-app-1"
|
cmnd = "docker stop nginx-app-1"
|
||||||
status, output = subprocess.getstatusoutput(cmnd)
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
@ -319,7 +323,7 @@ if _RESTORE:
|
|||||||
print(cmnd)
|
print(cmnd)
|
||||||
status, output = subprocess.getstatusoutput(cmnd)
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
|
|
||||||
cmnd = 'egrep -l "# kestra.sectorq.eu|# auth.sectorq.eu|# ha.sectorq.eu|# pw.sectorq.eu|# semaphore.sectorq.eu|# sectorq.eu|# gitlab.sectorq.eu|# ha.sectorq.eu" /share/docker_data/nginx/data/nginx/proxy_host/*'
|
cmnd = 'egrep -l "# bazarr.sectorq.eu|# gitea.sectorq.eu|# jf.sectorq.eu|# kestra.sectorq.eu|# auth.sectorq.eu|# ha.sectorq.eu|# pw.sectorq.eu|# semaphore.sectorq.eu|# sectorq.eu|# gitlab.sectorq.eu|# ha.sectorq.eu" /share/docker_data/nginx/data/nginx/proxy_host/*'
|
||||||
status, output = subprocess.getstatusoutput(cmnd)
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
print(output.splitlines())
|
print(output.splitlines())
|
||||||
for file in output.splitlines():
|
for file in output.splitlines():
|
||||||
@ -350,18 +354,14 @@ if _RESTORE:
|
|||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
ENDTIME = now.strftime("%Y-%m-%d_%H:%M:%S")
|
ENDTIME = now.strftime("%Y-%m-%d_%H:%M:%S")
|
||||||
msg = {"status":"finished","bak_name":app,"start_time":DATETIME,"end_time":ENDTIME,"progress":0}
|
msg = {"status":"finished","bak_name":app,"start_time":DATETIME,"end_time":ENDTIME,"progress":0}
|
||||||
client.connect(broker,1883,10)
|
send_mqtt_message(msg)
|
||||||
client.publish(topic, json.dumps(msg),2,True)
|
|
||||||
client.disconnect()
|
|
||||||
|
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
ENDJOB = now.strftime("%Y-%m-%d_%H:%M:%S")
|
ENDJOB = now.strftime("%Y-%m-%d_%H:%M:%S")
|
||||||
print("Sending finished status")
|
print("Sending finished status")
|
||||||
msg = {"mode":_MODE,"status":"finished","bak_name":"complete","start_time":STARTTIME,"end_time":ENDJOB,"progress":0,"used_space":"?"}
|
msg = {"mode":_MODE,"status":"finished","bak_name":"complete","start_time":STARTTIME,"end_time":ENDJOB,"progress":0,"used_space":"?"}
|
||||||
print(msg)
|
print(msg)
|
||||||
client.connect(broker,1883,10)
|
send_mqtt_message(msg)
|
||||||
client.publish(topic_sum, json.dumps(msg),2,True)
|
|
||||||
client.disconnect()
|
|
||||||
|
|
||||||
if _MODE == "auto":
|
if _MODE == "auto":
|
||||||
cmnd = "ssh root@amd.home.lan 'systemctl suspend &'"
|
cmnd = "ssh root@amd.home.lan 'systemctl suspend &'"
|
||||||
@ -384,8 +384,7 @@ if _BACKUP:
|
|||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
STARTTIME = now.strftime("%Y-%m-%d_%H:%M:%S")
|
STARTTIME = now.strftime("%Y-%m-%d_%H:%M:%S")
|
||||||
msg = {"mode":_MODE, "status":"started","bak_name":"complete","host":"","cur_job":"","start_time":STARTTIME,"end_time":"in progress","progress":0,"finished":",".join(finished)}
|
msg = {"mode":_MODE, "status":"started","bak_name":"complete","host":"","cur_job":"","start_time":STARTTIME,"end_time":"in progress","progress":0,"finished":",".join(finished)}
|
||||||
client.publish(topic_sum, json.dumps(msg),qos=0, retain=True);
|
send_mqtt_message(msg)
|
||||||
client.disconnect()
|
|
||||||
# iterate over files in
|
# iterate over files in
|
||||||
# that directory
|
# that directory
|
||||||
|
|
||||||
@ -399,11 +398,8 @@ if _BACKUP:
|
|||||||
topic = "sectorq/amd/backups"
|
topic = "sectorq/amd/backups"
|
||||||
if not backups[host]["jobs"][b]["active"]:
|
if not backups[host]["jobs"][b]["active"]:
|
||||||
print("Backup {} is not active!".format(b))
|
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}
|
msg = {"status":"inactive","bak_name":b,"start_time":"inactive","end_time":"inactive","progress":0}
|
||||||
|
send_mqtt_message(msg)
|
||||||
client.publish(topic, json.dumps(msg),qos=0, retain=True)
|
|
||||||
client.disconnect()
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
SOURCE_DIR = backups[host]["jobs"][b]["source"]
|
SOURCE_DIR = backups[host]["jobs"][b]["source"]
|
||||||
@ -460,7 +456,9 @@ if _BACKUP:
|
|||||||
|
|
||||||
status, output = subprocess.getstatusoutput(cmnd)
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
#proc = subprocess.Popen(cmnd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,cwd = "/myapps/",shell=True)
|
#proc = subprocess.Popen(cmnd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,cwd = "/myapps/",shell=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cmnd = f"rm -rf {FULL_BACKUP_LATEST}"
|
cmnd = f"rm -rf {FULL_BACKUP_LATEST}"
|
||||||
|
|
||||||
print(cmnd)
|
print(cmnd)
|
||||||
@ -493,9 +491,7 @@ if _BACKUP:
|
|||||||
#msg = {"status":"finished","bak_name":b,"start_time":DATETIME,"end_time":ENDTIME,"progress":0}
|
#msg = {"status":"finished","bak_name":b,"start_time":DATETIME,"end_time":ENDTIME,"progress":0}
|
||||||
finished.append(b)
|
finished.append(b)
|
||||||
msg = {"mode":_MODE, "status":"finished","bak_name":"complete","host":host,"cur_job":b,"start_time":ENDTIME,"end_time":"in progress","progress":0,"finished":",".join(finished)}
|
msg = {"mode":_MODE, "status":"finished","bak_name":"complete","host":host,"cur_job":b,"start_time":ENDTIME,"end_time":"in progress","progress":0,"finished":",".join(finished)}
|
||||||
client.connect(broker,1883,10)
|
send_mqtt_message(msg)
|
||||||
client.publish(topic, json.dumps(msg),qos=0, retain=True)
|
|
||||||
client.disconnect()
|
|
||||||
|
|
||||||
print("Getting size of FS")
|
print("Getting size of FS")
|
||||||
cmnd = "df -h /mnt/raid|awk '{ print $3 }'|tail -1"
|
cmnd = "df -h /mnt/raid|awk '{ print $3 }'|tail -1"
|
||||||
@ -511,9 +507,7 @@ if _BACKUP:
|
|||||||
print(msg)
|
print(msg)
|
||||||
|
|
||||||
|
|
||||||
client.connect(broker,1883,10)
|
send_mqtt_message(msg)
|
||||||
client.publish(topic_sum, json.dumps(msg),qos=0, retain=True)
|
|
||||||
client.disconnect()
|
|
||||||
|
|
||||||
os.remove(filename.path)
|
os.remove(filename.path)
|
||||||
# if _MODE == "auto":
|
# if _MODE == "auto":
|
||||||
|
Reference in New Issue
Block a user