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

Preserving Last Backup of Each Year: Configuring Backup Retention #196

Closed
zN3utr4l opened this issue Mar 13, 2023 · 5 comments
Closed

Preserving Last Backup of Each Year: Configuring Backup Retention #196

zN3utr4l opened this issue Mar 13, 2023 · 5 comments
Labels
wontfix This will not be worked on

Comments

@zN3utr4l
Copy link

What are you trying to do?
I was wondering if it would be possible, through some configuration, to prevent the deletion of the last backup of each year.

For example, if it is 31st December 2022, and I perform a backup, two days later, according to my configuration, the 31st backup will be deleted, is it possible to not delete it?

For instance, by moving each backup to its own folder, such as backup/2022, backup/2023, and every time you look for the backups to delete, deleting them in the current year's folder, or for each year the backups of BACKUP_RETENTION_DAYS are deleted but starting from December 31st, that is, according to my configuration, BACKUP_RETENTION_DAYS=2, the backups of December 31st and December 30th will remain in the system.

What is your current configuration?

backup:
        image: offen/docker-volume-backup:v2.25.1
        logging:
            driver: "json-file"
            options:
                max-file: "6"
                max-size: 1024m
        user: root
        restart: always
        mem_limit: 256m
        mem_reservation: 100m
        container_name: backup
        environment:
            - BACKUP_SOURCES='/backup'
            - BACKUP_CRON_EXPRESSION='0 2-12/10 * * *'
            - BACKUP_FILENAME='backup-%Y-%m-%dT%H-%M-%S.tar.gz'
            - BACKUP_PRUNING_PREFIX='backup-'
            - BACKUP_RETENTION_DAYS=2
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock:ro
            - backup:/backup:ro
            - C:\BackupVolume:/archive
@m90
Copy link
Member

m90 commented Mar 13, 2023

This is not really possible out of the box right now. I'm also not sure if a rule engine complicated enough to cater for this case would be in scope for this project still.

What you can do however is define a docker-volume-backup.prune-pre command on the container that calls a custom script of yours that implements the logic (e.g. move files elswhere) before the default pruning mechanism runs.

@m90 m90 added the wontfix This will not be worked on label Mar 14, 2023
@zN3utr4l
Copy link
Author

i will try

@m90
Copy link
Member

m90 commented Mar 14, 2023

Closing this as a duplicate of #93 - if you have further questions, feel free to comment in there. Thanks.

@m90 m90 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2023
@zN3utr4l
Copy link
Author

If anyone asks for the solution, this is mine

pruning.sh

#!/bin/sh

folder="/archive"

for file in "$folder"/*.tar.gz; do
    created_year=$(stat -c %y "$file" | cut -d'-' -f1)

    # If the file was created in a previous year
    if [ "$created_year" -lt "$(date +%Y)" ]; then
        # Create the year folder if it doesn't exist
        mkdir "$folder/$created_year"

        # Move the file to the year folder if it's newer than the current year file
        latest_year_file=$(ls -t "$folder/$created_year/"*.tar.gz 2>/dev/null | head -n1)
        if [ "$latest_year_file" ]; then
            if [ "$file" -nt "$latest_year_file" ]; then
                mv -v "$latest_year_file" "$folder"
                mv -v "$file" "$folder/$created_year"
            fi
        else
            mv -v "$file" "$folder/$created_year"
        fi
    fi
done

Dockerfile

FROM offen/docker-volume-backup:v2.25.1

COPY pruning.sh .

RUN apk add --no-cache dos2unix && dos2unix pruning.sh && chmod +x pruning.sh

ENTRYPOINT ["/root/entrypoint.sh"]

docker-compose labels

        labels:
            - docker-volume-backup.prune-pre=sh /root/pruning.sh

@m90
Copy link
Member

m90 commented Mar 14, 2023

Thank you for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants