Easy backup method for a Linux server

Do I need backup?

Off course you need to backup your data! If you don’t have backups, you don’t have data security!

Depending on the value of your data you will need to make more backups or keep them longer or whatever. You should always ask yourself the question what if I lose all my data? Which data is extremely important.

You can make a backup of various data, for example:

  • The operating system
  • The storage server (RAID 5 = NO BACKUP!)
  • Mail server
  • SQL server
  • Web server

When you lose a server or its data you should be able to restore your data in a few hours. You can’t make it to lose all your data. When you are working in a professional environment you will notice that there is a backup plan (if not create one!). This plan indicates how often backups should be made and where they are stored.

When you make a backup never save this on the same disk as the original disk. If you do this, in case of a disk failure you will lose all your data + all your backup data. Therefor its important to have a backup disk or a backup server.

Backup is very important but whats even more important is the restore procedure. In case of a failure you will rely on your backups and you need to restore everything.

You should test if your backups work, test if they still work after you restored them. At least once each year you should test if your backup & recovery plan is working. By testing it, you will be familiar with the procedure and its problems. You can also indicate the time it takes to be up & running again. (Your management will demand answers when things go wrong, be prepared!)

How do I start

“What should I use to backup my system then?” might you ask. Easy; the same thing you use to backup/compress everything else; TAR. Unlike Windows, Linux doesn’t restrict root access to anything, so you can just throw every single file on a partition in a TAR file!

In this tutorial I’m going to explain how you can backup a Debian 6.0 server with the tar command. This is a very simple backup solution but it works for what it has to do. Under Linux we have several of solutions again I’m only going to explain one. I’m going to tell you how you can write a shell script and crontab this script. Off course we are open to hear about other solutions and I’d love it of you could send them to me.

Before we can start we must make sure that we are root, we can do this by either running the backup script as root or run it with the sudo permissions. I’m going to choose for the root.

xxxx@debian:~# sudo su
root@debian:~# vim backup.sh

The content of my script:

#!/bin/sh
#This script is written to backup my data from my personal data store
#The high compression solution
tar cvpjf “/mnt/datastore2/backup-`date “+%F”`.tar.bz2″  “/mnt/datastore0/”
#the low compression solution
#tar cvpzf /mnt/datastore2/backup-`date “+%F”`.tgz  /mnt/datastore0/

#Explanation of the parameters
#c= create
#v= verbose (show output)
#p= preserver-permissions
#z= filter the archive through gzip
#j= filter the archive through bzip2*
#f= use archive file or device ARCHIVE

#RESTORE PROCEDURE
#How to restore?
#High compression
#  tar xvpfz backup.tgz -C /mnt/datastore0
#Low compression
#  tar xvpfz backup.tgz -C /mnt/datastore0

#Remove old backups
#It is important to remove old backups

#Removing old backups with high compression
rm -R /mnt/datastore2/backup-`date –date=’14 days ago’ “+%F”`.tar.bz2

# low compression backup
#rm -R /mnt/datastore2/backup-`date –date=’14 days ago’ “+%F”`.tgz

This script was written to backup my personal data from datastore0 to datastore2 (which is my backup disk). If you like to backup your entire server your command would look like this:

#Low compression
tar cvpzf backup.tgz –exclude=/proc –exclude=/lost+found –exclude=/backup.tgz –exclude=/mnt –exclude=/sys /
#High compression
tar cvpjf backup.tar.bz2 –exclude=/proc –exclude=/lost+found –exclude=/backup.tar.bz2 –exclude=/mnt –exclude=/sys /

* if you like to use the bzip2 remember to install bzip2 (apt-get install bzip2)

As you see it’s important to edit this script to your personal wishes. A good backup schema is important. Personally I’m making a full backup each week. (This is private data so this is less important then company data). You can edit the script to backup multiple directory’s at the same time. As you see Tar is a very powerful tool.

Backup data that only grows?

In this case I suggest that you make a backup with the cp (copy) command. I know that your data isn’t compressed but it does the trick.

root@debian:~# cp -bruv /mnt/datastore0/ /mnt/datastore2/

#b = backup
#u = update
#r = recursive
#v = verbose

Crontab

what is crontab? The definition of google: “Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word chronograph (a time-piece). Cron enables users to schedule jobs (commands or shell scripts) to run automatically at a certain time or date. …”

What I’m going to do is initialize a crontab for my backup script. This will make sure that my backups are made right on time.

root@debian:~# crontab -h

Crontab help

To edit the crontab enter

root@debian:~# crontab -e

The standard crontab file will open with the nano editor (at least in Debian 6.0)

The crontab file

To execute my script (the script is in the root directory) I will have to add it to the file. Add this after the last lines in the crontab file

# m h  dom mon dow   command
#m = min
#h = hours
#dom = day of month
#mon = month
#dow = day of week
#command = the command

1 1 * * tue sh /root/backup.sh

#This will execute sh /root/backup.sh 1 min past 1 every Tuesday.

more information on the crontab

What if somethings goes wrong?

everyone has a moment in his life where he thinks “O my god I’m screwed”. To prevent, or at least keep this moments to the minimal you should also test your restore procedure. Depending on what you are restoring you can unpack the archive to a new datastore.

Be carefull with restoring. If you want to test your restore procedure and you have the possibility, test it on an other machine!

When your server is beyond repair, or your users have deleted data that should not be deleted it is time to use the restore procedure. Remember that this procedure could take some time because your data is compressed. If you want to have quick access to your backup data then use the light compression or use no compression at all!

So how do I recover? you can open the archive and decompress the data where you want it.

root@debian:~# tar xvpfj backup.tar.bz2 -C /mnt/datastore0

or

root@debian:~# tar xvpfz backup.tgz -C /mnt/datastore0

WARNING: this will overwrite every single file on your datastore0 with the one in the archive!

when you press enter the magic will happen. The server will start the restore procedure. Again you will need to edit this command to your personal wishes. Always watch out with a restore procedure. You cannot undo changes you make! Especially if you are restoring a Linux server watch out! if you restore the wrong server your server might be malfunctioning.

Conclusion

Backup is very important,  but what is more important is the restore procedure. Make a decent backup & recovery plan.

This is just one solution out of thousand. It is also possible to use third party software. For companies this might be more interesting than this backup script. For home use or small enterprises this backup script might be more then enough.

If you have any questions feel free to ask.

Comments are closed.