Automatically create (incremental) backups of zfs snapshots on a file server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

67 lines
1.9 KiB

#!/usr/local/bin/python
# simple python module to copy incremental backups of zfs snapshots to a
# remote server
import pathlib
import subprocess
SSH_KEY_FILE = "/mnt/Datenspeicher/snap-backup-dataset/backup_key"
SSH_REMOTE = "zfs_snap_backup@etha.cpi.imtek.uni-freiburg.de"
REMOTE_PATH = "zfs-backups"
SCP_REMOTE_URL = f"{SSH_REMOTE}:~/{REMOTE_PATH}/"
ZFS_POOL = "Datenspeicher"
ZFS_ELAB_PREFIX = "elabfs-"
TMP_BACKUP_FOLDER = "/mnt/Datenspeicher/snap-backup-dataset/temporary-backups"
def call(arguments, as_text=False):
""" run a command line argument
simple wrapper around subprocess.run() with some sensible defaults
:params arguments: list of command line arguments and parameters
:params as_text: should the output treated as text
:returns: bytesarray or string (if as_text is trueish)
:raises subprocess.CalledProcessError: if command has not an exit value of 0
"""
result = subprocess.run(
arguments,
check=True,
stdout=subprocess.PIPE,
universal_newlines=as_text,
)
return result.stdout
def get_member_name(filename):
#elabfs-LukasMetzler@auto-20190809.0200-1w.gz
if not filename.startswith(ZFS_ELAB_PREFIX):
raise ValueError(f"not an elabfs snapshot: {filename}")
prefix, rest = filename.split("@", 1)
return prefix.replace(ZFS_ELAB_PREFIX, "")
def copy_snapshot(filepath):
filename = filepath.name
member = get_member_name(filename)
print(f" - copying {filename}")
remote_url = (
f"{SSH_REMOTE}:~/{REMOTE_PATH}/{member}/{filename}"
)
copy_cmd = ["scp", "-i", SSH_KEY_FILE, str(filepath), remote_url]
call(copy_cmd)
def batch_copy_snapshots():
tmp_folder = pathlib.Path(TMP_BACKUP_FOLDER)
gzip_items = (i for i in tmp_folder.iterdir() if i.suffix==".gz")
for path in gzip_items:
copy_snapshot(path)
if __name__ == "__main__":
batch_copy_snapshots()