admin-scripts/sync_to_remotes.py

80 lines
1.9 KiB
Python
Executable File

# /// script
# dependencies = [
# "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 run_kbackup():
kbackup_cmd = [
"kbackup",
"--verbose",
"--autobg",
"/home/jas/documents/default.kbp",
]
try:
subprocess.run(kbackup_cmd, check=True, text=True)
print(f"kbackup ran successfully")
except subprocess.CalledProcessError as e:
print(f"Error running kbackup: {e}")
send_email("sync_to_remotes", "err")
exit(1)
def sync_to_remotes(src: str, dest: str):
rclone_cmd = ["rclone", "sync", "--transfers", "8", src, dest]
try:
subprocess.run(rclone_cmd, check=True, text=True)
print(f"Successful sync to {dest}")
except subprocess.CalledProcessError as e:
print(f"Error during sync to {dest}: {e}")
send_email("sync_to_remotes", "err")
exit(1)
if __name__ == "__main__":
remotes = [
("/home/jas/sync_to_remotes/", "wasabi:/desktop-backups-rep/"),
]
run_kbackup()
for remote in remotes:
sync_to_remotes(remote[0], remote[1])
send_email("sync_to_remotes", "ok")