How to back up data on dedicated servers and VPS

Backups are critical for website owners to prevent data loss in the event of an accidental or security attack.  A good and secure last known backup allows you to restore the site and minimise data loss, resulting in much less downtime for your site. The time it takes to restore data is determined by the frequency and type of backup you have on your server. Backup solutions or tools are frequently included in hosting packages or as add-on services; for more information on A2 Hosting's backup offerings, please contact the A2 Hosting support team to understand the backup solutions included in your hosting account.This article describes the various backup options for managed and unmanaged Dedicated Server and VPS hosting packages.

Information in this article about unmanaged dedicated servers is only for customers who purchased those plans before May 27, 2024. As of that date, unmanaged dedicated servers are no longer available. Our new managed dedicated server plans feature more powerful hardware to meet your hosting needs. To view available dedicated hosting options, please visit https://www.a2hosting.com/dedicated-server-hosting/.

Available backup options

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

  Managed Unmanaged
VPS Manual backups Manual backups
Dedicated Optional A2 Cloud Backup (powered by JetBackup) and local backups Manual backups and optional local backups
  • For Dedicated Server customers, we offer A2 Cloud Backup, powered by JetBackup, for an additional fee. For more information about A2 Cloud Backup, please see this article.
  • VPS and unmanaged Dedicated Server customers are responsible for configuring their own backups. For information about backups on these account types, see below.
  • Although A2 Hosting maintains backups of unmanaged servers for disaster recovery purposes, we do not have “snapshots” available for recovering specific accounts. You should therefore maintain your own backups.
  • Similarly, A2 Hosting servers have redundant disks (RAID 1 data mirroring). However, RAID 1 only stores the most recent version of a file. It does not enable you to “go back in time” and restore older versions of files. This is another reason why you should maintain your own backups.

Configuring your own backups

If you have a VPS or unmanaged Dedicated Server, you are responsible for configuring your own backups. There are multiple methods you can use to back up your data:

Method #1: Use WHM backups

If your account includes WebHost Manager, you can use it to back up and restore data for all of your user accounts. You can also back up the server's system files, and store your backup files at a remote (offsite) location. For information about how to do this, please see this article.

Method #2: Use cPanel backups

If your account includes cPanel, you can use it to configure backups for individual user accounts. For information about how to create, download, and restore partial and full backups in cPanel, please see this article.

Method #3: Use remote cloud server backups

Using command-line client programs and cron jobs, you can back up your data to an Amazon S3 bucket or Google Drive account. With this configuration, your data is backed up securely to an off-site storage location.

  • For information about how to use Amazon S3 to back up your data, please see this article.
  • For information about how to use Google Drive to back up your data, please see this article.
Method #4: Use a Bash script and cron job

For complete control over what is backed up and when, you can configure your own backups using a Bash script and cron job. To do this, 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, 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 --exclude='/home/username/backup' -czvf "$destination_folder/$archive_file" /home/username/target
    
    • 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).
    • 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.

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 easy. If you do not have cPanel access, you can set up cron jobs from the command line:

  • 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.

  • To set up a cron job using the command line, 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 --routines -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.

Step 4: Do remote backups with rsync (optional)

The scripts in the previous procedures create 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 AlmaLinux or Fedora, type yum install rsync. If the server is running Debian or Ubuntu, type apt-get install rsync.

To set up remote backups using rsync, 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, 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/
    
  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.

More Information

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

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.