rsync - Linux Security Cookbook - Recipe 6.3 Copying Files Remotely

来源:互联网 发布:中国高尔夫网络电视台 编辑:程序博客网 时间:2024/06/05 16:10
 

rsync - Linux Security Cookbook - Recipe 6.3 Copying Files Remotely

Recipe 6.3 Copying Files Remotely

6.3.1 Problem

You want to copy files securely from onecomputer to another.

6.3.2 Solution

For one file:

$ scp myfile remotehost:
$ scp remotehost:myfile .

For one file,renamed:

$ scp myfile remotehost:myfilecopy
$ scp remotehost:myfile myfilecopy

For multiple files:

$ scp myfile* remotehost:
$ scp remotehost:myfile/* .

To specify another directory:

$ scp myfile* remotehost:/name/of/directory
$ scp remotehost:/name/of/directory/myfile/* .

To specify an alternate username for authentication:

$ scp myfile smith@remotehost:
$ scp smith@remotehost:myfile .

To copy a directory recursively (-r):

$ scp -r mydir remotehost:
$ scp -r remotehost:mydir .

To preserve file attributes (-p):

$ scp -p myfile* remotehost:
$ scp -p remotehost:myfile .

6.3.3 Discussion

The scp commandhas syntax very similar to that of rcp or even cp:

scp name-of-source name-of-destination

Asingle file may be copied to a remote file or directory. In other words, ifname-of-source is a file, name-of-destinationmay be a file (existing or not) or a directory (which must exist).

Multiple files and directories, however, may be copied onlyinto a directory. So, if name-of-source is two or more files,one or more directories, or a combination, then specifyname-of-destination as an existing directory into which the copywill take place.

Both name-of-source andname-of-destination may have the following form, inorder:

  1. The username of the account containingthe file or directory, followed by "@". (Optional; permitted only if a hostnameis specified.) If omitted, the value is the username of the user invokingscp.

  2. The hostname of the host containing thefile or directory, followed by a colon. (Optional if the path is present.) Ifomitted, the local host is assumed.

  3. The path to the file ordirectory. Relative pathnames are assumed relative tothe default directory, which is the current directory (for local paths) or theremote user's home directory (for remote paths). If omitted entirely, the pathis assumed to be the default directory.

Although each of the fields is optional, you cannot omit themall at the same time, yielding the empty string. Either the hostname (item 2) orthe directory path (item 3) must be present.

Whew! Once you get the hang of it, scp is pretty easy touse, and most scp commands you invoke will probably be pretty basic. Ifyou prefer a more interactive interface, try sftp ,which resembles ftp.

If you want to "mirror" a set of files securely betweenmachines, you could use scp -pr, but it has disadvantages:

  • scp follows symbolic links automatically, which youmight not want.

  • scp copies every file in its entirety, even if theyalready exist on the mirror machine, which is inefficient.

A better alternative is rsync with ssh, which optimizes thetransfer in various ways and needn't follow symbolic links:

$ rsync -a -e ssh mydir remotehost:otherdir

Add -v and —progress for more verbose output:

$ rsync -a -e ssh -v --progress mydir remotehost:otherdir

[相关问题]

全局常用配置说明

模块常用配置说明

客户端常用参数

for Windows (cygwin)

远程shell模式和rsync守护进程模式

22.6. File Synchronization. Building Internet Firewalls, 2nd Edition

Hack 92 Mirroring Web Sites with wget and rsync. Spidering Hacks

Linux Security Cookbook - Recipe 1.16 Integrity Checking with rsync

Linux Security Cookbook - Recipe 1.6 Remote Integrity Checking

Linux Security Cookbook - Recipe 6.3 Copying Files Remotely

原创粉丝点击