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"
doeknius_gloek@feddit.de 1 year ago
This line seems to list all dumps and then deletes the two oldest ones. It will also remove your most recent backup if the folder contains less than three files, so be careful.
In detail:
ls -1 /backup/*.dump
lists all files ending with .dump alphabetically inside the /backup directoryhead -n -2
returns the two filenames from the top of the list (this should also behead -n 2
orhead -2
, nothead -n -2
)xargs rm -f
passes the two filenames torm -f
to delete themTake a look at explainshell.com.
klay@lemmy.world 1 year ago
I just looked up the man page, and actually
head -n -2
means “everything up to but not including the last two lines”, so this should always leave two files remaining.doeknius_gloek@feddit.de 1 year ago
You’re right, I edited my comment. Thanks!