From 76825000f77cf8f30a3d0a7135bd45d5f90cb0e0 Mon Sep 17 00:00:00 2001 From: Emmanuel Nyachoke Date: Tue, 5 Nov 2024 13:12:46 +0000 Subject: [PATCH] Switch to croniter for cron validation --- .gitignore | 2 +- docker-compose-backup-service.yaml | 2 -- ...pose-restore-test.yaml => docker-compose-live-two.yaml | 8 ++++---- restic_compose_backup.env | 2 +- restore-backup.sh | 2 -- src/Dockerfile | 1 - src/crontab | 2 -- src/restic_compose_backup/config.py | 2 +- src/restic_compose_backup/cron.py | 3 ++- src/setup.py | 1 + start-live-system.sh | 4 ++-- start-live-two-with-restore.sh | 2 ++ stop-backup-system.sh | 1 - stop-live-two.sh | 1 + stop-live-system.sh => stop-live.sh | 0 15 files changed, 15 insertions(+), 18 deletions(-) rename docker-compose-restore-test.yaml => docker-compose-live-two.yaml (88%) delete mode 100755 restore-backup.sh delete mode 100755 src/crontab create mode 100755 start-live-two-with-restore.sh delete mode 100755 stop-backup-system.sh create mode 100755 stop-live-two.sh rename stop-live-system.sh => stop-live.sh (100%) diff --git a/.gitignore b/.gitignore index eac56f4..032fd5e 100755 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,7 @@ venv /private/ restic_data/ restic_cache/ -restored_filestores +live_two_filestores alerts.env # build diff --git a/docker-compose-backup-service.yaml b/docker-compose-backup-service.yaml index 7904748..5e03e68 100755 --- a/docker-compose-backup-service.yaml +++ b/docker-compose-backup-service.yaml @@ -14,8 +14,6 @@ services: - ./restic_data:/restic_data # Map restic cache - ./restic_cache:/cache - # Map in project source in dev - - ./src:/restic-compose-backup networks: global: external: true \ No newline at end of file diff --git a/docker-compose-restore-test.yaml b/docker-compose-live-two.yaml similarity index 88% rename from docker-compose-restore-test.yaml rename to docker-compose-live-two.yaml index 9aedfa9..3a8bd93 100755 --- a/docker-compose-restore-test.yaml +++ b/docker-compose-live-two.yaml @@ -7,8 +7,8 @@ services: restic-compose-backup.volumes: true restic-compose-backup.volumes.include: "/tests" volumes: - - ./restored_filestores/src/tests:/srv/tests - - ./restored_filestores/.vscode:/srv/code + - ./live_two_filestores/src/tests:/srv/tests + - ./live_two_filestores/.vscode:/srv/code environment: - SOME_VALUE=test - ANOTHER_VALUE=1 @@ -24,8 +24,8 @@ services: restic-compose-backup.volumes: true restic-compose-backup.volumes.exclude: "/tests" volumes: - - ./restored_filestores/src/tests:/srv/tests - - ./restored_filestores/.vscode:/srv/code + - ./live_two_filestores/src/tests:/srv/tests + - ./live_two_filestores/.vscode:/srv/code environment: - SOME_VALUE=test - ANOTHER_VALUE=1 diff --git a/restic_compose_backup.env b/restic_compose_backup.env index a45c092..7790aba 100755 --- a/restic_compose_backup.env +++ b/restic_compose_backup.env @@ -18,7 +18,7 @@ RESTIC_KEEP_MONTHLY=12 RESTIC_KEEP_YEARLY=3 LOG_LEVEL=info -CRON_SCHEDULE=0,5,10,15,20,25,30,35,40,45,50,55 * * * * +CRON_SCHEDULE=*/5 * * * * # EMAIL_HOST= # EMAIL_PORT= diff --git a/restore-backup.sh b/restore-backup.sh deleted file mode 100755 index 4bb3ef2..0000000 --- a/restore-backup.sh +++ /dev/null @@ -1,2 +0,0 @@ -docker compose -p restore-test -f docker-compose-restore-test.yaml -f docker-compose-restore-service.yaml down -v -docker compose -p restore-test -f docker-compose-restore-test.yaml -f docker-compose-restore-service.yaml up --build --force-recreate -d \ No newline at end of file diff --git a/src/Dockerfile b/src/Dockerfile index 629cbb8..a25a048 100755 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -8,7 +8,6 @@ RUN apk update && apk add python3 \ py3-pip \ bash RUN python3 -m pip install --no-cache-dir pipx --break-system-packages - RUN python3 -m pipx ensurepath --global ADD . /restic-compose-backup WORKDIR /restic-compose-backup diff --git a/src/crontab b/src/crontab deleted file mode 100755 index 6bd361a..0000000 --- a/src/crontab +++ /dev/null @@ -1,2 +0,0 @@ -*/5 * * * * source /env.sh && rcb backup > /proc/1/fd/1 - diff --git a/src/restic_compose_backup/config.py b/src/restic_compose_backup/config.py index 114e88c..b452b4b 100755 --- a/src/restic_compose_backup/config.py +++ b/src/restic_compose_backup/config.py @@ -3,7 +3,7 @@ class Config: default_backup_command = "source /env.sh && rcb backup > /proc/1/fd/1" - default_crontab_schedule = "*/5 * * * *" + default_crontab_schedule = "0 2 * * *" """Bag for config values""" def __init__(self, check=True): diff --git a/src/restic_compose_backup/cron.py b/src/restic_compose_backup/cron.py index cf520c6..f69dc22 100755 --- a/src/restic_compose_backup/cron.py +++ b/src/restic_compose_backup/cron.py @@ -1,3 +1,4 @@ +from croniter import croniter """ # ┌───────────── minute (0 - 59) # │ ┌───────────── hour (0 - 23) @@ -20,7 +21,7 @@ def generate_crontab(config): if schedule: schedule = schedule.strip() schedule = strip_quotes(schedule) - if not validate_schedule(schedule): + if not croniter.is_valid(schedule): schedule = config.default_crontab_schedule else: schedule = config.default_crontab_schedule diff --git a/src/setup.py b/src/setup.py index ddd8799..63c164e 100755 --- a/src/setup.py +++ b/src/setup.py @@ -12,6 +12,7 @@ ]), install_requires=[ 'docker~=7.1.0', + 'croniter~=5.0.1' ], entry_points={'console_scripts': [ 'restic-compose-backup = restic_compose_backup.cli:main', diff --git a/start-live-system.sh b/start-live-system.sh index d0fa889..534c03d 100755 --- a/start-live-system.sh +++ b/start-live-system.sh @@ -1,2 +1,2 @@ -docker compose -p live-system -f docker-compose-backup-service.yaml -f docker-compose-live.yaml down -v -docker compose -p live-system -f docker-compose-backup-service.yaml -f docker-compose-live.yaml up --build --force-recreate -d \ No newline at end of file +docker compose -p live -f docker-compose-backup-service.yaml -f docker-compose-live.yaml down -v +docker compose -p live -f docker-compose-backup-service.yaml -f docker-compose-live.yaml up --build --force-recreate -d \ No newline at end of file diff --git a/start-live-two-with-restore.sh b/start-live-two-with-restore.sh new file mode 100755 index 0000000..82cb0ab --- /dev/null +++ b/start-live-two-with-restore.sh @@ -0,0 +1,2 @@ +docker compose -p live-two -f docker-compose-live-two.yaml -f docker-compose-restore-service.yaml down -v +docker compose -p live-two -f docker-compose-live-two.yaml -f docker-compose-restore-service.yaml up --build --force-recreate -d \ No newline at end of file diff --git a/stop-backup-system.sh b/stop-backup-system.sh deleted file mode 100755 index 77e04d9..0000000 --- a/stop-backup-system.sh +++ /dev/null @@ -1 +0,0 @@ -docker compose -p restore-test -f docker-compose-restore-test.yaml -f docker-compose-restore-service.yaml down \ No newline at end of file diff --git a/stop-live-two.sh b/stop-live-two.sh new file mode 100755 index 0000000..84af649 --- /dev/null +++ b/stop-live-two.sh @@ -0,0 +1 @@ +docker compose -p restore-test -f docker-compose-live-two.yaml -f docker-compose-restore-service.yaml down \ No newline at end of file diff --git a/stop-live-system.sh b/stop-live.sh similarity index 100% rename from stop-live-system.sh rename to stop-live.sh