Archiving and Backing Up Before You Touch Anything
Before you edit any config file that matters, make a backup of it first - not because you're bad at editing files, but because when something goes wrong later, the two things people say most often are "I was sure that change was right" and "and there was no way to undo it."
tar, the archive format you'll see everywhere
tar -czf backup.tar.gz path/to/directory # create (c), through gzip compression (z), to this file (f)
tar -tzf backup.tar.gz # list (t) what's inside, through gzip (z), from this file (f), without extracting anything
tar -xzf backup.tar.gz # extract (x), through gzip (z), from this file (f)
Each flag is one letter with one job, and you read them left to right like a
short sentence: c create, z use gzip compression, f here's the file
name. To list what's inside instead of creating an archive, swap c for t.
To pull the files back out, swap c for x. The z and f stay the same
every time.
The quick alternative
For a single file or a quick "just in case," a plain copy with a timestamp is
enough and doesn't need tar at all:
cp -a settings.ini settings.ini.bak-$(date +%F)
-a ("archive") preserves permissions, ownership, and timestamps instead of
just the file's bytes - the right default whenever what you're copying might
get restored later and needs to behave identically to the original.
Back up before you break anything
~/app-config/ holds a real config and a cert directory. Before touching
either, back the whole thing up:
mkdir -p ~/backups
tar -czf ~/backups/app-config-backup.tar.gz -C ~/app-config .
-C ~/app-config . tells tar to change into that directory first, so the
archive contains settings.ini and certs/site.pem directly, not a chain of
parent directories you'd have to strip out later. Check your work before you
trust it - that's what -t is for:
tar -tzf ~/backups/app-config-backup.tar.gz
./
./settings.ini
./certs/
./certs/site.pem
Both real files are in there (settings.ini and certs/site.pem), each
listed with the exact relative path it'll be restored to. If you'd forgotten
the -C ~/app-config and archived from your home directory instead, this
list would show something like app-config/settings.ini instead - a small
difference, but it changes where tar -xzf would put the files back on
restore. Always run -tzf once, right after creating an archive, before you
trust it's actually correct.
The archive is missing certs/site.pem
Check what directory you were in when you ran tar -czf. If you archived just
settings.ini on its own instead of the whole ~/app-config directory, the
cert file never made it in.
- Previous lesson
- Files and Directories Without Regret
- Next lesson
- Editing Configs Over SSH