Hello everyone!
I had a container with a DB crap itself yesterday so I’m trying to speed up my learning to back up stuff.
I came across a script that taught me how to back-up a containerized postgres db at given intervals and it works. I managed to create db dumps and restore them. I’ve documented everything and now my whole docker-compose/env etc are on git control.
There’s one part of the script I don’t decypher but I’d like to maybe change it. It is about the number of back-up copies.
Here’s the line from the tutorial:
ls -1 /backup/*.dump | head -n -2 | xargs rm -f
Can someone explain to me what this line does? I’d like to keep maybe 3 copies just in case the auto-backup backs up a rotten one.
Thanks!
Full code below:
backup:
image: postgres:13
depends_on:
- db_recipes
volumes:
- ./backup:/backup
command: >
bash -c "while true; do
PGPASSWORD=$$POSTGRES_PASSWORD pg_dump -h db-postgresql -U $$POSTGRES_USER -Fc $$POSTGRES_DB > /backup/$$(date +%Y-%m-%d-%H-%M-%S).dump
echo ""Backup done at $$(date +%Y-%m-%d_%H:%M:%S)""
ls -1 /backup/*.dump | head -n -2 | xargs rm -f
sleep 86400
done"
A place to share alternatives to popular online services that can be self-hosted without giving up privacy or locking you into a service you don’t control.
Rules:
Be civil: we’re here to support and learn from one another. Insults won’t be tolerated. Flame wars are frowned upon.
No spam posting.
Posts have to be centered around self-hosting. There are other communities for discussing hardware or home computing. If it’s not obvious why your post topic revolves around selfhosting, please include details to make it clear.
Don’t duplicate the full text of your blog or github here. Just post the link for folks to click.
Submission headline should match the article title (don’t cherry-pick information from the title to fit your agenda).
No trolling.
Resources:
Any issues on the community? Report it using the report flag.
Questions? DM the mods!
If you want to get more in depth, I’ve been using this container:
https://github.com/jareware/docker-volume-backup
It can be setup in the same compose or in it’s own, and it supports pre/post commands if you want to dump a db or stop a container before backup.
Additionally, Setting a post backup command like in their docs:
POST_BACKUP_COMMAND: "docker run --rm -e DRY_RUN=false -e DAILY=3 -e WEEKLY=1 -e MONTHLY=1 -v /backup:/archive ghcr.io/jan-brinkmann/docker-rotate-backups"
Lets you specify the number of backups retained per period, E.G. 3 daily, 1 weekly, 1 monthly.
You could also mix and match.