starting disk when borg runs
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
- name: reload systemd
|
- name: reload systemd
|
||||||
command: systemctl daemon-reload
|
command: systemctl daemon-reload
|
||||||
|
|
||||||
|
- name: reload sshd
|
||||||
|
command: systemctl reload sshd
|
||||||
|
|
||||||
- name: Restart NGINX
|
- name: Restart NGINX
|
||||||
service:
|
service:
|
||||||
name: nginx
|
name: nginx
|
||||||
state: restarted
|
state: restarted
|
||||||
|
|
||||||
|
|
||||||
- name: Run APT update
|
- name: Run APT update
|
||||||
command: apt update -y
|
command: apt update -y
|
||||||
@@ -5,6 +5,11 @@ import sys
|
|||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
import time
|
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):
|
def send_mqtt_message(topic, payload):
|
||||||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) # Use the latest API version
|
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) # Use the latest API version
|
||||||
client.connect("mqtt.chaos", 1883, 60)
|
client.connect("mqtt.chaos", 1883, 60)
|
||||||
@@ -57,44 +62,26 @@ for unit in running_units:
|
|||||||
print(f" - {unit['unit']}: {unit['description']}")
|
print(f" - {unit['unit']}: {unit['description']}")
|
||||||
|
|
||||||
# Send MQTT message based on mode
|
# Send MQTT message based on mode
|
||||||
if mode == "on" or (mode == "off" and running_units):
|
if mode == "on":
|
||||||
# Send ON MQTT message
|
|
||||||
print(f"Turning backup Disk ON")
|
print(f"Turning backup Disk ON")
|
||||||
send_mqtt_message("switch_backup/switch/switch_backup_power/command", "on")
|
send_mqtt_message("switch_backup/switch/switch_backup_power/command", "on")
|
||||||
time.sleep(15)
|
time.sleep(15)
|
||||||
# ON case: Send ON message after waiting 5 seconds and running vgchange -ay backup if needed
|
if not check_backup_vg():
|
||||||
if mode == "on":
|
|
||||||
# Check if 'backup' VG is active
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
print(f"Activating VG backup")
|
||||||
["vgdisplay", "-cA"],
|
subprocess.run(["vgchange", "-ay", "backup"], check=True)
|
||||||
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:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"Error executing vgdisplay: {e.stderr}")
|
print(f"Error executing vgchange: {e.stderr}")
|
||||||
exit(1)
|
exit(1)
|
||||||
else:
|
else:
|
||||||
# OFF case: Only execute if nothing is mounted
|
# 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")
|
print(f"Turning backup Disk OFF")
|
||||||
subprocess.run(["sync"], check=True)
|
subprocess.run(["sync"], check=True)
|
||||||
try:
|
try:
|
||||||
subprocess.run(["vgchange", "-an", "backup"], check=True)
|
subprocess.run(["vgchange", "-an", "backup"], check=True)
|
||||||
|
subprocess.run(["sync"], check=True)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"Error executing vgchange: {e.stderr}")
|
print(f"Error executing vgchange: {e.stderr}")
|
||||||
exit(1)
|
exit(1)
|
||||||
send_mqtt_message("switch_backup/switch/switch_backup_power/command", "off")
|
send_mqtt_message("switch_backup/switch/switch_backup_power/command", "off")
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ After=network.target
|
|||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
RemainAfterExit=no
|
||||||
ExecStart=/usr/local/bin/backup-disk-onoff.py %I
|
ExecStart=/usr/local/bin/backup-disk-onoff.py %I
|
||||||
RemainAfterExit=yes
|
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=timers.target
|
WantedBy=timers.target
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#!/usr/bin/bash
|
|
||||||
#
|
|
||||||
#set -Eeuo pipefail
|
|
||||||
#
|
|
||||||
MQTT='mosquitto_pub -h mqtt.chaos -t switch_backup/switch'
|
|
||||||
BASEDIR="/backup"
|
|
||||||
|
|
||||||
|
|
||||||
#${MQTT}/debug -m "ARGS: \"$@\""
|
|
||||||
|
|
||||||
#check for an unmount event
|
|
||||||
if [[ "$@" == "CHECK-MOUNTS-AND-SHUTDOWN" ]]; then
|
|
||||||
OPEN_FILES=$(lsof ${BASEDIR}/* |grep -c ${BASEDIR})
|
|
||||||
[[ ${OPEN_FILES} -eq 0 ]] && umount ${BASEDIR}/*
|
|
||||||
MOUNTS=$(grep -e ${BASEDIR} /proc/self/mounts | grep -v autofs -c)
|
|
||||||
[[ ${MOUNTS} -gt 0 ]] && exit 0 #We still have mounts
|
|
||||||
#No mounts left, shutdown disk
|
|
||||||
vgchange -an backup >/dev/null
|
|
||||||
sync
|
|
||||||
sleep 5
|
|
||||||
${MQTT}/switch_backup_power/command -m OFF
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
#default case, echo mount string for autofs
|
|
||||||
${MQTT}/switch_backup_power/command -m ON
|
|
||||||
sleep 15
|
|
||||||
vgchange -ay backup >/dev/null
|
|
||||||
|
|
||||||
echo "-fstype=btrfs,defaults,compress=lzo :/dev/backup/${1}"
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
#Ansible
|
||||||
|
Match User borg
|
||||||
|
ForceCommand /bin/bash -c 'sudo /usr/local/bin/backup-disk-onoff.py on >/dev/null; borg serve'
|
||||||
@@ -18,8 +18,7 @@
|
|||||||
Type: btrfs
|
Type: btrfs
|
||||||
Options: defaults,compress=lzo,space_cache=v2
|
Options: defaults,compress=lzo,space_cache=v2
|
||||||
Unit:
|
Unit:
|
||||||
After: network.target backup-disk-onoff@on.service
|
After: network.target
|
||||||
Requires: backup-disk-onoff@on.service
|
|
||||||
Install:
|
Install:
|
||||||
WantedBy: network.target
|
WantedBy: network.target
|
||||||
catena_automount:
|
catena_automount:
|
||||||
@@ -70,5 +69,23 @@
|
|||||||
name: backup-disk-off.timer
|
name: backup-disk-off.timer
|
||||||
enabled: true
|
enabled: true
|
||||||
state: started
|
state: started
|
||||||
|
|
||||||
|
# Setup ssh and sudo
|
||||||
|
- name: Allow borg user to start disk
|
||||||
|
community.general.sudoers:
|
||||||
|
name: borg-start-disk
|
||||||
|
nopassword: true
|
||||||
|
user: borg
|
||||||
|
commands:
|
||||||
|
- /usr/local/bin/backup-disk-onoff.py
|
||||||
|
|
||||||
|
- name: start disk on login
|
||||||
|
copy:
|
||||||
|
dest: /etc/ssh/sshd_config.d/borg-start-backupdisk.conf
|
||||||
|
src: sshd-borg-start-backupdisk.conf
|
||||||
|
notify: reload sshd
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user