• All I Wanted Was a Backup

    A sunlit brick house with open curtained windows, flower boxes, and a lit lamp inside rests on steel beams and timber cribbing above a new concrete slab.

    Backing up one small server sounds like a twenty-minute job. I had a second machine with plenty of free space, SSH already worked between them, and my muscle memory went straight for rsync. I was already worrying about the trailing slash before I had even stopped to ask what I was going to copy.

    The server runs a small Kubernetes cluster that hosts this blog and its photos, my site analytics, the CouchDB instance behind my Obsidian notes, a Telegram bot who thinks he is a pirate pig, and a few smaller services. The cluster even had a make backup command already. If the server died, I could restore Kubernetes from that backup and it would remember every deployment, every volume claim, and the exact directory where PostgreSQL was supposed to live. The directory itself would be empty. That was the part I actually wanted to save.

    Across all those services, the data lived in nine persistent volumes. Down on the host, they were just nine ordinary directories on the system disk. Pointing rsync at them while everything was running would copy files one after another while PostgreSQL was still writing to them. By the time rsync reached the last file in the directory, the first one could already belong to an earlier transaction, leaving me with a backup that looked complete until the day I tried to boot a database from it. Stopping everything before each backup would fix that, of course, at the small cost of taking my notes, my blog’s analytics, and the group chat’s bot offline every night for as long as the network transfer took.

    Knowing Too Much About Every App

    So I started doing it the conscientious way, one application at a time. PostgreSQL knows how to produce a consistent dump, and I had two of them to handle, one for analytics and one for the bot, plus two more volumes holding their older dump history that I wanted to keep as well. CouchDB has its own rules for copying a live database safely. The blog’s photo store had an active writer process, and the smaller services each had their own caches and state files on disk.

    None of those problems was impossible on its own. Each one just wanted its own backup procedure and its own locking rules, and each would quietly break the day an upgrade changed how it wrote to disk. The moment the whole approach fell apart for me was when I caught myself designing a check to run Obsidian on my Mac just to confirm that CouchDB’s synced notes were healthy. Somewhere along the way, a nightly backup of a Linux server had hired my laptop for the night shift.

    That was when I stepped back and separated two jobs I had tangled together. Verifying that a restore works requires knowing what lives inside the data. Taking the nightly backup does not. If I upgrade CouchDB next month, I should not have to rewrite a backup script to match. At two in the morning, I wanted something much dumber and much cleaner: a block-level snapshot of a disk. Freeze the disk at one instant, let the second machine stream that frozen image over SSH at whatever pace the network allows, and let every application keep writing to the live system in the meantime.

    There was only one catch. I did not have a disk I could snapshot.

    Building a Disk Inside a Nearly Full Disk

    I checked the host’s storage layout hoping for an unused partition or at least an LVM volume group I had forgotten about. The server had one disk, one plain ext4 root partition, zero unpartitioned space, and no LVM at all.

    Putting a new volume beside the old data ran straight into arithmetic. The nine volume directories already used about 14 GiB. The root filesystem had around 18 GiB free, and I wanted to keep at least 10 GiB untouched so the operating system and Kubernetes would never run short of breathing room. That left nowhere near enough space to allocate a second full-sized home for everything in one go.

    What I could do was treat the server like a sliding-tile puzzle. I did not have room for all nine volumes at once, but I did have room for one application. Move its data onto a small new volume, prove it works there, delete the old files, and spend the reclaimed space to grow the new volume for the next application. Repeat until all nine directories live on the same snapshot-capable filesystem.

    To pull that off without repartitioning the host or rebooting it, I started by allocating a single 4.5 GiB file on the system disk. Linux can attach a regular file to a loop device and treat it like a physical disk. Inside that loop device, I created an LVM volume group, carved out a 4.25 GiB logical volume, and formatted it with ext4.

    Each layer in that stack had a specific job. LVM was the destination: it gives you copy-on-write snapshots. Putting LVM inside a file was what made the migration incremental. Whenever I freed space on the host, I could extend the backing file, tell the loop device and LVM to pick up the new capacity, and grow ext4 online while databases were still actively reading and writing on it. I did not have to reboot the server, take the cluster down, or guess the final disk size up front.

    Moving Live Volumes Under Kubernetes’s Feet

    Analytics went first. Its PostgreSQL database and dump history lived on two persistent volumes and easily fit inside the 4.25 GiB starter volume. Being small also made analytics a safe place to rehearse the two-pass move I would need later for multi-gigabyte services.

    This was where rsync finally earned its keep, as a warm-up rather than a backup tool. The first pass ran while analytics was still online and counting visits. That copy was never meant to boot a database; its only job was to land most of the bytes on the new volume ahead of time so the actual cutover would stay brief.

    Once the warm pass finished, I paused the scheduled analytics jobs, stopped the pods using those two volumes, and ran rsync a second time. With PostgreSQL quiet, that second pass caught up on every change and removed anything deleted, and a checksum verification pass confirmed that both sides matched byte for byte, including file attributes. Before switching over, I archived the old directories and shipped an encrypted safety copy off the server, so I would not be deleting anything later without a fallback. Meanwhile, the blog kept serving photos and my friends could still talk to the pig. Moving one service gave me no reason to disturb the rest of the cluster.

    The neatest trick was avoiding Kubernetes changes altogether. Kubernetes already knew which host paths belonged to those volume claims. Rather than creating new volumes and rewriting manifests, I bind-mounted each migrated directory right on top of its original host path. When I started analytics back up, the pods came online with the exact same PersistentVolumeClaims and container paths, now silently pointing at the new LVM volume.

    From inside the running pod, I confirmed PostgreSQL was hitting the new device, wrote a test value, and read it back. Only then came the cleanup, which comes with a classic Linux gotcha: bind-mounting a new directory over an old one hides the old files from view, but it does not free a single byte of disk space. To actually get that space back, I had mounted a private view of the old storage tree off to the side beforehand. Reaching in through that back door let me empty the old directories underneath the live mount without touching the running database.

    Kubernetes pod
      -> original host path (bind-mounted)
           -> migrated directory on ext4
                -> LVM volume group (data LV + snapshot space)
                     -> loop device
                          -> growable file on the system disk
    
    Private side mount (host only)
      -> original host directory underneath the bind mount
           -> old files, emptied after verification

    The reclaimed space went straight back to the host’s root filesystem, ready to pay for the next expansion. When the next service needed more room, I extended the backing file, grew the loop device and LVM volume, and resized ext4 right under analytics’ feet. It kept recording visits while its own floor got wider.

    Leaving empty host directories underneath bind mounts does create one subtle trap for the future. After a reboot, those empty directories are still sitting there. If Kubernetes boots before the loop device, LVM volume, and bind mounts are restored, every application will start up cleanly, find its expected paths in the exact right places, and conclude that it is a brand-new empty installation. So I wired systemd to bring up the storage stack before Kubernetes is allowed to start, and to block Kubernetes from booting at all if mounting fails. Better a failed start than a cluster cheerfully initializing empty databases.

    Where the Neat Procedure Bent

    On paper, the remaining seven volumes were just more turns of the analytics loop. CouchDB was the first to break the pattern.

    The database moved cleanly onto the new volume, opened all my existing databases, and passed a write-and-read-back test. None of that told me what I actually cared about, though: whether my notes still synced. So I triggered a full sync check from Obsidian’s LiveSync plugin on my Mac, with a hard two-minute timeout. Two minutes later, the timer ran out before the sync finished.

    That was not an error from CouchDB; it was just an inconclusive check. Still, it left me in an awkward spot. CouchDB was already live on the new volume, so switching back to the old directory risked losing an edit my phone might have synced in the meantime. Deleting the old directory before a sync actually passed meant throwing away my safety net blind.

    Nothing required me to settle that on the spot. The database was already up, and the only thing waiting on the old directory’s space was the next step of my own migration. So I left both copies in place and let Obsidian’s normal background sync resume and catch up at its own pace. When I ran the verification check again later, the round trip finished cleanly inside the window. I emptied the old directory without stopping CouchDB or copying its files a second time.

    The next few volumes were refreshingly uneventful: a couple of small service directories and the blog’s photo store. Checking the photos was my favorite verification of the whole project. Before the move, I fetched an image straight from this blog over HTTPS; after the move, I fetched the same URL, compared the bytes, and checked that the uploader could still write to the new volume.

    Then came Swinobot, and that same safety habit ran into scale. Between his PostgreSQL database and a pile of accumulated dumps, even the compressed archive came to more than 8 GiB. For analytics, creating and uploading that off-server safety copy during the maintenance pause had taken seconds. Doing the same thing here would have left a pig in a pirate hat offline in the group chat while eight gigabytes compressed and crawled across the network.

    That was when I realized I had two steps in the wrong order. Stopping the bot was only necessary for the final local rsync between volumes on the same server, which took almost no time. Once that local copy passed checksum verification and the bind mount was in place, I brought Swinobot straight back up on the new volume. Because the bot was now writing only to the new filesystem, the old directories underneath the mount were completely frozen. I could take as long as I wanted to archive and upload those eight gigabytes from under the mount, and only delete the old files once the upload finished, while Swinobot was already back in chat.

    Once the last of the remaining volumes was across, all nine original volume claims led to the new filesystem. The starter 4.25 GiB volume had grown in stages to 16.25 GiB. The server had never rebooted, Kubernetes had never restarted, and every application had spent almost its entire migration doing its normal job.

    Two in the Morning

    Before the first nightly backup could run, I grew the backing file one more time while keeping the ext4 filesystem at 16.25 GiB. That left the extra capacity inside LVM purely as a buffer for copy-on-write snapshots, giving LVM room to stash the original contents of any blocks modified during a transfer without touching the host’s safety reserve.

    At two every morning, the second machine connects over SSH and asks LVM for a read-only snapshot of that volume. The server compresses the snapshot as it reads it; the backup machine pulls that stream and encrypts it as it arrives. Meanwhile, every application on the server keeps writing as usual. Whenever a service modifies a block on the live filesystem, LVM copies the block’s old contents aside for the snapshot reader first. That way, the last block to arrive over the wire belongs to the exact same instant as the first. From the filesystem’s point of view, the resulting image looks like someone yanked the power cord at 2:00 AM and handed the disk to the backup machine, ext4 journal and all.

    That is a crash-consistent backup, and it is worth being honest about what it does and does not promise. A single PostgreSQL instance whose data and write-ahead log live on the same volume knows how to recover from a sudden power cut by replaying its log on startup. What a disk snapshot does not give you is transactional coordination across separate services, or automatic proof that every application boots cleanly from the restored bytes without testing it.

    None of the nightly machinery knows what runs on the server, though, and that is the trade I wanted. It does not care which version of PostgreSQL or CouchDB is installed, what schema a database uses, or whether my laptop is awake. When I add a new service later, putting its volume on that same LVM filesystem is enough to include it.

    None of this counted until I could get the data back out, so before turning on the nightly schedule I pulled the first encrypted copy back from the backup machine. It came to 10.2 GiB on disk. I decrypted it into a separate disk image, replayed the ext4 journal, ran a filesystem check, and found all nine volume directories intact. In a separate test, I loaded one of Swinobot’s database dumps into PostgreSQL and read the data back out. That proved the snapshot image mounts cleanly and its files are readable, while booting every live database directly from a restored disk image remains its own separate drill.

    The backup machine keeps the last seven successful copies, removing the oldest only after the new copy has been written and verified. If thirty hours pass without a fresh one, I get a message on Telegram.

    I still needed Obsidian on my Mac to prove my notes had survived the move. At two in the morning, the laptop stays shut.