mirror of
https://codeberg.org/hyperreal/admin-scripts
synced 2024-11-01 08:03:05 +01:00
Add sync_to_remotes.py
This commit is contained in:
parent
8700b620c8
commit
f5d5c85d8f
@ -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__":
|
||||
|
@ -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
|
76
sync_to_remotes.py
Executable file
76
sync_to_remotes.py
Executable file
@ -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 <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/", "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")
|
Loading…
Reference in New Issue
Block a user