The problem of (Unix) swapfiles and server backups

November 10, 2022

I recently wrote about how we're moving away from swap partitions to swap files on our Linux servers, since swap files work fine (and are what Ubuntu defaults to) and swap partitions are slightly more work to set up. However, a potential drawback of swap files recently occurred to me. Suppose that you're using swap files, and also that you're making backups of the root filesystem of some of your servers (because they're 'pets' instead of cattle, and this way you automatically capture local logs and local state). Then, since your swap file is a file in the root filesystem, your backups include your swap file, all however many GBytes it is.

There are two pieces of good news for us in our particular environment (with Ubuntu 22.04 servers). The first is that in general our swap files are small; an extra GB for the moderate number of servers that we back up won't really break the bank, not when set against the amount of data from our fileservers. The second is that Linux doesn't update the modification time for swap files when they're used, so to a backup system these look like files that never change. Since they never change, they'll only be included in full ('level 0') backups, not in the much more common incremental backups that our backup system does.

(This is in general the saving grace of backing up the entire root filesystem for servers; almost all of it is completely static and so only appears in full backups. Only logs and a few other things show up in incremental backups.)

Still, this isn't ideal. If someday we give a NVMe based server a big swap file (because NVMe is so fast, it might not be as dangerous), we might need (or at least want) to care about this. I suspect that I should investigate how to make Amanda automatically exclude these files.

(This also makes me thankful that we corrected the default Ubuntu 20.04 and 22.04 swap file size of 4 GB down to 1 GB in our installer customizations. We had other reasons, but saving 3 GB on full backups is a nice bonus.)


Comments on this page:

By Robert Earl at 2022-11-10 23:29:03:

You could make a convention of naming them with a particular file extension or containing a string, and then they would be easy to exclude by Amanda or any other backup solution.

By Michael at 2022-11-11 04:10:53:

Or, adding further to Robert Earl's comment, place them in a distinct, clearly named directory and then exclude the contents of that directory wholesale from backups. For example, you could put swap files in /swap to future-proof the setup. (Need more swap? Just add one more file in the same place; no need to worry about how it will be handled during backups.)

Any setup that excludes swap files from all backups will need a little extra care during a bare-metal restore, of course, since the swap configuration will then point at a location that doesn't contain a swap file, but that seems like a manageable hurdle. It would have been a concern if the system had so little RAM that it couldn't even boot without swap, but that seems to me like an unlikely scenario these days even in highly resource-constrained but still general-purpose embedded systems.

And if the nodes are cattle, then it's a non-issue since you'd just have whatever process you use to spin up a new node set up swap as well, no different in principle from how it would have to be done with swap partitions in that case.

Or you could just use `chattr +d`.

Written on 10 November 2022.
« Linux swap files don't seem to update their modification time when you swap
How Linux swap files (and swap partitions) find where to read and write »

Page tools: View Source, View Normal.
Search:
Login: Password:

Last modified: Thu Nov 10 23:05:38 2022
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.