Introduction


When you work with a VPS or even any kind of server architecture, is extremely important to backup your data as a precautionary measure against data loss from failures or corruption from handling your virtual server. Here we present a working method to efficiently backup your data with a very easy and widely available tool among Linux systems called “rsnapshot”.


How to install rsnapshot on CentOS


Installing rsnapshot on CentOS requires de EPEL repository. Therefore, you can proceed with the following commands:

For x86 architectures:

wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
sudo rpm -Uvh remi-release-5*.rpm epel-release-5*.rpm

For x86_64 architectures:

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm

If installation went well, you should see additional repo definitions under the /etc/yum.repos.d directory. Now, you can enable the remi repository to get a wider array of packages that are useful and a requirement for many services.


Open your /etc/yum.repos.d/remi.repo file using vi:

sudo vi /etc/yum.repos.d/remi.repo

Edit the [remi] portion and set the enabled option to = 1. Then, save and exit the vi editor.


Install rsnapshot


Enter the following command as root:

yum install rsnapshot -y

Once installed open the file /etc/rsnapshot.conf and tell it where to save your backups:

vi /etc/rsnapshot.conf

Set the section snapshot_root with the desired path for the backups files to be saved:

# All snapshots will be stored under this root directory.
# snapshot_root   /backup/snapshots/

Now, you should uncomment no_create_root 1 in order to stop rsnapshot to create the snaptshot_root_dir. It is useful if you’re creating a backup to a removanble media like a usb drive on a physical server and you do not connect it rsnapshot will not backup to the mount point filling all your drive space.

# If no_create_root is enabled, rsnapshot will not automatically create the
# snapshot_root directory. This is particularly useful if you are backing
# up to removable media, such as a FireWire or USB drive.
#
no_create_root  1

Since we’re using Linux, you should uncomment the cmd_cp line.

# LINUX USERS:   Be sure to uncomment "cmd_cp". This gives you extra features.
# EVERYONE ELSE: Leave "cmd_cp" commented out for compatibility.
#
# See the README file or the man page for more details.
#
cmd_cp          /bin/cp

You should also uncomment cmd_ssh and provide it with the correct path to the ssh binary.

# Uncomment this to enable remote ssh backups over rsync.
#
cmd_ssh        /usr/bin/ssh

If you wish, you can also uncomment the cmd_du option in order to let rsnapshot show you the size of your snapshots.

# Uncomment this to specify the path to "du" for disk usage checks.
# If you have an older version of "du", you may also want to check the
# "du_args" parameter below.
#
cmd_du          /usr/bin/du

Now we should set the backup intervals. If default values does not suits your need, then you should change it accordingly. This is the history of your backups, it means, you should set the number of versions to keep before starting to write new ones to replace the old ones.

#########################################
#           BACKUP INTERVALS            #
# Must be unique and in ascending order #
# i.e. hourly, daily, weekly, etc.      #
#########################################
interval        hourly  6
interval        daily   7
interval        weekly  4
interval        monthly 3

In this section you can set the verbosity level you wish during each backup job. Set it according to your needs. Generally, a level 3 would be fine to see the job as it is performed.

# Verbose level, 1 through 5.
# 1     Quiet           Print fatal errors only
# 2     Default         Print errors and warnings only
# 3     Verbose         Show equivalent shell commands being executed
# 4     Extra Verbose   Show extra verbose information
# 5     Debug mode      Everything
#
verbose

You can also uncomment the log file in order to have it for finding out what went wrong, if that is the case.

# If you enable this, data will be written to the file you specify. The
# amount of data written is controlled by the "loglevel" parameter.
#
logfile /var/log/rsnapshot

If you wish, you can exclude directories or files you do not need to be included in your backups:

# The include and exclude parameters, if enabled, simply get passed directly
# to rsync. If you have multiple include/exclude patterns, put each one on a
# separate line. Please look up the --include and --exclude options in the
# rsync man page for more details on how to specify file name patterns.
#
#include        ???
#include        ???
#exclude        ???
exclude         /home/velocihost/junk/

In this section you setup the backup points, meaning what you want to backup from your servers, starting with localhost.

###############################
### BACKUP POINTS / SCRIPTS ###
###############################
# LOCALHOST
backup  /home/          localhost/
backup  /etc/           localhost/
backup  /usr/local/     localhost/
backup  /var/log/       localhost/
backup  /srv/           localhost/
backup  /boot/          localhost/
backup  /opt/           localhost/

Here, you setup your config file to pull the data to backup over SSH. In this scenario, we will use myserver.com as an example.

backup  [email protected]/home/           myserver.com/
backup  [email protected]/etc/            myserver.com/
backup  [email protected]/usr/local/bin/  myserver.com/

Here is where the config file ends, save it and exit your text editor. You can give your config file a try by running:

rsnapshot configtest

If the result is Syntax OK, then you’re good to go.

Now, let’s setup an automatic login to the remote servers in order to get the backup data. To do this, we create SSH keys to use key base authentication:

ssh-keygen -t rsa

We will not enter any passphrase when requested to, you just press enter.

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

If you did it as root, then you should have a set of keys in /root/.ssh/. Now you need to copy those keys to the server you need to connect to, in this case myserver.com . Then you enter the following command:

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

Now you will be prompted for your password on the remote server, enter it and let the command copy the keys to the corresponding directory. You can verify if it is working with this command:

ssh myserver.com

You can look at /etc/cron.d/rsnapshot and see something like this:

# This is a sample cron file for rsnapshot.
# The values used correspond to the examples in /etc/rsnapshot.conf.
# There you can also set the backup points and many other things.
#
# To activate this cron file you have to uncomment the lines below.
# Feel free to adapt it to your needs.
# 0 */4         * * *           root    /usr/bin/rsnapshot hourly
# 30 3          * * *           root    /usr/bin/rsnapshot daily
# 0  3          * * 1           root    /usr/bin/rsnapshot weekly
# 30 2          1 * *           root    /usr/bin/rsnapshot monthly

If you have no changes to make, uncomment it and leave it like this:

# This is a sample cron file for rsnapshot.
# The values used correspond to the examples in /etc/rsnapshot.conf.
# There you can also set the backup points and many other things.
#
# To activate this cron file you have to uncomment the lines below.
# Feel free to adapt it to your needs.
0 */4         * * *           root    /usr/bin/rsnapshot hourly
30 3          * * *           root    /usr/bin/rsnapshot daily
0  3          * * 1           root    /usr/bin/rsnapshot weekly
30 2          1 * *           root    /usr/bin/rsnapshot monthly

With the last performed step you have enabled a cron job to perform your backups. Let’s try it:

rsnapshot -t hourly

You should check that everything looks as it should and repeat this step for the daily, weekly and monthly cron jobs.


Now, let’s make the first backup by issuing the following command:#
rsnapshot hourly

If there is verbosity set on the config file as instructed before, you should see the files as they are being backed up. You should also start to see files downloaded on your snapshot_root . It should look something like this:

/backup/snapshots# ls -l
total 35
drwxr-xr-x 3 root root 4096 2013-02-21 18:19 daily.0
drwxr-xr-x 3 root root 4096 2013-02-21 18:19 daily.1
drwxr-xr-x 3 root root 4096 2013-02-21 18:18 daily.2
drwxr-xr-x 3 root root 4096 2013-02-21 17:57 daily.3
drwxr-xr-x 3 root root 4096 2013-02-21 17:57 daily.4
drwxr-xr-x 3 root root 4096 2013-02-21 17:56 daily.5
drwxr-xr-x 3 root root 4096 2013-02-21 18:20 hourly.0
drwxr-xr-x 3 root root 4096 2013-02-21 18:19 hourly.1
drwxr-xr-x 3 root root 4096 2013-02-21 18:19 hourly.2
drwxr-xr-x 3 root root 4096 2013-02-21 18:19 hourly.3
drwxr-xr-x 3 root root 4096 2013-02-21 18:19 hourly.4
drwxr-xr-x 3 root root 4096 2013-02-21 17:56 weekly.0

That’s all there is to it. Enjoy your backed up VPS with VelociHOST.


If there is any typographical error or would like to add something we missed, please feel free to let us know.

 

Test on a Miami VPS Now

or

Deploy on a Miami Dedicated Server

Hjälpte svaret dig? 5 användare blev hjälpta av detta svar (20 Antal röster)