diff --git a/src/example_config.py b/src/example_config.py index 6b2dff6..bdba900 100644 --- a/src/example_config.py +++ b/src/example_config.py @@ -1,3 +1,10 @@ PWD_URL = "https://my-domain/ABunchOfRandomCharactersBecauseThisIsNotReallyImportant.file" +SSH_KEY_PWD_URL = "https://my-domain/ABunchOfRandomCharactersBecauseThisIsNotReallyImportant.file" DISK = "/dev/sda" 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 +] \ No newline at end of file diff --git a/src/main.py b/src/main.py index 14863a3..ee50092 100644 --- a/src/main.py +++ b/src/main.py @@ -13,9 +13,12 @@ def log(*args, **kwargs): if not os.path.exists(config.DISK): 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) +ssh_key_passwd = requests.get(config.SSH_KEY_PWD_URL).text.strip() +log("Got password :", ssh_key_passwd) + subprocess.run([ "cryptsetup", "luksOpen", @@ -27,7 +30,43 @@ subprocess.call([ 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([ "umount", config.MNT_LOCATION @@ -37,3 +76,5 @@ subprocess.call([ "cryptsetup", "luksClose", "/dev/mapper/backup" ]) + +log("Done !") \ No newline at end of file