import pytest from datetime import datetime testinfra_hosts = ["borg-client", "borg-client-2"] def get_server_info(hostname): if hostname == "borg-client-2": return ("backupserver", "borg-server-2", "/var/backups") return ("borg", "borg-server", "/opt/borg") compression_types = [ "none", "lz4", "zstd", "zstd,10", "zlib", "zlib,6", ] """Creates backups with all possible combinations of compression to the backup host""" @pytest.mark.parametrize("compression", compression_types) def test_backup_push(host, compression): hostname = host.backend.get_hostname() server_user, server_host, server_path = get_server_info(hostname) c = host.run( f'borg create -C "{compression}" {server_user}@{server_host}:{server_path}/{hostname}::testinfra-{{now:%S.%f}} /etc' ) assert c.rc == 0 assert c.stdout == "" assert c.stderr == "" @pytest.mark.parametrize("compression", compression_types) def test_backup_restore(host, compression): hostname = host.backend.get_hostname() server_user, server_host, server_path = get_server_info(hostname) timestamp = datetime.now().strftime("%Y%m%d-%H%M%S-%f") archive_name = f"testinfra-backup-restore-{compression}-{timestamp}" # Create backup c = host.run( f'borg create -C "{compression}" {server_user}@{server_host}:{server_path}/{hostname}::{archive_name} /etc' ) assert c.rc == 0 assert c.stdout == "" assert c.stderr == "" # Restore Backup c = host.run( f"cd /mnt && borg extract {server_user}@{server_host}:{server_path}/{hostname}::{archive_name}" ) assert c.rc == 0 assert c.stdout == "" assert c.stderr == "" # Check if every file exists, content has, and permissions / metadata c1 = host.run( 'cd /etc && find /etc -type f -printf "%P\\n" | sort | xargs -i sh -c "echo {}; sha512sum {} | cut -d \' \' -f 1; ls -l {}; echo"' ) c2 = host.run( 'cd /mnt/etc && find /etc -type f -printf "%P\\n" | sort | xargs -i sh -c "echo {}; sha512sum {} | cut -d \' \' -f 1; ls -l {}; echo"' ) assert c1.rc == 0 and c2.rc == 0 assert c1.stderr == "" and c2.stderr == "" assert c1.stdout == c2.stdout # Delete directory extract directory again for future tests c = host.run("rm -rf /mnt/etc") assert c.rc == 0 assert c.stdout == "" assert c.stderr == "" # Delete backup c = host.run( f"borg delete {server_user}@{server_host}:{server_path}/{hostname}::{archive_name}" ) assert c.rc == 0 assert c.stdout == "" assert c.stderr == "" def test_after_backup_scripts_prune_old_archives(host): hostname = host.backend.get_hostname() if hostname != "borg-client": return server_user, server_host, server_path = get_server_info(hostname) backup_script_path = "/usr/local/bin/run_borg_backup@borg-server" prune_script_path = "/usr/local/bin/run_borg_prune@borg-server" original_backup_script = host.file(backup_script_path).content_string original_prune_script = host.file(prune_script_path).content_string prefix = f"testprune-{datetime.now().strftime('%Y%m%d%H%M%S%f')}" modified_backup_script = original_backup_script.replace( "::{hostname}-{now:%Y-%m-%dT%H:%M:%S}", f"::{prefix}-${{BORG_TEST_ITERATION}}-{{now:%Y-%m-%dT%H:%M:%S}}", ) modified_prune_script = original_prune_script.replace( "{hostname}-*", f"{prefix}-*", ) rewrite = host.run( "python3 - <<'PY'\n" "from pathlib import Path\n" f"Path({backup_script_path!r}).write_text({modified_backup_script!r})\n" f"Path({prune_script_path!r}).write_text({modified_prune_script!r})\n" "PY" ) assert rewrite.rc == 0 assert host.run(f"bash -n {backup_script_path}").rc == 0 assert host.run(f"bash -n {prune_script_path}").rc == 0 try: for iteration in range(4): backup = host.run(f"BORG_TEST_ITERATION={iteration} {backup_script_path}") assert backup.rc == 0 prune = host.run(prune_script_path) assert prune.rc == 0 archives = host.run( f"borg list {server_user}@{server_host}:{server_path}/{hostname} --glob-archives '{prefix}-*'" ) assert archives.rc == 0 archive_lines = [line for line in archives.stdout.splitlines() if line.strip()] assert len(archive_lines) == 2 finally: restore = host.run( "python3 - <<'PY'\n" "from pathlib import Path\n" f"Path({backup_script_path!r}).write_text({original_backup_script!r})\n" f"Path({prune_script_path!r}).write_text({original_prune_script!r})\n" "PY" ) assert restore.rc == 0 def test_manual_prune_script_prunes_old_archives(host): hostname = host.backend.get_hostname() if hostname != "borg-client-2": return server_user, server_host, server_path = get_server_info(hostname) prefix = f"testprune-timer-{datetime.now().strftime('%Y%m%d%H%M%S%f')}" script_path = "/usr/local/bin/run_borg_prune@borg-server-2" for iteration in range(4): create = host.run( f"borg create -C lz4 {server_user}@{server_host}:{server_path}/{hostname}::{prefix}-{iteration}-{{now:%Y-%m-%dT%H:%M:%S}} /etc" ) assert create.rc == 0 original_script = host.file(script_path).content_string assert "--keep-daily 7" in original_script assert "--keep-weekly 4" in original_script modified_script = ( original_script.replace("{hostname}-*", f"{prefix}-*") .replace("--keep-daily 7", "--keep-last 2") .replace("--keep-weekly 4", "") ) assert "--keep-last 2" in modified_script assert "--keep-daily 7" not in modified_script assert "--keep-weekly 4" not in modified_script rewrite = host.run( "python3 - <<'PY'\n" "from pathlib import Path\n" f"Path({script_path!r}).write_text({modified_script!r})\n" "PY" ) assert rewrite.rc == 0 syntax_check = host.run(f"bash -n {script_path}") assert syntax_check.rc == 0 try: result = host.run(script_path) assert result.rc == 0 archives = host.run( f"borg list {server_user}@{server_host}:{server_path}/{hostname} --glob-archives '{prefix}-*'" ) assert archives.rc == 0 archive_lines = [line for line in archives.stdout.splitlines() if line.strip()] assert len(archive_lines) == 2 finally: restore = host.run( "python3 - <<'PY'\n" "from pathlib import Path\n" f"Path({script_path!r}).write_text({original_script!r})\n" "PY" ) assert restore.rc == 0 cleanup = host.run( f"borg delete --glob-archives '{prefix}-*' {server_user}@{server_host}:{server_path}/{hostname}" ) assert cleanup.rc == 0