Linuxrsync use _ rsync for file sync backup from linux to linux (windows) _ rsync

Source: Internet
Author: User
Tags rsync rsync usage
Rsync is a data image backup tool in Linux. It can be seen from the software name-remotesync. Rsync supports most Unix-like systems, including Linux, Solaris, and BSD. I. rsync CommandIntroduction


Rsync is a command for Synchronizing files to a Linux system. It can synchronize two computers to files and directories, and find different files in different blocks to reduce data transmission. You can also synchronize data from a computer to different directories. For example, you can write a simple script to back up some configuration files from the system to a dotfiles folder, upload to GitHub so that new computers or systems can use rsync to restore these configuration files, which is very convenient and automated.

Basic rsync usage
$ rsync [options] src dest
  1. Synchronization is triggered when the content of the target and source files is different.
  2. Rsync does not synchronize files to "modify time" and synchronizes them to files. The destination to "modify time" is always changed to the latest time to the time.
  3. Rsync does not pay too much attention to the rwx permission on the target file. If the target does not have the permission on this file, the permission is consistent with that on the source. If the target has the permission on this file, the permission will not change with the source.
  4. Rsync has the read permission on the source file and write permission on the target path. rsync ensures that the target file is synchronized to the source.
  5. Rsync can only create files by logging on to the target end to the account, and cannot maintain consistency between the source and owner of the target end files.
[-T option]
  1. After the-t option is used, rsync always thinks about one thing, that is, synchronizing the "modify time" of the source file to the target machine.
  2. Rsync with the-t option will become smarter. It will first compare the timestamp and file size of the files on both sides before synchronization. If they are consistent, the files on both sides will be considered the same, this file will no longer be updated.
  3. Because rsync is smart, it will be replaced by smart mistakes. If the timestamp, size, and content of the target file are exactly the same as those of the source file, rsync cannot find them. This is the legendary "pitfall "!
  4. When rsync is smart, the solution is to use the-I option.
[-I option]
  1. -The I option will make rsync very honest, and it will initiate data synchronization from one file.
  2. -The I option ensures data consistency. The cost is that the speed slows down because the "quick check" policy is abandoned. (The quick check policy is to first check the file Timestamp and file size, and first exclude a batch of files that are considered to be the same)
  3. In any case, the modify time of the target object is always updated to the current time.
[-V option]

This option is easy to understand, that is, to allow rsync to output more information. The more v you add, the more log information you can obtain.

[-Z option]

This is a compression option. Once this option is used, rsync compresses the data sent to the peer end before transmitting the data. If the network environment is poor, we recommend that you use it. Generally, the-z compression algorithm is the same as that of gzip.

[-R option]

When we use rsync for the first time, we often encounter this problem? Atlas? /P>

$ rsync superman machineB:/home/userBskipping directory superman

If you don't tell rsync that you need it to help you synchronize folders, it won't take the initiative, which is exactly what rsync is lazy about. Therefore, if you really want to synchronize folders, you need to add the-r option, that is, recursive (recursive, cyclic), like this:

$  rsync -r superman machineB:/home/userB

For folders, rsync is clear, as long as you add the-r option, it will go to the folder to check as much as possible, instead of "quick check" for the folder itself.

[-L option]

Once rsync finds that a file is a soft link, it will be ignored unless we add the-l option. When the-l option is used, rsync will completely retain the soft link file type. In the original version, the soft link file will be copied to the target end, instead of "follow link" to the object file to which it is directed. If I want rsync to adopt the follow link method, use the-L option. You can try the effect on your own.

[-P option]

The full name of this option is "perserve permissions". As the name suggests, it means to keep the permission. If you do not use this option, rsync handles permission issues as follows:

  1. If the target does not have this file, the permissions of the target file will be consistent with those of the source after synchronization;
  2. If the object already exists on the target end, only the file content is synchronized and the permissions remain unchanged.

If you use the-p option, rsync keeps the target end consistent with the source end.

[-G option and-o option]

These two options are a pair to keep the group and owner of the file clear. However, you must note that only Administrators can change the owner and group permissions.

[-D option]

-D option. The original text is "preserve devices (root only)", which literally means to keep the original information of the device file.

[-A option]
  1. -Option a is a dominant option in rsync, because you use option-a, it is equivalent to using-rlptgoDThis option. The only option is-.
  2. -The student name of option a should be archive option, and the Chinese name is archive option. Using the-a option indicates that you want to use a recursive method for synchronization and maintain consistency in all aspects as much as possible.
  3. However,-a cannot synchronize hard links. If you have such requirements, add the-H option.
[-Delete option,-delete-excluded option, and-delete-after option]

The three options are related to "delete:

  1. -Delete: if the source end does not have this file, do not want to own it. delete it. (If you use this option, you must use the-r option together)
  2. -Delete-excluded: Specify the objects to be deleted on the target.
  3. -Delete-after: by default, rsync clears the target file and starts data synchronization. If this option is used, rsync synchronizes data first, and then delete the files to be cleared.

This learning should be used with caution. A lot of things will be deleted accidentally.

You can use the-n option, which will warn you about the list of affected files, but it will not be deleted, which gives us the opportunity to confirm and maneuver.

[-Exclude option and-exclude-from option]

If you do not want to synchronize something to the target end, you can use the-exclude option to hide it. rsync still attaches great importance to privacy, you can use the-exclude option multiple times to set a lot of "privacy ". If you want to hide too much privacy, setting it in the command line option will be troublesome, and rsync is still considerate. It provides the-exclude-from option, this allows you to list privacy in one file, and then let rsync directly read the file.

[-Partial Option]

This is the legendary resumable upload function. By default, rsync deletes the files that are interrupted by transmission and re-transmits the files. However, in some special cases, we do not want to re-transmit data, but resume data transmission. We often see people using the-P option, which is designed to be lazy. In the past, people always had to manually write-partial-progress. They thought it was too difficult to replace it with a new option, so-P came into being.

[-Progress options]

With This option, rsync will display the transmission progress. What is the use of rsync? rsync provides an interesting explanation: This gives a bored user something to watch.

Rsync Core Algorithm

The rsync algorithm is very efficient. I will not go into details about the core algorithm in an article on cool shell.

Rsync server setup

You can set up an rsync server. rsync runs as a daemon. The client writes the rsync command into a shell script and regularly executes the script through crontab to synchronize specific files or directories between the server and the client, in this way, you do not need to manually synchronize each time, and the rsync server configuration is also very simple.

It mainly involves three files:

  1. /Etc/rsyncd. conf is the main configuration file of rsync server
  2. /Etc/rsyncd. secrets is used to log on to the rsync server to the password file.
  3. /Etc/rsyncd. motd indicates the information displayed during user logon.
Rsyncd. conf

The file content is as follows:

123456789101112131415161718192021222324252627282930313233343536373839
# Minimal configuration file for rsync daemon# See rsync(1) and rsyncd.conf(5) man pages for help# This line is required by the /etc/init.d/rsync scriptpid file = /var/run/rsyncd.pidmotd file = /etc/rsyncd.motdport = 873address = 192.168.20.24#uid = nobody#gid = nobodyuid = rootgid = rootuse chroot = yesread only = no# limit access to private LANshosts allow = *hosts deny = *max connections = 5# This will give you a separate log filelog file = /var/log/rsync.log# This will log every file transferred - up to 85,000+ per user, per sync# transfer logging = yeslog format = %t %a %m %f %bsyslog facility = local3timeout = 300[test]path = /home/lcl/testlist = yesignore errorsauth users = lclsecrets file = /etc/rsyncd.secretscomment = lcl test#exclude = samba/

Rsync uses port 873, Which is configured abovehosts allowIs to allow the use of this rsync service to the machine to the IP address,hosts denyThe IP address is rejected, and different IP addresses are separated by spaces.

[Test] and later, configure the rsync directory, including directory to path, user, password file, andexcludeExclude not to synchronize to files or directories.

Rsyncd. secrets

This is used to store the user name and password of the rsync service. It is a plaintext text file. It is very important to set the attribute to 600 and only allow the owner to read and write the file. The file format is as follows:

lcl:12345
Rsyncd. motd

File records are sent to the rsync service for welcome information. When you use this service, it is displayed and can be set to any text information, such:

+++++++++++++++++++++++++++++++++++++++ Welcome to use lcl rsync services! +++++++++++++++++++++++++++++++++++++++

After configuring the above files, you can start the rsync service:

rsync --daemon

The client can use the rsync command to synchronize data.

The Command Format of rsync can be:

  1. Rsync [OPTION]… SRC [SRC]… [USER @] HOST: DEST]
  2. Rsync [OPTION]… [USER @] HOST: src dest]
  3. Rsync [OPTION]… SRC [SRC]… DEST]
  4. Rsync [OPTION]… [USER @] HOST: SRC [DEST]
  5. Rsync [OPTION]… SRC [SRC]… [USER @] HOST: DEST]
  6. Rsync [OPTION]… Rsync: // [USER @] HOST [: PORT]/SRC [DEST]

Rsync has six different working modes:

  1. Copy the local file. This mode is enabled when the SRC and DES paths do not contain a single colon ":" separator.
  2. A remote shell program (such as rsh and ssh) is used to copy the content of the local machine to the remote machine. This mode is enabled when the DST address contains a single colon ":" separator.
  3. A remote shell program (such as rsh and ssh) is used to copy the contents of the remote machine to the local machine. This mode is enabled when the SRC path contains a single colon ":" separator.
  4. Copy files from the remote rsync server to the local machine. This mode is enabled when the SRC path information contains the separator.
  5. Copy files from the local machine to the remote rsync server. This mode is enabled when the DST path information includes the separator.
  6. List of remote machine files. This is similar to rsync transmission, but you only need to omit the local machine information in the command.

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.