automatic backup disk on/off

This commit is contained in:
2025-11-19 16:27:06 +01:00
parent 2e245eeefa
commit 153c774692
6 changed files with 135 additions and 27 deletions

View File

@@ -1,5 +1,24 @@
#!/usr/bin/env python3
import subprocess
import json
import sys
import paho.mqtt.client as mqtt
import time
def send_mqtt_message(topic, payload):
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) # Use the latest API version
client.connect("mqtt.chaos", 1883, 60)
client.publish(topic, payload)
client.disconnect()
def get_args(name='default', first='on'):
return first
mode = get_args(*sys.argv)
print(f"Mode is: {mode}")
if mode not in ["on", "off"]:
print("Error: mode must be either 'on' or 'off'")
sys.exit(1)
# Run the systemctl command and capture the output
try:
@@ -21,7 +40,7 @@ except json.JSONDecodeError as e:
print(f"Error parsing JSON: {e}")
exit(1)
# Filter units that are loaded, active, and sub: running
# Filter units that are loaded, active, and sub: running, and start with 'backup-'
running_units = []
for unit in units:
if (
@@ -36,3 +55,46 @@ for unit in units:
print("Units with load: loaded, active: active, sub: running:")
for unit in running_units:
print(f" - {unit['unit']}: {unit['description']}")
# Send MQTT message based on mode
if mode == "on" or (mode == "off" and running_units):
# Send ON MQTT message
print(f"Turning backup Disk ON")
send_mqtt_message("switch_backup/switch/switch_backup_power/command", "on")
time.sleep(15)
# ON case: Send ON message after waiting 5 seconds and running vgchange -ay backup if needed
if mode == "on":
# Check if 'backup' VG is active
try:
result = subprocess.run(
["vgdisplay", "-cA"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True
)
vgdisplay_output = result.stdout.strip().split("\n")
print(f"vgdisplay: {vgdisplay_output}")
backup_present = any(line.split(":")[0].strip() == "backup" for line in vgdisplay_output)
print(f"backup VG active? {backup_present}")
if not backup_present:
try:
print(f"Activating VG backup")
subprocess.run(["vgchange", "-ay", "backup"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error executing vgchange: {e.stderr}")
exit(1)
except subprocess.CalledProcessError as e:
print(f"Error executing vgdisplay: {e.stderr}")
exit(1)
else:
# OFF case: Only execute if nothing is mounted
if not running_units:
print(f"Turning backup Disk OFF")
subprocess.run(["sync"], check=True)
try:
subprocess.run(["vgchange", "-an", "backup"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error executing vgchange: {e.stderr}")
exit(1)
send_mqtt_message("switch_backup/switch/switch_backup_power/command", "off")