mirror of
https://codeberg.org/hyperreal/admin-scripts
synced 2024-11-25 09:03:41 +01:00
Use age
This commit is contained in:
parent
8c0bffec41
commit
57eb219269
@ -4,7 +4,7 @@
|
|||||||
# ]
|
# ]
|
||||||
# ///
|
# ///
|
||||||
|
|
||||||
|
import os
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@ -12,8 +12,9 @@ from pathlib import Path
|
|||||||
import resend
|
import resend
|
||||||
|
|
||||||
|
|
||||||
def send_email(program: str, log: str):
|
def send_email(program: str, log: str, **kwargs: BaseException):
|
||||||
resend.api_key = Path("/usr/local/etc/resend_api_key.txt").read_text().strip("\n")
|
resend.api_key = Path("/usr/local/etc/resend_api_key.txt").read_text().strip("\n")
|
||||||
|
err_msg = kwargs.get("err_msg", None)
|
||||||
|
|
||||||
match log:
|
match log:
|
||||||
case "ok":
|
case "ok":
|
||||||
@ -21,7 +22,7 @@ def send_email(program: str, log: str):
|
|||||||
msg = f"{program} on {socket.getfqdn()} ran successfully!"
|
msg = f"{program} on {socket.getfqdn()} ran successfully!"
|
||||||
case "err":
|
case "err":
|
||||||
subj = f"[{socket.getfqdn()}] {program} Error ❌"
|
subj = f"[{socket.getfqdn()}] {program} Error ❌"
|
||||||
msg = f"There was an error running {program} on {socket.getfqdn()}. Please investigate."
|
msg = f"There was an error running {program} on {socket.getfqdn()}: {err_msg}.\n\nPlease investigate."
|
||||||
case _:
|
case _:
|
||||||
subj = ""
|
subj = ""
|
||||||
msg = ""
|
msg = ""
|
||||||
@ -50,8 +51,41 @@ def run_kbackup():
|
|||||||
print(f"kbackup ran successfully")
|
print(f"kbackup ran successfully")
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"Error running kbackup: {e}")
|
print(f"Error running kbackup: {e}")
|
||||||
send_email("sync_to_remotes", "err")
|
send_email("sync_to_remotes", "err", err_msg=e)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
def sync_to_remotes(src: str, dest: str):
|
def sync_to_remotes(src: str, dest: str):
|
||||||
@ -62,17 +96,23 @@ def sync_to_remotes(src: str, dest: str):
|
|||||||
print(f"Successful sync to {dest}")
|
print(f"Successful sync to {dest}")
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"Error during sync to {dest}: {e}")
|
print(f"Error during sync to {dest}: {e}")
|
||||||
send_email("sync_to_remotes", "err")
|
send_email("sync_to_remotes", "err", err_msg=e)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
except KeyboardInterrupt as e:
|
||||||
|
print(f"Sync to {dest} interrupted: {e}")
|
||||||
|
send_email("sync_to_remotes", "err", err_msg=e)
|
||||||
|
exit(130)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
remotes = [
|
remotes = [
|
||||||
("/home/jas/sync_to_remotes/", "wasabi:/desktop-backups-rep/"),
|
("/home/jas/sync_to_remotes/", "wasabi:/desktop-backups-moonshadow/"),
|
||||||
]
|
]
|
||||||
|
|
||||||
run_kbackup()
|
run_kbackup()
|
||||||
|
|
||||||
|
encrypt_tar()
|
||||||
|
|
||||||
for remote in remotes:
|
for remote in remotes:
|
||||||
sync_to_remotes(remote[0], remote[1])
|
sync_to_remotes(remote[0], remote[1])
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user