Sub topic 17
Capture a snapshot or backup of the clean baseline
This chapter assumes you have finished the baseline from the earlier chapters: an updated system, an admin user with sudo, key-based SSH, a firewall, correct time, and a passing verification checklist. It also assumes you can read df -h and lsblk output and know what a filesystem is. The goal is to freeze that known-good state so you can return to it later.
Why capture the baseline
A fresh, verified server is the cleanest state it will ever be. Every later change adds risk. If an upgrade fails or a service is misconfigured, you want a way back that takes minutes instead of a reinstall.
Capturing the baseline gives you three things:
- A rollback point for risky changes such as kernel upgrades or major configuration edits.
- A template for building more servers that start from the same state.
- A reference for comparing a damaged system against what it looked like when it was healthy.
Do it after verification, not before. A snapshot of a half-configured server preserves its mistakes.
Snapshot versus backup
The two words are often mixed up, but they protect against different failures.
| Property | Snapshot | Backup |
|---|---|---|
| What it records | The state of a disk or volume at one moment | A copy of selected files or the whole disk |
| Where it lives | Usually on the same storage system as the original | On separate storage, ideally another location |
| Speed to create | Seconds, since most systems record only later changes | Minutes to hours, depending on data size |
| Survives loss of the disk or host | Usually not | Yes, if stored elsewhere |
| Best use | Quick rollback before a change | Recovery from disaster or deletion |
A snapshot is not a backup. If the storage fails, the snapshot fails with it. For a baseline, the safest approach is to take a snapshot for fast rollback and also keep one copy elsewhere.
Choose a method for your platform
The right tool depends on where the server runs.
- Virtual machine on a hypervisor (VirtualBox, KVM, VMware, Proxmox): use the hypervisor's snapshot feature. It captures disk and optionally memory state.
- Cloud instance (AWS, Azure, Google Cloud, and similar): create a machine image or a volume snapshot through the provider.
- Physical machine or any host without platform support: use a filesystem snapshot (LVM or Btrfs) or an image or file-level backup taken with standard tools.
Prepare the system before capturing
Some preparation makes the captured state more useful and more consistent.
- Run the verification checklist from the previous chapter and fix anything that fails.
- Clear the package cache so the image is smaller. On Debian and Ubuntu run
sudo apt clean. On the RHEL family runsudo dnf clean all. - Decide whether the capture will be used as a template for other servers. If so, you must remove identity that should be unique per machine (see the template section below).
- Note the date, the release, and the kernel version in a short text file so you know what the snapshot contains.
# Record what this baseline contains
{
echo "Baseline captured: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "Hostname: $(hostname)"
echo "Kernel: $(uname -r)"
grep PRETTY_NAME /etc/os-release
} | sudo tee /root/baseline-info.txtTake an LVM snapshot
If your root filesystem sits on an LVM logical volume (a common installer choice from the partitioning chapter), you can take a copy-on-write snapshot. Copy-on-write means the snapshot stores only blocks that change after it is created, so it starts small but grows as the original changes.
First, find the volume group and logical volume names, and check free space in the volume group.
sudo vgs
sudo lvsThe VFree column from vgs shows the space available for snapshot storage. Then create the snapshot. The names below are examples. Replace vg0 and root with your own.
# Reserve 5 GiB to hold changes made after the snapshot
sudo lvcreate --size 5G --snapshot --name root-baseline /dev/vg0/root
sudo lvsIf the snapshot's space fills up, LVM invalidates it, and it becomes useless. Watch the Data% column in lvs output. Remove snapshots you no longer need.
To roll back, merge the snapshot into the origin volume. For a mounted root volume, the merge completes at the next reboot.
sudo lvconvert --merge /dev/vg0/root-baseline
sudo rebootA merge discards everything changed since the snapshot. That includes logs, new users, and data. Be sure that is what you want.
Take a Btrfs snapshot
If you chose Btrfs, snapshots are built in and cheap. A snapshot is a subvolume that shares data with the original.
sudo mkdir -p /.snapshots
sudo btrfs subvolume snapshot -r / /.snapshots/baseline
sudo btrfs subvolume list /The -r flag makes the snapshot read-only, which prevents accidental changes to your recovery point. Rolling back on Btrfs involves changing the default subvolume or copying files from the snapshot, and the exact steps depend on the layout your installer created. Test your rollback procedure on a throwaway machine before you rely on it.
Take a file-level backup with rsync
When no snapshot tooling exists, copy the important files. This does not capture a bootable disk, but it records your configuration, which is what you most often need.
sudo rsync -aAXH --numeric-ids \
--exclude='/proc/*' --exclude='/sys/*' --exclude='/dev/*' \
--exclude='/run/*' --exclude='/tmp/*' --exclude='/mnt/*' \
--exclude='/media/*' --exclude='/lost+found' \
/ [email protected]:/srv/backups/web01-baseline/The flags matter. -a preserves permissions, ownership, and timestamps. -A keeps ACLs, -X keeps extended attributes, and -H keeps hard links. The exclusions skip virtual filesystems, which contain no real files and would cause errors or huge transfers.
If you only want configuration, a smaller archive is enough:
sudo tar --xattrs --acls -czpf /tmp/etc-baseline.tar.gz /etc /root/baseline-info.txt
sha256sum /tmp/etc-baseline.tar.gzCopy the archive off the machine with scp, then compare its checksum on the destination, using the same method you used for the installation image.
Make a bootable template (optional)
If you will clone this baseline to create more servers, remove per-machine identity first. Otherwise every clone has the same machine ID, the same SSH host keys, and possibly the same DHCP lease identity.
sudo truncate -s 0 /etc/machine-id
sudo rm -f /etc/ssh/ssh_host_*
sudo journalctl --rotate && sudo journalctl --vacuum-time=1s
history -cOn Debian and Ubuntu, SSH host keys are not regenerated automatically on next boot unless something triggers it. Run sudo dpkg-reconfigure openssh-server on each clone, or use cloud-init, which handles this for you on supported images. Do this on the template only just before shutting it down, because removing host keys breaks SSH on the running machine after its next restart. Cloud providers often provide an image-creation process that performs similar cleanup.
Test the restore
A capture you have never restored is an assumption. Test at least once.
- Create a throwaway clone from the snapshot, image, or backup, on a separate VM or instance.
- Boot it and log in with your SSH key.
- Run the verification checklist from the previous chapter.
- Delete the clone.
If the test fails, you have found the problem while it is cheap to fix.
Retention and housekeeping
- Keep the baseline snapshot until you have a newer verified baseline. Do not let it grow stale forever. After major release upgrades, capture a new one.
- Delete temporary LVM snapshots promptly with
sudo lvremove /dev/vg0/root-baseline, since they slow writes on the origin volume. - Store at least one copy in a separate location and protect it. A backup of a server contains its configuration and possibly secrets, so restrict access to it.
- Never put private SSH keys from your workstation inside the server image.
Exercise
You created an LVM snapshot of the root volume with 1 GiB of space. Two weeks later you want to roll back, but lvs shows the snapshot as invalid. What most likely happened, and what should you have done?
Show answer
The snapshot's 1 GiB of copy-on-write space filled up as the origin volume changed, so LVM invalidated it. You should have sized the snapshot for the expected changes, monitored the Data% column in lvs, and kept a separate off-host copy. A snapshot also should not be left in place for weeks, because it is meant for short-term rollback.
Exercise
You plan to clone a baseline VM into five new servers. Name two items you must reset on the template first, and explain why.
Show answer
Reset /etc/machine-id and the SSH host keys (/etc/ssh/sshhost*). If they are kept, all clones share the same machine identity and host keys. That confuses tools that rely on unique machine IDs, and it lets anyone who obtains one server's host key impersonate the others.