В началоUnix Toolbox → 7. RSYNC
Gentoo-doc HOME Пред.: 6. VPN WITH SSHВ началоУровень выше: Unix ToolboxСлед.: 8. SUDO

7. 7. RSYNC

Rsync can almost completely replace cp and scp, furthermore interrupted transfers are

efficiently restarted. A trailing slash (and the absence thereof) has different meanings, the man page is good... Here some examples:

Copy the directories with full content:

# rsync -a /home/colin/ /backup/colin/

# rsync -a /var/ /var_bak/

# rsync -aR --delete-during /home/user/ /backup/ # use relative (see below)

Same as before but over the network and with compression. Rsync uses SSH for the transport per default and will use the ssh key if they are set. Use ":" as with SCP. A typical remote copy:

# rsync -axSRzv /home/user/ user@server:/backup/user/

Exclude any directory tmp within /home/user/ and keep the relative folders hierarchy, that is the remote directory will have the structure /backup/home/user/. This is typically used for backups.

# rsync -azR --exclude /tmp/ /home/user/ user@server:/backup/

Use port 20022 for the ssh connection:

# rsync -az -e 'ssh -p 20022' /home/colin/ user@server:/backup/colin/

Using the rsync daemon (used with "::") is much faster, but not encrypted over ssh. The

location of /backup is defined by the configuration in /etc/rsyncd.conf. The variable

RSYNC_PASSWORD can be set to avoid the need to enter the password manually.

# rsync -axSRz /home/ ruser@hostname::rmodule/backup/

# rsync -axSRz ruser@hostname::rmodule/backup/ /home/ # To copy back

Some important options:

-a, --archive archive mode; same as -rlptgoD (no -H)

-r, --recursive recurse into directories

-R, --relative use relative path names

-H, --hard-links preserve hard links

-S, --sparse handle sparse files efficiently

-x, --one-file-system don't cross file system boundaries

--exclude=PATTERN exclude files matching PATTERN

--delete-during receiver deletes during xfer, not before

--delete-after receiver deletes after transfer, not before

7.1. 7.1 Rsync on Windows

Rsync is available for Windows through cygwin or as stand-alone packaged in cwrsync7. This is very convenient for automated backups. Install one of them (not both) and add the path to the Windows system variables: # Control Panel -> System -> tab Advanced, button Environment

Variables. Edit the "Path" system variable and add the full path to the installed rsync, e.g.

C:\Program Files\cwRsync\bin or C:\cygwin\bin. This way the commands rsync and ssh are available in a Windows command shell.

Public key authentication

Rsync is automatically tunneled over SSH and thus uses the SSH authentication on the server.

Automatic backups have to avoid a user interaction, for this the SSH public key authentication can be used and the rsync command will run without a password.

All the following commands are executed within a Windows console. In a console (Start -> Run -> cmd) create and upload the key as described in SSH, change "user" and "server" as appropriate. If the file authorized_keys2 does not exist yet, simply copy id_dsa.pub to authorized_keys2 and upload it.

# ssh-keygen -t dsa -N '' # Creates a public and a private key

# rsync user@server:.ssh/authorized_keys2 . # Copy the file locally from the server

# cat id_dsa.pub >> authorized_keys2 # Or use an editor to add the key

# rsync authorized_keys2 user@server:.ssh/ # Copy the file back to the server

# del authorized_keys2 # Remove the local copy

Now test it with (in one line):

rsync -rv "/cygdrive/c/Documents and Settings/%USERNAME%/My Documents/" \

'user@server:My\ Documents/'

Automatic backup

Use a batch file to automate the backup and add the file in the scheduled tasks (Programs -> Accessories -> System Tools -> Scheduled Tasks). For example create the file backup.bat and

replace user@server.

@ECHO OFF

REM rsync the directory My Documents

SETLOCAL

SET CWRSYNCHOME=C:\PROGRAM FILES\CWRSYNC

SET CYGWIN=nontsec

SET CWOLDPATH=%PATH%

REM uncomment the next line when using cygwin

SET PATH=%CWRSYNCHOME%\BIN;%PATH%

echo Press Control-C to abort

rsync -av "/cygdrive/c/Documents and Settings/%USERNAME%/My Documents/" \

'user@server:My\ Documents/'

pause

Пред.: 6. VPN WITH SSHВ началоУровень выше: Unix ToolboxСлед.: 8. SUDO
В началоUnix Toolbox → 7. RSYNC