CentOS Remote Backup Using rsync

Source: Internet
Author: User
Tags vps install wordpress

CentOS Remote Backup Using rsync

This article provides a quick way to back up the most common things in a typical Web host scenario: website files and database data. We will set up a complete daily backup of the folder of a website and copy the data to a remote server (this can also work on another VPS ). We will also set up an rsync example, the incremental changes just copied. Finally, we will create a MySQL database backup. This program describes how to use a few simple bash commands, rsync and cron to schedule backups. For example, we can install WordPress following this tutorial. It will store WordPress in the/var/www/WordPress directory and the WordPress of the MySQL database. Now we want to back up all the data.

Snapshots and backups are excellent for processing backups and snapshots. There are also excellent, with the lowest configuration backup work as a service solution. But sometimes we want to use another backup server or a server on-demand service, and we want a quick way to pull data from the host provider to our server. In this case, these skills can help.

There are two ways to back up: Incremental backup and full backup. The Snapshot only processes the changes from the last update.

Full backup

Full backup usually has the following features:
Archive all folders
The generated archive is copied to the remote server.

As mentioned above, our data will reside in the/var/www/WordPress directory. We will specify the location of the backup folder.

mkdir -p /backup/wordpress

The preceding command creates a/backup Directory and A/backup/wordpress directory if they do not exist. To create a complete snapshot of our data, we use the tar tool of linux.

Tar-czf/backup/wordpress/initial_backup.tar.gz
/Var/www/wordpress

Tarwill create a gzip compressed file in initial_backup.tar.gz. We can add the AV flag (so we get the tar-czvf) if we want a detailed output (File Name List ). . Tar uses any parameter. We provide a source. In our case, it backs up the/var/www/WordPress directory. We can use two or more parameters, whether it is a file or a folder: IE browser.

tar -czf /backup/cms_systems_backup.tar.gz /var/www/wordpress/var/www/drupal /var/www/joomla

The last command backs up all installed CMS systems.
Now, for our future backup, we may want to add the backup date taken:

Tar-czf/backup/wordpress-'date 'Too many m1_d1_y''.tar.gz
/Var/www/wordpress

Now let's see what

[root@Backup ~]# ls -l /backup/wordpress/total 9760-rw-r--r-- 1 root root 4995743 Apr 17 12:16 initial_backup.tar.gz-rw-r--r-- 1 root root 4995743 Apr 17 12:25 wordpress-041713.tar.gz[root@Backup ~]#

We have two files, one called initial_backup and the other called wordpress-041713 (on April 17, 2013, the time of writing this article ). Now, we need to create a crontab entry every day. Crontab is a linux scheduled task: we tell it when to do something and how to do it. In any case, we opened the crontab Editor:

EDITOR=nano crontab -e

It opens a crontab file in the text editor. By default, do NOT edit vim contained in CentOS images. This requires a single-bit setting. Therefore, we use a simple editor called nano editor for this purpose. We can only use the Default Editor:

crontab -e

Now, we need to tell cron every day to back up data. For example, do we want a lot of traffic at three o'clock A.M. We will also notify it of any results sent to us by email. We will add this content to crontab:

MAILTO=email@example.com30 3 * * * /bin/tar -czf /backup/wordpress/wordpress-`date+\%m\%d\%y`.tar.gz /var/www/wordpress

We save the file with the CTRL-X and confirm the Y and Enter keys. The above command tells linux to repeat our command at every day. We also emailed the result of cron. You will receive the following message:/bin/tar: remove the faucet '/' from the member name. As a sign, everything goes smoothly. In case of an error, the message will contain information so that we can solve the problem. Therefore, daily backup preparation and work.

Copy backup to another remote server

To back up data to another remote server, we will use scp-secure replication. First, we need to generate an SSH key:

ssh-keygen

We can leave the password empty for now, and use/root /. ssh/id_rsa_backup key file (or/home/username /. ssh/id_rsa_backup if we do not run as root ). Now we can check the public key section:

cat .ssh/id_rsa_backup.pub

The SSH key that we need. This public part is copied to the remote server and is in the authorized_keys file. I suppose we already have a remote server named backup.example.com and user backup. This may be an empty New DigitalOcean VPS, but the user must create it in advance. We will only do this part once.

scp .ssh/id_rsa_backup.pubbackup@backup.example.com:/home/backup/backup_key.pub

We will prompt you to enter the password of the backup user. Now let's add the copied file, which should be in authorized_keys. I cannot assume that this user already has a set file and folder, So let's check the information:

ssh backup@backup.example.com "mkdir -p /home/backup/.ssh"ssh backup@backup.example.com "chmod 700 /home/backup/.ssh"ssh backup@backup.example.com "touch /home/backup/.ssh/authorized_keys"ssh backup@backup.example.com "chmod 600 /home/backup/.ssh/authorized_keys"ssh backup@backup.example.com "mkdir -p /home/backup/backups"

The preceding commands create a directory for SSH to work together. If it does not exist, the authorized_keys file is also required. We also created a backup directory to store our files. The rest is that our public key is copied to the file.

ssh backup@backup.example.com "cat /home/backup/backup_key.pub >>/home/backup/.ssh/authorized_keys"

Now, we can use this key to replicate things in the future.
Now, let's copy the backup file there:

scp -i .ssh/id_rsa_backup/backup/wordpress/wordpress-041713.tar.gzbackup@backup.example.com:/home/backup/backups

If our key settings are correct, the file will be copied and we will not be asked to provide the password. We can check whether the file actually exists:

ssh backup@backup.example.com "ls -l /home/backup/backups"

Okay, now we can schedule this action to start the crontab editor again too:

EDITOR=nano crontab -e

Now, we will change our backup line: when we want to add information to copy our backup archive, it is created, so We append a new command so that it looks like this:

30 3 * * * /bin/tar -czf /backup/wordpress/wordpress-`date+\%m\%d\%y`.tar.gz /var/www/wordpress;/usr/bin/scp -i/root/.ssh/id_rsa_backup /backup/wordpress/wordpress-`date+\%m\%d\%y`.tar.gzbackup@backup.example.com:/home/backup/backups

Note: This is not the usual way to do this, it will be better to set the script for completing all tasks, and then arrange the script, but it is concise for this article, we will use this form.

Incremental Backup
But if we have backup software on another server? We just want to synchronize data and keep other servers for backup. In addition, we want to retain the stamp of the document. Then we use rsync. In this example, we only want to copy incremental data from/var/www/WordPress to remote servers, at this time, the/home/backup/snapshots/wordpress directory contains a simple command to implement these functions:

ssh backup@backup.example.com "mkdir -p /home/backup/sync"rsync -avz --delete -e "ssh -i /root/.ssh/id_rsa_backup"/var/www/wordpress backup@backup.example.com:/home/backup/sync

The first line creates a snapshot directory and modifies the modified file in the second copy. Newly created or deleted files can be arranged in cron as follows:

EDITOR=nano crontab -e

The crontab line should look like this:

30 3 * * * /usr/bin/rsync -avz --delete -e "ssh -i/root/.ssh/id_rsa_backup" /var/www/wordpressbackup@backup.example.com:/home/backup/sync

Now our remote server will always have fresh synchronized copies of data, and we can back up the data there.

Back up database

We can also back up our database. First, we need to dump the data. If we follow the WordPress installation instructions, we also have a database WordPress. The user wordpressuser uses the password to access the password, we can do the initial dump as follows:

Mkdir/backup/mysql
Mysqldump <wordpress-u wordpressuser-ppassword | gzip>
/Backup/mysql/initial. SQL .gz

This command creates an initial. SQL .gz gziped SQL file. We can schedule it in cron every day, just as the cron line we created previously should look like this:

0 4 * * * /usr/bin/mysqldump < wordpress -u wordpressuser-ppassword | /bin/gzip > /backup/mysql/mysql--`date +\%m\%d\%y`.sql.gz

Now, we can also use the combination of scp or rsync for remote replication.

0 4 * * * /usr/bin/mysqldump < wordpress -u wordpressuser-ppassword | /bin/gzip > /backup/mysql/mysql-`date +\%m\%d\%y`.sql.gz;/usr/bin/scp -i /root/.ssh/id_rsa_backup /backup/mysql/mysql-`date+\%m\%d\%y`.sql.gz  backup@backup.example.com:/home/backup/

With this setting, we have set basic backup for emergency data.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.