Backup

From SaruWiki
Jump to navigation Jump to search

If you have a server, you need a good backup.

No really. You need a good backup.

You only have the server because it delivers services that you want to have delivered, and you've invested time and effort in getting the server to deliver those services. Furthermore, your server will contain more or less valuable data. There may come a time when you lose part or all of your server: due to hardware failure, corruption due to unexpected shutdown, malware, hacking, theft, fire or other disasters, failed upgrade, or user or operator error. When that happens, you probably want to restore the services your server was providing. This goes much faster and cheaper (in time, money, or both) if you have a current backup.

What to backup

In essence, everything you need to backup should be on your server's file system. Depending on your configuration, you want to include the following items in your backup:

  • a file-based backup of
    • your /data directory, as it contains your data (if you've created such a place in your filesystem). Note that if you have daemons or services that keep files open (e.g. a MySQL server that stores its databases under /data/mysql), then you may want to stop the daemon or service, make a copy of the files, and then restart the services
    • the /home directory, as it contains the environment of the root user, your own user account, and possibly the accounts of other users on the system;
    • the /etc directory, as it contains the complete configuration of your server
    • the /boot directory, as it contains the files with which to boot your server with all it's hardware
  • a database dump of
    • the database's internal database, containing users, permissions and such;
    • each of your production databases.
  • the following bits of system data
    • for every hard disk: a copy of the partition table
    • for every bootable hard disk in your system: a file containing a copy of the Master Boot Record (MBR)
    • if you use LVM: a backup of your LVM configuration

There are also things that you probably do NOT want to backup:

  • the /tmp directory,
  • several directories under /var, most likely /var/run, /var/spool and /var/cache.

How to backup

  1. connect and mount an external USB harddisk, e.g. under /media/backup;
  2. make an initial file-based copy of all your data files using cp -pR <datapath> <backuppath>, e.g.
    cp -pR /data /media/backup
  3. copy the partition table of each harddisk, or at least of each bootable harddisk:
    sfdisk -d /dev/sda > /media/backup/partitions.sda
  4. copy the boot sector of each bootable harddisk:
    dd if=/dev/sda of=/media/backup/MBRsda.bin bs=512 count=1
  5. backup your LVM configuration:
    vgcfgbackup -v -f /media/backup/vgcfgbackup.lvm
  6. dump your MySQL databases to files:

What to test

When you've created a backup, you may want to perform some tests before starting that risky upgrade:

  • REBOOT your server! You must be sure that you've got a backup from a server that can actually reboot. If you have a boot problem, fix it, then update your backup;
  • Check your filesystems (your reboot may have done that for you).

How to restore

You should know how to restore the data you've backed up, or the backup is totally useless: "nobody cares if you can backup, only if you can restore".