Common scenarios: I want to synchronize some php files under/myweb, but I don't want to copy some log files in/myweb/log/because these files are too large, backup is meaningless. Now, if you want to avoid adding-exclude directly to a path, such as-exclude "./log"-exclude './log/file'
Note: the directory path is relative to the folder you are backing up.
Exclude specified directories or files during rsync backup
To exclude the specified directory, you can use the-exclude-from command in the client command line.
For example
Rsync-vzrtopg-progress-delete-exclude-from =/home/pcfile user@11.22.33.44/back/xxx
In a pcfile, each record occupies one row and supports wildcards.
/Tmp # exclude the root directory named tmp
. [A-z] * ## do not back up hidden files starting with a point
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 & quot; 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.
3
-Stats: Adds a little more output regarding the file transfer status.
4
-Progress: shows the progress of each file transfer. Can be useful to know if you have large files being backup up.
Of course, you can also specify the directory through rsync synchronization, as shown below:
Rsync uses the-include and-exclude parameters to synchronize the specified directory, and The-exclude directory is placed behind it.
For example:
File Directory on linux
Ls/usr/local/apache/htdocs/site
Aa
Bb
Cc
Dd
Ee
..
Zz
Only the directories aa, bb, and ee are to be synchronized.
Implementation code:
/Usr/local/rsync/bin/rsync-azv -- include "aa/" -- include "bb/" -- include "ee" -- exclude "/*" -- progress rsync: // 127.0.0.1/site/work/backup/htdocs_bak/site
Note:
-Include "aa/"-include "bb/"-include "ee" contains the directory you want to synchronize-exclude contains all non-synchronous directories specified.
-Exclude "/*" excluding the-include synchronization Directory, exclude other directories
Rsync: // 127.0.0.1/site: directory to be synchronized for your rsync server
/Work/backup/htdocs_bak/site is your client Directory