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.
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 CDP Backups (powered by R1Soft) and local backups | Manual backups and optional local 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:
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.
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.
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 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:
The first step is to create a Bash script that generates backups of your files. To do this, follow these steps:
mkdir /home/username/backup/
Create a file named backup-cron.sh in the backup directory. For example, type the following command:
vi backup-cron.sh
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'
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.
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:
/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.
crontab -e
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
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]
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
To view the cron configuration file without editing it, type the following command:
crontab -l
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.
For detailed information about how to create MySQL database backups, please see this article.
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.
To set up remote backups using rsync, follow these steps:
mkdir /backup/
Create a file named backup-remote.sh in the backup directory. For example, type the following command:
vi backup-remote.sh
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/
To make the backup-remote.sh file executable, type the following command:
chmod +x /backup/backup-cron.sh
For more information about how to set up cron job intervals, please visit http://en.wikipedia.org/wiki/Cron.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
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.