Knowledge Base

How to back up data on dedicated servers and VPS

This article describes the various backup options for Flex Dedicated Server and VPS packages.

Backup options

The following table summarizes the backup options available for managed and unmanaged VPS and dedicated servers:

  Managed Unmanaged
VPS Automatic backups (files only) Manual backups
Flex Dedicated R1Soft CDP Backups R1Soft CDP Backups / Manual backups

Managed VPS customers can use the Server Rewind feature in cPanel to restore server backups. However, Server Rewind only restores files. It does not restore databases, so you must set up your own database backups. For information about how to do this, please see this article.

For Flex Dedicated Server customers, A2 Hosting offers R1Soft CDP (Continuous Data Protection) Backups for an additional fee. R1Soft CDP Backups provide daily backups for the last seven days, and a weekly backup for the last four weeks, for a total of one month of backups. For more information about R1Soft, please see this article.

Unmanaged VPS and Flex Dedicated Server customers are responsible for configuring their own backups. For information about how to do this, read the procedures below.

Although A2 Hosting maintains backups of unmanaged servers for disaster recovery purposes, we do not have “snapshots” available for recovering specific accounts. Therefore, you should maintain your own backups.

Configuring backups for unmanaged servers

If you have an unmanaged Flex Dedicated Server or VPS, you are responsible for setting up your own backups. (However, managed VPS and Flex Dedicated Server customers can also configure their own manual backups for extra protection.)

To configure your own backups, use the following procedures.

Step 1: Create a Bash script

The first step is to create a Bash script that generates backups of your files. To do this, follow these steps:

  1. Log in to your account using SSH.
  2. To create a directory where the backup files will be stored, type the following command. Replace username with your own username:
    mkdir /home/username/backup/
    This article uses a directory named backup to store the backup files. However, you can name the directory whatever you want, as long as you replace backup in the following steps with your own directory name.
  3. Create a file named backup-cron.sh in the backup directory. For example, type the following command:

    vi backup-cron.sh
    You can use whichever text editor you prefer (Vim, Pico, Nano, etc.) to create and edit the file.
  4. Add the following code to the backup-cron.sh file. Replace username with your own username, and replace target with the path to the directory that you want to back up:

    #!/bin/bash
    destination_folder=/home/username/backup/`date +%F`
    archive_file="backup-`date +%H%M`.tar.gz"
    mkdir -p $destination_folder
    /bin/tar -czvf $destination_folder/$archive_file /target --exclude='/home/username/backup'
    
    • This script generates a compressed .tar.gz file in a directory name that is determined by the current date. The backup filename itself is determined by the current time (hours and minutes).
    • Make sure you type backticks (`) in the destination_folder variable definition, and not an apostrophe (').
    • The —exclude option prevents the backup script from recursively backing up previous backup files, which could potentially use up disk space quickly.
  5. Save the backup-cron.sh file and exit the text editor.
  6. To make the backup-cron.sh file executable, type the following command. Replace username with your own username:

    chmod +x /home/username/backup/backup-cron.sh

    The Bash script is now ready to run and can be called from a cron job. To set up the cron job, proceed to Step 2: Set up a cron job to run the bash script automatically .

Remote backups

The script in the previous procedure creates compressed backup files on the VPS or dedicated server itself. However, you can also transfer backups to a remote server by using the rsync program.

If rsync is not installed on the remote server, you can install it by using the operating system's package manager. For example, if the server is running CentOS or Fedora, type yum install rsync. If the server is running Debian or Ubuntu, type apt-get install rsync.

To set up remote backups, follow these steps:

  1. Log in to the remote server using SSH.
  2. To create a directory where the backup files are stored on the remote server, type the following command:
    mkdir /backup/
  3. Create a file named backup-remote.sh in the backup directory. For example, type the following command:

    vi backup-remote.sh
    You can use whichever text editor you prefer (Vim, Pico, Nano, etc.) to create and edit the file.
  4. Add the following code to the backup-remote.sh file. Replace username with your own username on the VPS or dedicated server, and replace example.com with the domain name of the VPS or dedicated server. Replace target with the name of the directory that you want to back up:

    #!/bin/bash
    destination_folder=/backup/`date +%F`
    mkdir -p $destination_folder
    rsync -e 'ssh -p 7822' -avl --delete --stats --progress username@example.com:/target $destination_folder/
    
    Make sure you type backticks (`) in the destination_folder variable definition, and not an apostrophe (').
  5. Save the backup-remote.sh file and exit the text editor.
  6. To make the backup-remote.sh file executable, type the following command:

    chmod +x /backup/backup-cron.sh
  7. The Bash script is now ready to run and can be called from a cron job. However, you also must set up SSH keys so the script can access the VPS or dedicated server without prompting for a password. For information about how to set up SSH keys, please see this article.
Step 2: Set up a cron job to run the bash script automatically

Using cron jobs to automate backups makes your life as a system administrator easier. You don't have to worry about remembering to run a backup script at a certain time. If you have a managed VPS or dedicated server with cPanel support, creating cron jobs is very easy. If you do not have cPanel access, you can set up cron jobs from the command line.

Using cPanel

To set up a cron job in cPanel, all you need to do is specify the backup interval (for example, weekly, monthly, etc.) and the command to run. For example, to run the Bash backup script you created above, use the following command. Replace username with your own username:

/bin/sh /home/username/backup/backup-cron.sh

For detailed information about how to set up a cron job using cPanel, please see this article.

Using the command line

If you have a semi-managed VPS or dedicated server, you can set up cron jobs using the command line. To do this, follow these steps:

  1. Log in to your account using SSH.
  2. At the command prompt, type the following command to access the cron configuration file:
    crontab -e
  3. To enter insert mode, type i.
  4. Add the following lines to the cron configuration:

    # Run backup at 23:50hrs every day
    50 23 * * *     /bin/sh /home/username/backup/backup-cron.sh
    This example generates a backup every day at 11:50 PM. Of course, you can modify these intervals however you want.
  5. To receive an e-mail message every time cron runs a script, add the following line to the cron configuration file. Replace [email protected] with your own e-mail address:

    MAILTO=[email protected]
  6. To save your changes and exit the editor, press Esc, type :wq and then press Enter. You see the following message:

    crontab: installing new crontab
  7. To view the cron configuration file without editing it, type the following command:

    crontab -l
Step 3: Set up MySQL database backups (optional)

If you are using MySQL, you can back up databases using the mysqldump command. You can automate this process by running the mysqldump command (or a script that calls it) from a cron job.

The following sample Bash script shows one possible implementation:

#!/bin/bash
db_name=dbname
db_user=dbusername
db_password='dbpassword'
backup_filename=$db_name-`date +%F`

mysqldump -h localhost -u $db_user -p$db_password $db_name | gzip > /home/username/dbbackup/$backup_filename.sql.gz

This script creates a compressed backup of database dbname in the /home/username/dbbackup directory. The backup filename is composed of the name of the database being backed up, and the current date.

The single quote (') characters around the password ensure that any special characters are processed correctly.

For detailed information about how to create MySQL database backups, please see this article.

More Information

For more information about how to set up cron job intervals, please visit http://en.wikipedia.org/wiki/Cron.