How To Create An Off-Site Backup Of Your Site With Rsync On CentOS 7
Full backup typically does the following:
- Make an archive of all the files in a folder
- Copy the resulting archive to a remote server.
Step 1: Create a backup file for your site
Use the following command to create a backup file
mkdir -p /backup/mysite
Step 2: Compress your site directory
Tar will create a gzip archive in a file initial_backup.tar.gz. We could add a v flag (so we get tar -czvf) if we want a verbose output (list of filenames).
tar -czf /backup/mysite/initial_backup.tar.gz /var/www/samplewebsite
You can pass multiple arguments in the above command for naming the backups after the date.
tar -czf /backup/mysite/backup-`date '+%m%d%y'`.tar.gz /var/www/samplewebsite
Step 3: Schedule the Backup
We can schedule the Backup using
Add the following line of code into the file
30 3 * * * /bin/tar -czf /backup/mysite/backup-`date +\%m\%d\%y`.tar.gz /var/www/samplewebsite
The above command will tell linux to repeat our command every day at 3:30.
Copy the Backups to Another Remote Server
To copy the backups to another remote server, we will use scp - secure copy. First, we need to generate an SSH key:
Copy the public part of the SSH key to the remote server where do you want create the backup of your website
scp .ssh/id_rsa_backup.pub [email protected]:/home/backup/backup_key.pub
The few commands above created a directory for SSH to work with, if it didn’t exist, and also the authorized_keys file, which needs to be present for backups to work. We also created a backups directory to store our files to. Now what is left is to copy our public key to that file.
ssh [email protected] "cat /home/backup/backup_key.pub >> /home/backup/.ssh/authorized_keys"
Let’s copy the backup file over there:
scp -i .ssh/id_rsa_backup /backup/mysite/backup-041713.tar.gz [email protected]:/home/backup/backups
Automate the backup using cron command
Open the cron tab file
Paste the following code
30 3 * * * /bin/tar -czf /backup/mysite/backup-`date +\%m\%d\%y`.tar.gz /var/www/samplewebsite;/usr/bin/scp -i /root/.ssh/id_rsa_backup /backup/mysite/backup-`date +\%m\%d\%y`.tar.gz [email protected]:/home/backup/backups
Increamental backups come into play when we use a software from the other server to do the the backup in the other server. We use the rsync command for this
Following simple command will do the Incremental backup
ssh [email protected] "mkdir -p /home/backup/sync" rsync -avz --delete -e "ssh -i /root/.ssh/id_rsa_backup" /var/www/samplewebsite [email protected]:home/backup/sync
Use the cron command for automating this
30 3 * * * /usr/bin/rsync -avz --delete -e "ssh -i /root/.ssh/id_rsa_backup" /var/www/samplewebsite [email protected]:/home/backup/sync
Backing up your website is a must and you can follow any of the above three techniques to backup your website securely and easily. TheStack also performs offsite backups to prevent disaster, if you are looking for an affordable and reliable dedicated server hosting plans, reach out to our team of experts to know more.