starting disk when borg runs

This commit is contained in:
2025-11-21 13:27:07 +01:00
parent 153c774692
commit c8239571de
6 changed files with 39 additions and 60 deletions

View File

@@ -5,6 +5,11 @@ import sys
import paho.mqtt.client as mqtt
import time
def check_backup_vg():
result = subprocess.run(["vgdisplay", "-cA"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
vgdisplay_output = result.stdout.strip().split("\n")
return any(line.split(":")[0].strip() == "backup" for line in vgdisplay_output)
def send_mqtt_message(topic, payload):
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) # Use the latest API version
client.connect("mqtt.chaos", 1883, 60)
@@ -57,44 +62,26 @@ 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
if mode == "on":
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
if not check_backup_vg():
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)
print(f"Activating VG backup")
subprocess.run(["vgchange", "-ay", "backup"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error executing vgdisplay: {e.stderr}")
print(f"Error executing vgchange: {e.stderr}")
exit(1)
else:
# OFF case: Only execute if nothing is mounted
if not running_units:
if not running_units and check_backup_vg():
print(f"Turning backup Disk OFF")
subprocess.run(["sync"], check=True)
try:
subprocess.run(["vgchange", "-an", "backup"], check=True)
subprocess.run(["sync"], 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")
send_mqtt_message("switch_backup/switch/switch_backup_power/command", "off")