Skip to content
Linux Running · 7 min read

Before You Delete the Old Server: Final Backup, Checksums and Dangling DNS

Final offsite backup with zstd and sha256 checked on both ends, a one-off SSH key, a nightly job for the new box, and 8 DNS records still on the dead IP.

The forum was live on the new server by early afternoon (that move had its own gotchas). Nothing I cared about was left on the old box. The plan was to keep it around for a week or two as a fallback. I changed my mind the same day: make one complete, verified backup, then delete it. A full archive is a better fallback than a server you keep paying for and patching.

Deleting a server is one click. The work is in what you do before that click and what still points at it afterwards.

TL;DR: Dump all databases and tar the whole root filesystem, both through zstd, straight to an offsite storage box, with sha256 computed on the fly. Then run sha256sum again on the storage box side and compare. Use a fresh SSH key just for this and remove it afterwards. Set up the nightly offsite job on the new server and use StrictHostKeyChecking=accept-new with BatchMode. After deleting the server, list every DNS record in every zone that still points at the old IP, delete them, check again with a dry run and against the authoritative nameserver. I had 8 of them.

What was on the box

df -h /
# /dev/sda1   75G   61G   11G  85% /
du -sh /var/lib/psa/dumps /var/www/vhosts /var/lib/mysql
# 28G   /var/lib/psa/dumps
# 7.2G  /var/www/vhosts
# 832M  /var/lib/mysql

61 GB used, 28 GB of it old panel backups. I took everything anyway. Sorting out what's worth keeping is a job for later, from the archive, not for the last hour of a server's life.

A fresh key just for the backup

The storage box already had three keys in its authorized_keys, one of them labelled for recovering this exact server. The matching private key wasn't on the server anymore. So I made a new one on the old box, appended it on the storage box, kept the other keys and saved a copy of the file next to it:

ssh-keygen -t ed25519 -N '' -C backup-final -f /root/.ssh/backup_final
# append backup_final.pub to .ssh/authorized_keys on the storage box (keep a backup of that file first)

A key with one job is easy to remove when the job is done. More on that below.

The backup itself

The shape of it, simplified. It ran detached on the old box and wrote to a log file:

#!/bin/bash
SB="ssh -i /root/.ssh/backup_final -p 23 -o BatchMode=yes u123456@u123456.example-storage.net"
T=archive/server-old-final-$(date +%Y%m%d)
$SB "mkdir -p $T"

# 1) all databases
mysqldump --all-databases --single-transaction --quick --routines --events \
  | zstd -T0 -q \
  | tee >(sha256sum | sed "s/-\$/mysql-all.sql.zst/" >> /root/SHA256SUMS) \
  | $SB "dd of=$T/mysql-all.sql.zst"
echo "mysql dump rc=${PIPESTATUS[0]}"

# 2) the whole root filesystem
tar --one-file-system -cpf - / 2>/root/backup-final-tar.err \
  | zstd -T0 -q \
  | tee >(sha256sum | sed "s/-\$/rootfs.tar.zst/" >> /root/SHA256SUMS) \
  | $SB "dd of=$T/rootfs.tar.zst"
echo "rootfs tar rc=${PIPESTATUS[0]} (1 = files changed while reading, ok)"

cat /root/SHA256SUMS | $SB "dd of=$T/SHA256SUMS"
echo BACKUP_DONE

The log afterwards:

16661973 bytes (17 MB, 16 MiB) copied, 3.94948 s, 4.2 MB/s
mysql dump rc=0
43815076020 bytes (44 GB, 41 GiB) copied, 877.304 s, 49.9 MB/s
rootfs tar rc=0 (1 = files changed while reading, ok)
BACKUP_DONE

17 MB for all databases, 43.8 GB for the root filesystem, about 15 minutes for the tar at 49.9 MB/s. Nothing hits the old server's disk, which matters on a box that's 85 % full.

Why accept exit code 1 from GNU tar: on a running server, log files change while tar reads them, and tar reports that as 1. It isn't a failed archive. Exit 2 is a real error. In my run it was 0 anyway. The only noise in the error file was "Removing leading /" and "socket ignored".

--one-file-system keeps /proc, /sys and friends out. If you have data on a second mount, list it separately.

I also dropped a short README.txt into the folder: what this is, where it came from, how to restore it. You'll be glad about that in a year.

Gotcha: the watcher died, the backup didn't

I watched the tar from my laptop through an SSH session that tailed the log. That session dropped, and my watcher reported failure. The backup was fine. It ran detached on the server, and when I logged in again the log already said BACKUP_DONE, long before.

Two lessons. Run long jobs detached on the box itself (nohup, systemd-run, tmux, whatever you like), never as a child of your SSH session. And a dead watcher tells you nothing about the job. Look at the job's own log and exit codes.

Verify on the other side

The checksum computed while streaming only proves what was sent. I wanted to know what's actually stored, so I hashed the files again on the storage box:

$SB "sha256sum $T/mysql-all.sql.zst $T/rootfs.tar.zst"
cat /root/SHA256SUMS

Both hashes matched the ones from the stream. That means every byte was read back once from the box and is identical. What it doesn't prove is that a restore works. I didn't test a restore here, and I'd rather say that than pretend. I wrote more about checking backups properly in dead backups: check freshness.

After that I removed both keys belonging to the old server from the storage box's authorized_keys: the new one and the old recovery key. Backup of the file next to it again. The server was about to disappear, and its keys had no reason to keep working.

Gotcha: the new server's offsite job and known_hosts

The old box used to push the forum offsite every night. The new box didn't do that yet, so I set it up before deleting anything: its own key, nightly at 04:50, after the local DB dump at 03:41. On the storage box: db/ with all daily dumps (nothing gets deleted there, locally 14 days), files/ as a mirror of the webroot, root/ with the helper scripts and crons, and a LAST_OK timestamp.

The first run failed:

Warning: Permanently added '[u123456.example-storage.net]:23' (ED25519) to the list of known hosts.
rsync: [Receiver] mkdir "/home/archive/forum-offsite/db" failed: No such file or directory (2)

The script first creates the target folders over SSH with BatchMode=yes. That first connection hit an unknown host key, and BatchMode means no prompt, so it just failed. My script didn't stop there. The rsync afterwards added the host key and ran into folders that didn't exist. The fix:

SSH_OPTS="-p 23 -i /root/.ssh/forum_offsite -o BatchMode=yes -o StrictHostKeyChecking=accept-new"
ssh $SSH_OPTS u123456@u123456.example-storage.net "mkdir -p archive/forum-offsite/{db,files,root}" || exit 1

accept-new saves unknown keys on first contact but still refuses a changed one. That's the sane setting for unattended jobs. And || exit 1, so the next one stops where it breaks. Plain rsync backups are fine for this, as long as they fail loudly.

Gotcha: 8 DNS records still pointing at a deleted server

I deleted the server in the provider's console. Then I checked DNS, and 8 A records across two zones still pointed at the old IP: old hostnames like mail, web, a status page from an old monitoring setup, each with a www variant. Nothing important lived there anymore. One returned a 403, one a redirect, the others nothing.

Why this is a risk: the provider will hand that IP to someone else at some point. Then status.example.com points at a stranger's server. Whoever gets the IP could serve content under your subdomain and very likely get a valid certificate for it, because HTTP validation only checks who answers on that IP. Probably nobody ever notices. I still don't want my name on someone else's box.

So I wrote a small script against my registrar's DNS API. Abstract version:

import sys

def find_records(api, ip):
    for zone in api.list_zones():
        for rec in api.list_records(zone):
            if rec.type in ("A", "AAAA") and rec.content == ip:
                yield zone, rec

ip, go = sys.argv[1], "--go" in sys.argv
hits = list(find_records(api, ip))
print("Mode:", "EXECUTE" if go else "DRY RUN", "ip=" + ip, "hits:", len(hits))
for zone, rec in hits:
    print(f"  {zone:28} {rec.type:5} {rec.name:36} {rec.content}")
    if go:
        print("    deleted", api.delete_record(rec.id))

api is your registrar client. Dry run by default, deletes only with --go. I ran it for the old IPv4 and IPv6 address. IPv6 had no hits, IPv4 had the 8.

The first --go run removed 6 of the 8. Two www records were still there. Running it again got them. Then verify twice:

python drop-ip.py 203.0.113.10                 # dry run: 0 hits
for h in mail web status www.mail www.web; do
  echo "$h -> $(dig +short $h.example.com A @ns1.example-registrar.net)"
done

Ask the authoritative nameserver, not your resolver, otherwise you're looking at cached answers.

One surprise: some of those names didn't go blank. They now resolved through a wildcard record in the zone to another server of mine. Harmless here, because I control that server. But worth knowing: deleting a record doesn't always mean the name stops resolving. A wildcard catches it.

Next time the order is DNS first, then the server. And after the time my server ran out and my backups turned out truncated, I don't delete anything anymore before the archive is verified on the other side.


Related posts