admin-scripts/sync_from_remotes.py

61 lines
1.8 KiB
Python
Raw Normal View History

2024-10-15 00:07:17 +02:00
#!/usr/bin/env nix-shell
#! nix-shell -i python3 --packages python3 python312Packages.resend
import socket
import subprocess
from pathlib import Path
import resend
def send_email(program: str, log: str):
resend.api_key = Path("/usr/local/etc/resend_api_key.txt").read_text().strip("\n")
match log:
case "ok":
subj = f"[{socket.getfqdn()}] {program} OK ✅"
msg = f"{program} on {socket.getfqdn()} ran successfully!"
case "err":
subj = f"[{socket.getfqdn()}] {program} Error ❌"
msg = f"There was an error running {program} on {socket.getfqdn()}. Please investigate."
case _:
subj = ""
msg = ""
params: resend.Emails.SendParams = {
"from": "Admin <admin@hyperreal.coffee>",
"to": ["hyperreal@moonshadow.dev"],
"subject": subj,
"text": msg,
}
email = resend.Emails.send(params)
print(email)
def sync_from_remotes(src: str, dest: str):
rsync_cmd = ["rsync", "-avz", "--delete", src, dest]
try:
subprocess.run(rsync_cmd, check=True, capture_output=True, text=True)
print(f"Successful sync from {src} to {dest}")
except subprocess.CalledProcessError as e:
print(f"Error during sync from {src} to {dest}: {e}")
send_email("sync_from_remotes", "err")
if __name__ == "__main__":
remotes = [
(
"root@hyperreal.coffee:/srv/borgbackup/hyperreal/",
"/mnt/borgbackup/hyperreal",
),
("root@hyperreal.coffee:/home/mastodon/live/public/system/", "/mnt/mastodata"),
("root@desktop:/srv/borgbackup/moonshadow/", "/mnt/borgbackup/moonshadow"),
]
for remote in remotes:
sync_from_remotes(remote[0], remote[1])
send_email("sync_from_remotes", "ok")