Add core backup mechanism

This commit is contained in:
augustin64 2025-03-18 18:38:05 +01:00
parent e90661eeac
commit 3eca803f08
2 changed files with 50 additions and 2 deletions

View File

@ -1,3 +1,10 @@
PWD_URL = "https://my-domain/ABunchOfRandomCharactersBecauseThisIsNotReallyImportant.file" PWD_URL = "https://my-domain/ABunchOfRandomCharactersBecauseThisIsNotReallyImportant.file"
SSH_KEY_PWD_URL = "https://my-domain/ABunchOfRandomCharactersBecauseThisIsNotReallyImportant.file"
DISK = "/dev/sda" DISK = "/dev/sda"
MNT_LOCATION = "/mnt" MNT_LOCATION = "/mnt"
REMOTE_LOCATION = "192.168.1.5"
REMOTE_USER = "myuser"
BACKUP_MAP = [
("/home/remote/MyData", "backup-it-here") # myuser@192.168.1.5:/home/remote/MyData will be backed up to /mnt/{backup-date and "latest"}/backup-it-here
]

View File

@ -13,9 +13,12 @@ def log(*args, **kwargs):
if not os.path.exists(config.DISK): if not os.path.exists(config.DISK):
log_error(f"Drive {config.DISK} not present on the system") log_error(f"Drive {config.DISK} not present on the system")
passwd = requests.get(config.PWD_URL).text passwd = requests.get(config.PWD_URL).text.strip()
log("Got password :", passwd) log("Got password :", passwd)
ssh_key_passwd = requests.get(config.SSH_KEY_PWD_URL).text.strip()
log("Got password :", ssh_key_passwd)
subprocess.run([ subprocess.run([
"cryptsetup", "luksOpen", "cryptsetup", "luksOpen",
@ -27,7 +30,43 @@ subprocess.call([
config.MNT_LOCATION config.MNT_LOCATION
]) ])
log("Succesfully mounted, doing the backup stuff") log("Successfully mounted, doing the backup stuff")
os.makedirs(
os.path.join(config.MNT_LOCATION, "latest"),
exist_ok=True
)
for src, dest in config.BACKUP_MAP:
# We could also use a rsync daemon on the remote machine for pwd(-less) auth
subprocess.call(
[
"sshpass", "-P", "passphrase", "-e",
"rsync",
"-ahz",
"--partial",
"--delete",
"--stats",
f"{config.REMOTE_USER}@{config.REMOTE_LOCATION}:{src}",
os.path.join(config.MNT_LOCATION, "latest", dest),
],
env={**os.environ, "SSHPASS":ssh_key_passwd}
)
log("Rsync ok. Creating snapshot.")
subprocess.call([
"cp", "-a",
"--reflink=always", #! The partition must be Btrfs or XFS
os.path.join(config.MNT_LOCATION, "latest"),
os.path.join(config.MNT_LOCATION, datetime.today().strftime('%Y-%m-%d'))
])
log("Snapshot ok. Deleting unneeded backups.")
#TODO
log("Unmounting.")
subprocess.call([ subprocess.call([
"umount", config.MNT_LOCATION "umount", config.MNT_LOCATION
@ -37,3 +76,5 @@ subprocess.call([
"cryptsetup", "luksClose", "cryptsetup", "luksClose",
"/dev/mapper/backup" "/dev/mapper/backup"
]) ])
log("Done !")