Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add behaviour to remove older backups #4038

Merged
merged 5 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# CHANGELOG
## Release 124.1.5 (in development)

### Enhancements

- Add cleanup functionality to the backup replication job to
only keep the latest 5 backup archives
(PR[#4038](https://github.com/scality/metalk8s/pull/4038))

### Bug fixes

- Make metalk8s-sosreport packages compatible with sos version 4.5
Expand All @@ -11,6 +17,9 @@
regenerated
(PR[#4035](https://github.com/scality/metalk8s/pull/4035))

- Fix a bug that skips backup archive replication
(PR[#4038](https://github.com/scality/metalk8s/pull/4038))

## Release 124.1.4

### Bug fixes
Expand Down
18 changes: 9 additions & 9 deletions salt/metalk8s/orchestrate/backup/files/job.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ spec:
containers:
- name: backup-replication
image: {{ image }}
command:
- wget
- --accept="*.tar.gz"
- --no-host-directories
- --mirror
- --ca-certificate=/certificate/ca.crt
- --user=$(BACKUP_USERNAME)
- --password=$(BACKUP_PASSWORD)
- https://backup/
command: ["/bin/sh", "-c"]
args:
- >-
wget --accept="*.tar.gz"
--no-host-directories --mirror
--ca-certificate=/certificate/ca.crt
--user=$(BACKUP_USERNAME)
--password=$(BACKUP_PASSWORD)
https://backup/; ls -t -1 /backups/*.tar.gz | tail -n+6 | xargs -t rm -f
eg-ayoub marked this conversation as resolved.
Show resolved Hide resolved
env:
- name: BACKUP_USERNAME
valueFrom:
Expand Down
5 changes: 5 additions & 0 deletions tests/post/features/backup.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@post @ci @local @backup
Feature: Backup
Scenario: Backup multiple times
When we run the backup script 6 times
Then we have 5 backups on each node
eg-ayoub marked this conversation as resolved.
Show resolved Hide resolved
62 changes: 62 additions & 0 deletions tests/post/steps/test_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import yaml

from pytest_bdd import scenario, when, then, parsers

from tests import utils

# Scenarios
@scenario("../features/backup.feature", "Backup multiple times")
def test_backup(host):
pass


# When
@when(parsers.parse("we run the backup script {times} times"))
def run_backup(request, host, times):
iso_root = request.config.getoption("--iso-root")

for _ in range(int(times)):
with host.sudo():
res = host.run("%s/backup.sh", str(iso_root))
assert res.rc == 0, res.stdout


# Then
@then(parsers.parse("we have {count} backups on each node"))
eg-ayoub marked this conversation as resolved.
Show resolved Hide resolved
def check_backup_archive_count(host, version, ssh_config, count):
# using yaml becasue json output is not always correctly formatted
eg-ayoub marked this conversation as resolved.
Show resolved Hide resolved
get_master_nodes_command = [
"salt-run",
"--out yaml",
"salt.cmd",
"metalk8s.minions_by_role",
"master",
"with_pillar=True",
]
eg-ayoub marked this conversation as resolved.
Show resolved Hide resolved
master_nodes = yaml.load(
utils.run_salt_command(host, get_master_nodes_command, ssh_config).stdout,
Loader=yaml.Loader,
eg-ayoub marked this conversation as resolved.
Show resolved Hide resolved
)
assert len(master_nodes) > 0, "No master node found"

if len(master_nodes) == 1:
master_nodes = master_nodes[0]
else:
master_nodes = f"-L {','.join(master_nodes)}"
eg-ayoub marked this conversation as resolved.
Show resolved Hide resolved

command = [
"salt",
"--out yaml",
eg-ayoub marked this conversation as resolved.
Show resolved Hide resolved
master_nodes,
"cmd.run_all",
'"find /var/lib/metalk8s/backups -name "*.tar.gz" | wc -l"',
eg-ayoub marked this conversation as resolved.
Show resolved Hide resolved
]
backup_counts = yaml.load(
utils.run_salt_command(host, command, ssh_config).stdout, Loader=yaml.Loader
)

for name, ret in backup_counts.items():
assert ret["retcode"] == 0, ret["stderr"]
assert int(ret["stdout"]) == int(count), (
f"Expected {count} backup archives on " f'node {name}, got {ret["stdout"]}'
)