diff --git a/sync_from_remotes.py b/sync_from_remotes.py index c1ca5d9..44725dd 100755 --- a/sync_from_remotes.py +++ b/sync_from_remotes.py @@ -37,11 +37,12 @@ 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) + subprocess.run(rsync_cmd, check=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") + exit(1) if __name__ == "__main__": diff --git a/sync_to_remotes b/sync_to_remotes deleted file mode 100755 index 54f428c..0000000 --- a/sync_to_remotes +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env bash - -# If the -q (quiet) flag is supplied, send all output to /dev/null. -if [[ "$1" == "-q" ]]; then - exec >/dev/null 2>&1 -fi - -if ! kbackup --verbose --autobg /home/jas/documents/default.kbp; then - logger -t sync_to_remotes -s "Error running kbackup" - /usr/local/bin/resend_status.py sync_to_remotes err - exit 1 -fi - -if ! rclone sync --transfers 8 -P /home/jas/sync_to_remotes/ protondrive:/desktop-backups/ ; then - logger -t sync_to_remotes -s "Error running rclone for protondrive remote" - /usr/local/bin/resend_status.py sync_to_remotes err - exit 1 -fi - -if ! rclone sync --transfers 8 -P /home/jas/sync_to_remotes/ wasabi:/desktop-backups-rep/ ; then - logger -t sync_to_remotes -s "Error running rclone for wasabi remote" - /usr/local/bin/resend_status.py sync_to_remotes err - exit 1 -fi - -logger -t sync_to_remotes "sync_to_remotes ran successfully" -/usr/local/bin/resend_status.py sync_to_remotes ok - -exit 0 diff --git a/sync_to_remotes.py b/sync_to_remotes.py new file mode 100755 index 0000000..e658bf8 --- /dev/null +++ b/sync_to_remotes.py @@ -0,0 +1,76 @@ +#!/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 ", + "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/", "protondrive:/desktop-backups/"), + ("/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")