How do I backup a server?

Solution 1:

Image type backups have several problems:

  1. They can not do daily/incremental backups
  2. Most of them can not extract an individual file or set of files you realize you deleted/overwrote
  3. They can not restore to a disk that is smaller than the original, even if there was plenty of free space
  4. Restoring to a larger disk often requires extra steps to make use of the additional space

It is best to to stick with the tried and true methods: tar and dump. I use dump because it is much faster at backing up and extracting individual files than tar, and handles incremental backups better. It also uses multithreaded compression, which is nice on today's multi core machines. Either way, when making the backup, you need to shut down your services to make sure no files are being written while you make the backup. If you use LVM then you only need to do this long enough to make a snapshot, then you can bring your services back online, and backup the snapshot. Here is the script I have cron automatically run every night:

#!/bin/bash
set -e
declare -a LEVELMAP=(1 5 4 5 3 5 4 5 2 5 4 5 3 5 4 5 1 5 4 5 3 5 4 5 2 5 4 5 3 5 4 5)
DATE=`date +%-d`
LEVEL=${LEVELMAP[$DATE-1]}
echo Performing a level $LEVEL dump
#shutdown services here
sync
lvcreate -s -n snap devserv/root -L 500m
#start them back up here
dump -$LEVEL -quz9 -b 1024 -f /backup/dump.$LEVEL /dev/mapper/devserv-snap
lvremove -f devserv/snap

I make a level 0 ( full ) dump usually every ~6 months when I upgrade the server, then this script makes a level 1 dump ( all files changed since level 0 ) on the 1st and 17th of the month, and alternates between level 2-5 the rest of the days.

This means to restore the system I have at most 5 dumps to restore, sometimes less ( if the server dies right after the level 1 dump then you just need to restore the level 0 and 1 ), and I have several dumps stretching back over the last days, weeks, months I can go pull an old file out of if I need to, possibly multiple versions of it as it changed at different points.

If you don't care about having multiple points to go back and pull older files out of, a simpler alternative is to do a level 0 once every 6 months, level 1 every Monday, and level 2 every other day of the week.