Backups for your linux server

In this guide I describe how to setup backups for your Linux server. We will backup both locally and remotely for extra security. We will backup…

  • MySQL-databases using automysqlbackup.
  • Normal files using rdiff-backup.
  • Special large folders per weekday using rsync.
  • To a remote server using rsync.

Setting up the folder structure

We start out with creating a folder structure like this:

The new root-level folder “backup” is the storage location we will use for any kind of backups. The folder “local” inside it is where we will save the backup-files for that computer itself. Later on we will add sibling folders that contain backups from other computers.

Note that the folder “/backup” not necessarily need to be a folder on your main storage device. It could for example be a mount point or a symlink to a folder on another storage device. This makes a lot of sense if you main storage device is an SSD. Do not backup to an SSD.

Backup MySQL-databases

For MySQL-databases we will use the tool automysqlbackup. This tool is in the Ubuntu repositories. The versions are Ubuntu 12.04 with automysqlbackup 2.5 right now when I write this.

We need to make two changes in the config file “/etc/default/automysqlbackup”:

The grep part in DBNAMES is to exclude databases. You may want to add additional databases there that you don’t want to backup. You should now run it once to make sure it works:

Look through the contents in “/backup/local/mysql” to verify it looks the way you want it to.

Question: When will these backups run?
Answer: When installing automysqlbackup the file “/etc/cron.daily/automysqlbackup” was created. Check the content of “/etc/crontab” to see when daily cron runs. In my case it looks like this:

So it seems daily cron-tasks run at 06:25 on my system (Ubuntu 12.04).

Backup normal files

For backing up normal files we are going to use a tool called rdiff-backup that creates incremental backups.

Question: How often should I backup?
Answer: I personally enjoy backing up once a day.

Question: How long should I save the backups?
Answer: I personally enjoy saving the backups for three months.

Question: What files should I backup?
Answer: A good start is “/home”, “/etc” and “/root”. You will however often need to setup some special exclude and include rules.

With this in mind I should create the file “/etc/cron.daily/backupfiles” with the following content:

Ensure that file has the right permissions:

Say however that I would like to ignore all home-files for the user “derp” and only want the folders “a” and “b” from the user “herp”.

The file would then look like this instead:

It is important to consider that:

Multiple include and exclude options take precedence in the order they are given.
http://www.nongnu.org/rdiff-backup/examples.html#exclude

I prefer looking at the include/exclude order like this:

  • The deeper the directory paths – the higher up in that cron-file they should be written.
  • We can visualize the end result by imagining we apply the includes/excludes from bottom up.

Backup normal files using crontab

I actually prefer having more fine grained control over when backups are run. For this we will use crontab. Start out with creating a folder for your cron-scripts:

And then create the file “/root/cron/backupfiles.sh” instead of the file “/etc/cron.daily/backupfiles”. Next we add a crontab entry as root:

If we want it to run the same time as automysqlbackup (06:25) the crontab entry would look like this:

Backup special large folders per weekday

To be more precise I run a few minecraft servers. The map files are huge and change often because they are compressed natively. If we took incremental backups (rdiff-backup) those increments would be very large due to the compression. I prefer creating this setup instead:

Monday is “1” and Sunday is “7”. Next we create the script “/root/cron/backupserver.sh”.

Note that after the first week the old backups will start getting overwritten.
Lets add it to the root crontab as well:

Backup to a remote server

For extra security it makes sense to backup to a remote computer as well. Lets call the local computer “torkel” and the remote computer “berit”. Let’s also assume entries for those are added to the file “/etc/hosts” so they resolve into actual ip-addresses.

For this we could simply rsync the folder “torkel:/backup/local” to “berit:/backup/torkel” but in our case the folder “torkel:/backup/local/server” will be huge. We thus only synchronize “torkel:/backup/local/1” (the monday server backup).

Create a user for torkel@berit and create the backup folder:

Create a private key for root@torkel and transfer it to torkel@berit:

Remove the password from torkel@berit:

Create the script “/root/cron/backupremote.sh”.

Note that the include/exclude syntaxes are different for rsync and rdiff-backup.
Lets add it to the root crontab as well:

Restoring MySQL-databases

TODO

Restoring normal files

TODO

Restoring special large folders

TODO

By | 2012-07-17T23:45:14+00:00 July 17th, 2012|Uncategorized|0 Comments

Leave A Comment