aboutsummaryrefslogtreecommitdiffstats
path: root/molecule/default/tests/test_manual_backup.py
diff options
context:
space:
mode:
Diffstat (limited to 'molecule/default/tests/test_manual_backup.py')
-rw-r--r--molecule/default/tests/test_manual_backup.py122
1 files changed, 122 insertions, 0 deletions
diff --git a/molecule/default/tests/test_manual_backup.py b/molecule/default/tests/test_manual_backup.py
index 8cd044c..ea48655 100644
--- a/molecule/default/tests/test_manual_backup.py
+++ b/molecule/default/tests/test_manual_backup.py
@@ -84,3 +84,125 @@ def test_backup_restore(host, compression):
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