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.
Ah! This is a shell pipe! It's composing several smaller commands together, cool stuff.
ls -1 is the grep-friendly version of ls, it prints one entry per line, like a shopping list.
head takes a set number of entries from the head of a list, in this case 2 items. negative two, meaning "all but the last two."
xargs takes the incoming pipe and converts it into extra arguments, in this case applying those arguments to rm.
So, combined, this says "list all the .dump files, pick the first two, all but the last two, and delete them." Presumably the first are the oldest ones and the last are the newest, if the .dump files are named chronologically.
So, combined, this says "list all the .dump files, pick the first two, and delete them."
No, no.
It's "list all the .dump files, pick all of them except two at the bottom, and delete them", where listing is done chronologically by default :).
Otherwise you'll delete 2 archives every time you create one and eventually there'll be none.