Linux rsync Command Detailed 2__linux

Source: Internet
Author: User
Tags auth file copy file permissions rsync
For each of the above six command formats, Rsync has six different working modes:
1 Copy Local files. This mode of operation is initiated when both SRC and des path information do not contain a single colon ":" delimiter. such as: Rsync-a/data/backup
2 Use a remote shell program (such as rsh, SSH) to copy the contents of the local machine to the remote machine. This mode is started when the DST path address contains a single colon ":" Separator. such as: Rsync-avz *.c foo:src

3 Use a remote shell program (such as rsh, SSH) to copy the contents of the remote machine to the local machine. This mode is started when the SRC address path contains a single colon ":" Separator. such as: Rsync-avz Foo:src/bar/data
4 Copy files from remote rsync server to local machine. This mode is started when the SRC path information contains the "::" delimiter. such as: Rsync-av Root@172.16.78.192::www/databack
5 copy files from local machine to remote rsync server. This mode is started when the DST path information contains the "::" separator. such as: Rsync-av/databack root@172.16.78.192::www
6 The list of files for the remote machine. This is similar to rsync transmission, but simply omitting the local machine information in the command. such as: Rsync-v rsync://172.16.78.192/www
The specific explanation for the rsync parameter is as follows:
-V,--verbose verbose mode output
-Q,--quiet thin output mode
-C,--checksum Open the check switch to force the file transfer to verify
-A,--archive archive mode, which represents the transfer of files recursively and maintains all file attributes equal to-rlptgod
-R,--recursive the subdirectory in recursive mode
-R,--relative using relative path information
-B,--backup creates a backup, which means that the old file is renamed to ~filename for the same file name already exists for the purpose. You can use the--suffix option to specify different backup file prefixes.
--backup-dir store backup files (such as ~filename) in the directory.
-suffix=suffix defines the backup file prefix
-U,--update only updates, skipping all files that already exist in DST, and the file time is later than the file you want to back up. (Do not overwrite the updated file)
-L,--links retention soft link knot
-L,--copy-links to treat soft links like regular files
--copy-unsafe-links only copies links that point beyond the src Path directory tree
--safe-links ignores links to the SRC path tree
-H,--hard-links keep hard links
-P,--perms keep file permissions
-O,--owner keep file owner information
-G,--group keep file group information
-D,--devices maintain device file information
-T,--times keep file time information
-S,--sparse special handling of sparse files to save DST space
-N,--dry-run which files will be transmitted
-W,--whole-file copy files without incremental detection
-X,--one-file-system do not cross file system boundaries
-B, the block size used by the--block-size=size test algorithm is 700 bytes by default
-E,--rsh=command specifies the use of rsh, SSH mode for data synchronization
--RSYNC-PATH=PATH Specifies the path information for the rsync command on the remote server
-C,--cvs-exclude use the same method as CVs to automatically ignore files to exclude files that you do not want to transfer
--existing updates only those files that already exist in DST, not the newly created files
--delete Delete files that are not in the DST SRC
--delete-excluded also deletes the receiving end of files that are excluded by the option specified
--delete-after after transmission is over
--ignore-errors in time IO errors are also deleted
--max-delete=num Delete NUM files up to
--partial retains files that are not fully transmitted for some reason, to expedite subsequent transmissions
--force forcibly deletes a directory, even if it is not empty
--numeric-ids does not match the user and group IDs of numbers to user and group names
--timeout=time IP timeout time in seconds
-I,--ignore-times not skip those files that have the same time and length
--size-only when deciding whether to back up a file, just look at the file size without considering the file time
--modify-window=num the time stamp window that determines whether a file is in the same time, default is 0
-t--temp-dir=dir create temporary files in dir
--compare-dest=dir also compares files in DIR to determine whether a backup is required
-P equals to--partial
--progress Display backup process
-Z,--compress compression of backed-up files on transfer
--EXCLUDE=PATTERN Specifies to exclude file modes that do not require transfer
--INCLUDE=PATTERN Specifies the file mode that is not excluded and needs to be transferred
--exclude-from=file exclude files in the specified mode in file
--include-from=file does not exclude files that match the file-specified pattern
--version Print version Information
--address bound to a specific address
--CONFIG=FILE specifies a different profile and does not use the default rsyncd.conf file
--PORT=PORT Specifies the other rsync service ports
--blocking-io blocking IO for a remote shell
-stats gives the transfer status of some files
--progress transmission process in real time
--log-format=format Specify log file format
--password-file=file gets the password from file
--bwlimit=kbps limit I/O bandwidth, Kbytes per second
-H,--help display Help information
4, the use of rsync example
4.1, SSH mode
1, first start the SSH service on the service side:
# service sshd Start
Start sshd: [OK]
2, use rsync to synchronize
You can then use the rsync command on the client to back up the data on the server, which is backed up by the system user, as follows:
# RSYNC-VZRTOPG--progress-e ssh--delete work@172.16.78.192:/www/*/databack/experiment/rsync
work@172.16.78.192 ' s Password:
Receiving file List ...
5 Files to consider
test/
A
0 100% 0.00kb/s 527:35:41 (1, 20.0% of 5)
B
100% 65.43kb/s 0:00:00 (2, 40.0% of 5)
C
0 100% 0.00kb/s 527:35:41 (3, 60.0% of 5)
Dd
100663296 100% 42.22mb/s 0:00:02 (4, 80.0% of 5)
Sent bytes received 98190 bytes 11563.06 bytes/sec
Total size is 100663363 speedup is 1024.19
The information above describes the entire backup process and the size of the total backup data.
4.2, the Backstage service way
1. Start rsync Service
Edit the/etc/xinetd.d/rsync file, change the Disable=yes to Disable=no, and restart the XINETD service as follows:
# Vi/etc/xinetd.d/rsync
#default: Off
# description:the Rsync server is a good addition to a FTP server, as it \
# allows CRC Checksumming etc.
Service rsync
{
Disable = no
Socket_type = Stream
wait = no
user = root
Server =/usr/bin/rsync
Server_args =--daemon
Log_on_failure + + USERID
}
#/etc/init.d/xinetd Restart
Stop xinetd: [OK]

Start xinetd: [OK]

2. Create configuration file
After you install the Rsync program by default, the main configuration file for rsync is not created automatically, you need to create it manually, the primary profile is "/etc/rsyncd.conf", create the file, and insert the following:
# vi/etc/rsyncd.conf
Uid=root
Gid=root
Max Connections=4


Log File=/var/log/rsyncd.log
PID File=/var/run/rsyncd.pid
Lock File=/var/run/rsyncd.lock
Secrets file=/etc/rsyncd.passwd
Hosts DENY=172.16.78.0/22
[WWW]
comment= Backup Web
Path=/www
Read Only = no
Exclude=test
Auth Users=work
3. Create a password file
In this way can not use the System user authentication client, so need to create a password file, its format is "Username:password", user name can and password can be casually defined, it is best not to be consistent with the system account, at the same time to create the password file permissions set to 600, This is described in detail in the previous module parameters.
# echo "work:abc123" >/ETC/RSYNCD.PASSWD
# chmod 600/ETC/RSYNCD.PASSWD
4, Backup
By doing this, you can now back up your data as follows:
# Rsync-avz--progress--delete Work@172.16.78.192::www/databack/experiment/rsync
Password:
Receiving file List ...
6 Files to consider
./Files ...
A
0 100% 0.00kb/s 528:20:41 (1, 50.0% of 6)
B
100% 65.43kb/s 0:00:00 (2, 66.7% of 6)
C
0 100% 0.00kb/s 528:20:41 (3, 83.3% of 6)
Dd
100663296 100% 37.49mb/s 0:00:02 (4, 100.0% of 6)
Sent 172 bytes received 98276 bytes 17899.64 bytes/sec
Total size is 150995011 speedup is 1533.75
5. Recovery
When there is a problem with the server's data, then it is necessary to restore the service through the client's data, provided that the client has write permission, or the client can not be restored directly to the service side, using rsync to restore the data as follows:
# Rsync-avz--progress/databack/experiment/rsync/work@172.16.78.192::www
Password:
Building file List ...
6 Files to consider
./
A
B
100% 0.00kb/s 0:00:00 (2, 66.7% of 6)
C
Sent 258 bytes Received bytes 95.43
Total size is 150995011 speedup is 452080.87
5. Sample Scripts
These scripts here are examples of rsync sites:
1, every seven days the data to the central server to do incremental backup
#!/bin/sh
# This script does personal backups to a rsync backup server. You'll end up
# with a 7 day rotating incremental backup. The Incrementals'll go
# into subdirectories named after the day of the week, and the current
# full backup goes into a directory called ' current '
# tridge@linuxcare.com
# Directory to Backup
bdir=/home/$USER
# excludes File-this contains a wildcard pattern/line of files to exclude
excludes= $HOME/cron/excludes
# The name of the backup machine
Bserver=owl
# Your password on the backup server
Export Rsync_password=xxxxxx
########################################################################
Backupdir= ' Date +%a '
opts= "--force--ignore-errors--delete-excluded--exclude-from= $EXCLUDES
--delete--backup--backup-dir=/$BACKUPDIR-a "
Export path= $PATH:/bin:/usr/bin:/usr/local/bin
# The following line clears weeks incremental directory
[-D $HOME/emptydir] | | mkdir $HOME/emptydir
rsync--delete-a $HOME/emptydir/$BSERVER:: $USER/$BACKUPDIR/
RmDir $HOME/emptydir
# now the actual transfer
Rsync $OPTS $BDIR $BSERVER:: $USER/current
2, Backup to a free hard drive
#!/bin/sh
Export Path=/usr/local/bin:/usr/bin:/bin
list= "Rootfs usr data data2"
For d in $LIST; Todo
mount/backup/$d
Rsync-ax--exclude fstab--delete/$d//backup/$d/
umount/backup/$d
Done
day= ' Date ' +%a '
Rsync-a--delete/usr/local/apache/data2/backups/$DAY
Rsync-a--delete/data/solid/data2/backups/$DAY
3, mirror the CVS Tree of vger.rutgers.edu
#!/bin/bash
cd/var/www/cvs/vger/
Path=/usr/local/bin:/usr/freeware/bin:/usr/bin:/bin
run= ' LPs x | grep rsync | Grep-v grep | Wc-l '
If ["$RUN"-GT 0]; Then
Echo already running
Exit 1
Fi
Rsync-az Vger.rutgers.edu::cvs/cvsroot/changelog $HOME/changelog
sum1= ' Sum $HOME/changelog '
Sum2= ' Sum/var/www/cvs/vger/cvsroot/changelog '
If ["$sum 1" = "$sum 2"]; Then
Echo Nothing Todo
Exit 0
Fi
Rsync-az--delete--force vger.rutgers.edu::cvs//var/www/cvs/vger/
Exit 0
6. FAQ
Q: How to use SSH for rsync without having to enter a password.
A: You can do this by following these steps
1. Set up SSH keys on Server A by Ssh-keygen, do not specify a password, you will see the identity and identity.pub files under ~/.ssh
2. Create subdirectories in the home directory on Server B. SSH
3. Copy A's identity.pub to Server B
4. Add Identity.pub to ~[user B]/.ssh/authorized_keys
5. A user on Server A can then SSH to Server B with User B via the following command
e.g. Ssh-l UserB ServerB
This allows user A on server A to SSH to User B's identity without a password to log on to Server B.
Q: How do I use rsync through a firewall without compromising security?
A: The answers are as follows:
This usually has two kinds of situations, one is the server inside the firewall, one is the server outside the fire wall. In either case, usually using SSH, it is best to create a new backup user and configure sshd only to allow this user to enter through the RSA authentication method. If the server is inside the firewall, it is best to qualify the client's IP address and reject all other connections. If the client is inside the firewall, it is OK to simply allow the firewall to open an SSH outgoing connection to TCP port 22.
Q: Can I back up any files that have been changed or deleted?
A: Of course you can:
You can use such as: Rsync-other-options-backupdir =./backup-2000-2-13 ... Such a command to implement.
So if the source file:/path/to/some/file.c changed, the old file will be moved to./BACKUP-2000-2-13/PATH/TO/SOME/FILE.C,
The catalogue here needs to be built by hand.
Q: What ports do I need to open on the firewall to accommodate rsync?
A: Depending on the situation
Rsync can transfer files directly through a TCP connection on port 873, or through 22-port SSH for file delivery, but you can also change its port via the following command:
Rsync--port 8730 otherhost::
Or
Rsync-e ' Ssh-p 2002 ' Otherhost:
Q: How can I just copy the directory structure through rsync and ignore the file?
A:rsync-av--include ' * * *--exclude ' * ' Source-dir dest-dir
Q: Why do I always have the "Read-only file system" error.
A: see if you forgot to set "Read Only = no"
Q: Why do I appear ' @ERROR: Invalid gid ' error.
A:rsync Use the default is to run with Uid=nobody;gid=nobody, if your system does not exist nobody group, there will be such a mistake, you can try the GID = Nogroup or other
Q: Bind Port 873 What's going on with failure.
A: If you are not running this daemon as root, this error can occur because the port below port 1024 is privileged. You can use the--port parameter to change.
Q: Why I failed to authenticate.
A: From your command line, it appears:
You use the following:
> bash$ rsync-a 144.16.251.213::test Test
> Password:
> @ERROR: Auth failed on module test
>
> I dont understand this. Can somebody explain as to the acomplish this.
> All suggestions are welcome.


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.