How do I back up a dedicated server?

Procédure

Why make a backup?

When you work with IT equipment, you need to make regular back-ups.

This backup will enable you to return your machine or website to a stable state.

In this tutorial we will look at how to manually automate the backup of your server.

If this operation is too complex for you, you can open an outsourcing contract so that we can set up this backup.

How do you manually back up important folders and files on your server?

Here are a few examples of scripts that will enable you to easily back up all the documents stored on your VPS server:


1. Simple backup

Please note that this example does not delete your previous backups. We advise you to check the disk space available on your backup solution.

Please note that this script requires free space on your VPS server. Free space equal to the space taken up by the folders you wish to back up is recommended.

This first example is a simple backup solution for your VPS. It is based on the tar (archiving) and ncftp (ftp client) programs.

Simple backup is fairly slow if there are a lot of files to backup. It also takes up a lot of space on the FTP server compared with other solutions.

Restoration is straightforward: simply apply the last backup.

The tar program is installed as standard on most distributions. The ncftp programme is not generally installed by default. Here are the instructions for installing ncftp:

  • On a debian distribution (ISPConfig, Prestashop) or ubuntu :
apt-get install ncftp

  • On a CentOS distribution :
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/ncftp-3.2.2-1.el5.i386.rpm

Copy and save the following content in the /root/backup.sh file on your server.

  • Script backup.sh
#!/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ## ## Example Backup Script ## ## LWS ( http://lws.fr/ ) ## ## ## Configuration ## # Directory to backup REPERTOIRES="/var /home" # FTP Parameter FTP_SERVEUR="192.168.2.242" FTP_UTILISATOR="client_ftp" FTP_PASSWORD="123456" FTP_REPERTOIRE="/backup/" ## ## Application settings ## # backup REPERTOIRE_BACKUP="/tmp/backup/" BACKUP="backup.tar.gz" # tar TAR_OPTIONS="--exclude=$REPERTOIRE_BACKUP --exclude=/dev --exclude=/proc --exclude=/sys" # date DATE=$(date +"%Y-%m-%d") ## ## Creation of the backup ## mkdir -p ${REPERTOIRE_BACKUP} mkdir -p /var/backup/ tar ${TAR_OPTIONS} -zcvf ${REPERTOIRE_BACKUP}/${BACKUP} $REPERTOIRES ## ## Send backup ## ncftp -u"$FTP_USER" -p"$FTP_PASSWORD" $FTP_SERVER < mkdir $FTP_REPERTOIRE mkdir $FTP_REPERTOIRE/$DATE cd $FTP_REPERTOIRE/$DATE lcd $REPERTOIRE_BACKUP mput * quit EOF rm -rf $REPERTOIRE_BACKUP

  • Configure the script by editing it:

The DIRECTORIES variable contains the path of the directories to be backed up. The /var and /home directories are generally the two directories containing user data.

# Directory to be backed up REPERTOIRES="/var /home


The FTP_ variables contain the connection parameters for your FTP backup account. FTP_REPERTOIRE is the directory where your backups will be saved on your FTP account.

# FTP parameters FTP_SERVER="192.168.1.250" FTP_USER="client_ftp" FTP_PASSWORD="12345678" FTP_REPERTOIRE="/backup/"


Make the backup.sh script executable:

chmod +x /root/backup.sh


Then add an entry in the cron tasks :

crontab -e


If you want to make a backup every day at 3am, add the following entry:

0 3 * * * /root/backup.sh >/dev/null 2>&1


If you want to back up every Sunday at 3am, add the following entry:

0 3 * * 0 /root/backup.sh >/dev/null 2>&1


2. Incremental backup

Please note that this example does not delete your old backups. You are advised to check the available disk space on your backup solution.

Please note that this script requires free space on your VPS server. Free space equal to the space taken up by the folders you wish to back up is recommended.

Example 2 repeats example 1 but adds the incremental backup.

The incremental backup only backs up files that have changed after a full backup has been made. It is advisable to perform a full backup from time to time.

This solution allows you to have regularly updated backups while consuming less space than full backups that are just as regular. Incremental back-ups are also faster.

However, restoration is more complicated. When restoring, you need to apply the last full backup and then all the incremental backups since that last full backup.

In this example, we will perform a full backup on the first Sunday of every month and then incremental backups every day.

Repeat example 1 except for the part concerning cron jobs.


Edit the /root/backup.sh file and modify the TAR_OPTIONS parameter as follows:

# tar TAR_OPTIONS="-g /var/backup/incremental_tar.data --exclude=/var/backup/ --exclude=$REPERTOIRE_BACKUP --exclude=/dev --exclude=/proc --exclude=/sys"


Then add two entries to your list of cron tasks:

crontab -e


The first entry performs backups every day at 3am:

0 3 * * * /root/backup.sh >/dev/null 2>&1


The second entry will delete the file /var/backup/incremental_tar.data on the first Sunday of each month. The tar program performing the backup will no longer know which files have already been backed up and will perform a new full backup.

0 2 1-7 * 0 rm -rf /var/backup/incremental_tar.data

Rate this article :

This article was useful to you ?

Article utileYes

Article non utileNo

Vous souhaitez nous laisser un commentaire concernant cet article ?

Si cela concerne une erreur dans la documentation ou un manque d'informations, n'hésitez pas à nous en faire part depuis le formulaire.

Pour toute question non liée à cette documentation ou problème technique sur l'un de vos services, contactez le support commercial ou le support technique

MerciMerci ! N'hésitez pas à poser des questions sur nos documentations si vous souhaitez plus d'informations et nous aider à les améliorer.


Vous avez noté 0 étoile(s)

Similar articles

3mn reading

How do you trigger outsourcing?

0mn reading

Contact technical support by e-mail

0mn reading

How do I restore a backup of my dedicated VPS server?


Ask the LWS team and its community a question