2024-11-09 00:18:47 +01:00
|
|
|
# /// script
|
|
|
|
# dependencies = [
|
|
|
|
# "resend",
|
|
|
|
# ]
|
|
|
|
# ///
|
|
|
|
|
2024-11-09 21:38:57 +01:00
|
|
|
import os
|
2024-10-15 06:01:52 +02:00
|
|
|
import socket
|
|
|
|
import subprocess
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import resend
|
|
|
|
|
|
|
|
|
2024-11-09 21:38:57 +01:00
|
|
|
def send_email(program: str, log: str, **kwargs: BaseException):
|
2024-10-15 06:01:52 +02:00
|
|
|
resend.api_key = Path("/usr/local/etc/resend_api_key.txt").read_text().strip("\n")
|
2024-11-09 21:38:57 +01:00
|
|
|
err_msg = kwargs.get("err_msg", None)
|
2024-10-15 06:01:52 +02:00
|
|
|
|
|
|
|
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 ❌"
|
2024-11-09 21:38:57 +01:00
|
|
|
msg = f"There was an error running {program} on {socket.getfqdn()}: {err_msg}.\n\nPlease investigate."
|
2024-10-15 06:01:52 +02:00
|
|
|
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}")
|
2024-11-09 21:38:57 +01:00
|
|
|
send_email("sync_to_remotes", "err", err_msg=e)
|
2024-10-15 06:01:52 +02:00
|
|
|
exit(1)
|
2024-11-09 21:38:57 +01:00
|
|
|
except KeyboardInterrupt as e:
|
|
|
|
print(f"Run of kbackup interrupted: {e}")
|
|
|
|
send_email("sync_to_remotes", "err", err_msg=e)
|
|
|
|
exit(130)
|
|
|
|
|
|
|
|
|
|
|
|
def encrypt_tar():
|
|
|
|
tar_dir = Path("/home/jas/sync_to_remotes")
|
|
|
|
for tarfile in tar_dir.iterdir():
|
|
|
|
if tarfile.name.endswith("tar"):
|
|
|
|
age_cmd = [
|
|
|
|
"age",
|
|
|
|
"-e",
|
|
|
|
"-i",
|
|
|
|
"/home/jas/documents/age-key.txt",
|
|
|
|
"-o",
|
|
|
|
f"{tarfile}.age",
|
|
|
|
tarfile,
|
|
|
|
]
|
|
|
|
|
|
|
|
print(f"Encrypting {tarfile}...")
|
|
|
|
try:
|
|
|
|
subprocess.run(age_cmd, check=True)
|
|
|
|
print(f"Successfully encrypted {tarfile}")
|
|
|
|
os.remove(tarfile)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print(f"Error during archive encryption of {tarfile}: {e}")
|
|
|
|
send_email("sync_to_remotes", "err", err_msg=e)
|
|
|
|
exit(1)
|
|
|
|
except KeyboardInterrupt as e:
|
|
|
|
print(f"Archive encryption of {tarfile} interrupted: {e}")
|
|
|
|
send_email("sync_to_remotes", "err", err_msg=e)
|
|
|
|
exit(130)
|
2024-10-15 06:01:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
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}")
|
2024-11-09 21:38:57 +01:00
|
|
|
send_email("sync_to_remotes", "err", err_msg=e)
|
2024-10-15 06:01:52 +02:00
|
|
|
exit(1)
|
2024-11-09 21:38:57 +01:00
|
|
|
except KeyboardInterrupt as e:
|
|
|
|
print(f"Sync to {dest} interrupted: {e}")
|
|
|
|
send_email("sync_to_remotes", "err", err_msg=e)
|
|
|
|
exit(130)
|
2024-10-15 06:01:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
remotes = [
|
2024-11-09 21:38:57 +01:00
|
|
|
("/home/jas/sync_to_remotes/", "wasabi:/desktop-backups-moonshadow/"),
|
2024-10-15 06:01:52 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
run_kbackup()
|
|
|
|
|
2024-11-09 21:38:57 +01:00
|
|
|
encrypt_tar()
|
|
|
|
|
2024-10-15 06:01:52 +02:00
|
|
|
for remote in remotes:
|
|
|
|
sync_to_remotes(remote[0], remote[1])
|
|
|
|
|
|
|
|
send_email("sync_to_remotes", "ok")
|