Linux Enterprise Cluster notes-methane synchronization: SSH and rsync

Source: Internet
Author: User
Tags ssh server
Chapter 4: synchronizing servers with rysnc and SSH

1. rsync. This book describes ha and load balance. This chapter mainly describes how to synchronize files and content on the two servers. The content on the two servers must be exactly the same during the HA process. In this way, when one server fails, the other server can take over immediately. Of course, if the two servers have no disks and share one storage, this is unnecessary. If not, do these things.

2. in the organization of this book, we should first talk about ha and then about load balance. After describing Ha, you can make the load balance server a dual-machine ha, so that load balance is more scientific.

3. Rsync can be used to synchronize two machines. Compared with the CP tools, rsync provides the following enhancement feature:
Examines the source files and only copies blocks of data that change.
(Optionally) works with the Secure Shell to encrypt data before it passes over the network.
Allows you to compress the data before sending it over the network.
Will automatically remove files on the destination system when they are removed from the source system.
Allows you to throttle the data transfer speed for WAN connections.
Has the ability to copy Device Files

Rsync can push data from one machine to another. It can also take data from one server to the local machine in PULL mode. Unison is more powerful than rsync.

4. OK. Then let's see how to use rsync and SSH. Bundling rsync and SSH can increase the security of rsync, because/etc/passwd may be the data to be synchronized, and SSH can be used to ensure the security of the data. The scenario is two servers. One server is called the primary server (this machine stores the data to be synchronized, and the data can only be modified on this machine. It is an SSH client, that is, the primary server initiates a connection), and the other server becomes the backup server (this server stores redundant backup of data, which is an SSH server ). Then, first, configure SSH and configure it to shuttle between the two machines without entering a password. At least the password from the primary server to the backup server does not need to be entered, use public/private key. Use the following rsync command to start synchronization:

# Rsync-v-a-z-e SSH -- delete/www/<Backup Server>:/www

In this command,-V indicates verbose;-A indicates synchronizing all files and folders;-Z indicates compressing data, which reduces the transmission traffic; -e ssh indicates that SSH is used as the connection method. If you want SSH to be used as the default connection method for rsync, you can set the rsync_rsh environment variable; -- delete indicates that if destination (Backup Server) if some files in the directory are not in the primary server directory, delete them to ensure that the backup server and primary server files are exactly the same. Therefore, the above command is to synchronize all files under the/WWW directory of the primary server and all files under the backup server/WWW directory. Here/www/is followed by an slash, indicating that the file under this directory is to be synchronized, not just a directory.

5. Rsync also has an option -- stats. After this option is added, rsync prints the synchronization details, such as what files are synchronized and how many bytes are synchronized. Rsync will not synchronize files that have not been modified. To put it bluntly, Rsync is an incremental backup. If you want to synchronize a file, you can:

# Rsync-v-a-z-e ssh/etc/sysconfig/iptables <Backup Server>:/etc/sysconfig/

6. There is also an option for rsync, which is -- bwlimit. This item can be used to limit the data size sent by rsync per second. It is quite useful in slow connection. For example:

Rsync-v-a-z-e SSH -- delete -- bwlimit = 8/www/<Backup Server>:/www # Send 8 K bytes per second

7. Use cron instead of rsync to become a scheduled task. Crontab-E:

# Execute rsync for 30th minutes every hour
30 * rsync-v-a-z-e SSH -- delete/www/<Backup Server >:/ WWW>/dev/null 2> & 1

You can use scheduled tasks to easily Synchronize files. You can write a script to enhance the function. For example, if rsync fails, you can send us an email or something. This is very simple.

8. In the end, rsync uses port 873 and SSH uses port 22. If there is a firewall, open these two ports.

Add a bonus when using rsync. Rsync reads file names and folders in byte mode, that is, rsync does not care about encoding, for example, the files and folders on my SuSE are the UTF-8 encoding method, at this time I back up the things on this machine to another machine, the created folder and files, their names are all UTF-8, and if we're mounting this machine with Samba, if codePage = cp936, the backed up files are all garbled characters (Chinese file names and folders). Therefore, codePage = UTF-8 must be specified here. Then you can check the files again. Supplement: rsync supports multiple sync methods, such as SSH and RSH. In addition, rsync also provides an rsyncd program, as long as rsyncd is started on the synchronized machine, then, rsync can work properly without relying on SSH and RSH. In this case, it will communicate with the other party's rsyncd. This is the origin of port 873 mentioned above.

 

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.