How does rsync exclude the specified folder for file synchronization?

Source: Internet
Author: User

In rsync synchronization settings, if we do not want to synchronize a folder, we can use exclude to exclude the specified folder. Next, I will introduce you to the specific implementation method.


Very common: I want to synchronize/usr/boot/Under/, but I don't want to copy/proc/tmp folders.

If you want to avoid a specific path, add-exclude directly.

For example,-exclude "proc"

-Exclude 'Sources'

Note: the directory path is relative to the folder you are backing up.

Note: This path must be a relative path, not an absolute path.
Example: The source server/home/yjwan/bashshell has a checkout folder.

[Root @ CentOS5-4 bashshell] # ls-dl checkout

Drwxr-xr-x 2 root 4096 Aug 21 checkou

What should I do if I want to completely avoid copying the contents of this folder?

Target Server execution


Rsync-av -- exclude "checkout" yjwan@172.16.251.241:/home/yjwan/bashshell/tmp
The folder will not be copied.

[Root @ free/tmp/bashshell] # ls-d/tmp/bashshell/checkout

Ls:/tmp/bashshell/checkout: No such file or directory
Note:
1. In fact, the system treats files and folders equally. If checkout is a file, it will not copy
2. If you want to avoid copying the content in checkout, you can write-exclude "checkout/123"
3. Do not enter an absolute path like-exclude "/checkout ".

This will not prevent checkout from being copied.

For example

[Root @ free/tmp/bashshell] # rsync-av-exclude "/checkout" yjwan@172.16.251.241:/home/yjwan/bashshell/tmp

Refreshing ing file list... Done

Bashshell/checkout/
4. You can use wildcards to avoid content that you do not want to copy.

For example,-exclude "fire *"

All files or folders with fire headers will not be copied.

5 if you want to avoid copying too many files, you can write

-- Exclude-from =/exclude. list
Exclude. list is a file placed in the absolute path/exclude. list. To avoid problems, it is best to set it to an absolute path.
The content must be written as a relative path.
For example, if I want to avoid the checkout folder and fire headers
Write/exclude. list

Checkout

Fire *

Then run the following command. Note that you can write either-exclude-from or-exclude-from =.

But cannot be-exclude

Rsync-av-exclude-from = "/exclude. list" yjwan@172.16.251.241:/home/yjwan/bashshell/tmp

Check Result: the checkout folder and fire header files are indeed avoided.

Q: How can I calculate and compare the number of copied files?
1. Check the error log to see if there is a problem during replication.

2. You can know the total number of files and folders on the source server.

Ls-AlR | grep "^ [-d]" | wc

Then, the target server calculates the number of times

Check if the number is correct.

No, what's going on?

3. Now the problem is: if I use the-exclude parameter, it will be troublesome.

How do I know how many files to copy?

First of all, we mentioned in the previous command that there is only a source address and no target address. This method can be used to list all the files to be copied.

You can use this command to calculate the number of files and folders under the/root/bashshell file.

Run

[Root @ CentOS5-4 bashshell] # rsync-av/root/bashshell/| grep "^ [-d]" | wc

62 310 4249

The result is consistent with that obtained by ls.

[Root @ CentOS5-4 bashshell] # ls-AlR | grep "^ [-d]" | wc

62 558 3731

Therefore, for example, if you do not want to create a file with the fire header, you can calculate the file to be copied on the server.

[Root @ CentOS5-4 bashshell] # rsync-av-exclude "fire *"/root/bashshell/| grep "^ [-d]" | wc

44 220 2695

Then copy it.

Check that the number of files and folders on the target machine is

[Root @ free/tmp] # ls-AlR/tmp/bashshell/| grep "^ [-d]" | wc

44 396 2554

We can know that the two are synchronized.

Problem: several other common parameters of Rsync
1

-Z-compress file data during the transfer

-Compress-level = NUM explicitly set compression level

-Skip-compress = LIST skip compressing files with suffix in LIST

For compressed transmission, if the network bandwidth is not enough, it should be compressed for later transmission, which consumes Of course machine resources. However, if the number of files transmitted over the Intranet is not large, this parameter is unnecessary.

2

-Password-file = FILE

As mentioned above, this parameter can be used only when the remote machine is an rsync server.

If you think that the FILE is written with the ssh login password, it is a big mistake. Many people have made this mistake.

-Stats: Adds a little more output regarding the file transfer status.


-Progress: shows the progress of each file transfer. Can be useful to know if you have large files being backup up.

About this parameter:

I frequently find myself adding the-P option for large transfers. It preserves partial transfers in case of interuption, and gives a progress report on each file as it's being uploaded.

I move large media files back and forth on my servers, so knowing how long the transfer has remaining is very useful.


How can we calculate and compare the number of copied files?

Check the error log to see if there is a problem during replication.
You can know the total number of files and folders on the source server.
Ls-AlR | grep "^ [d]" | wc
Then, the target server calculates the number of times
Check if the number is correct.
No, what's going on?
The problem is that if I use the exclude parameter, it will be troublesome.

How do I know how many files to copy?

First, we mentioned in the preceding command that only the source address has no target address. This method can be used to list all the files to be copied.

You can use this command to calculate the number of files and folders under the/root/bashshell file.

Run

[Root @ CentOS bashshell] # rsync av/root/bashshell/| grep "^ [d]" | wc

The result is consistent with that obtained by ls.
[Root @ CentOS bashshell] # ls AlR | grep "^ [d]" | wc

Therefore, for example, if I do not want to use the fire header, I can calculate the file to be copied on the server first.
[Root @ CentOS bashshell] # rsync av exclude "fire *"/root/bashshell/| grep "^ [d]" | wc

Then copy it.
Check that the number of files and folders on the target machine is
[Root @ free/tmp] # ls AlR/tmp/bashshell/| grep "^ [d]" | wc

You can see that the synchronization is performed.

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.