Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy resources across hosts
06-08-2018, 03:36 PM (This post was last modified: 06-13-2018 07:07 PM by cchen.)
Post: #1
Copy resources across hosts
It's a common need to copy resources between VMs or between VM and localhost. There are two simple techniques that can accomplish this:

P1). make an archive of local dir/ on remote machine using ssh
Code:
tar -c dir/ | gzip | ssh user@remote 'dd of=dir.tar.gz'
It will prompt you for the password of remote host. A zipped directory dir.tar.gz will appear on the remote host's home directory (~/).
To unpack the zipped file:
Code:
tar -xzf dir.tar.gz

Note that tar has many useful options, for example, you can use "--exclude" option to exclude some subdirectories or files from beging packed (you can use multiple "exclude" in one line):
Code:
tar -czf backup.tar.gz --exclude=PATTERN1 --exclude=PATTERN2 ... /path/to/backup

P2). copy files between remote hosts using scp
Syntax:
Code:
scp <src> <dst>
To copy a file from B to A while logged into B:
Code:
scp <path-to-file> username@A:<path-to-destination>
To copy a file from B to A while logged into A:
Code:
scp username@B:<path-to-file> <path-to-destination>

scp also has some options available, for example, it can also be used to copy a directory from remote host with port number specified:
Code:
Drive example:
scp -P 30022 -r cchen@ada.zettadom.com:/thinker/storage/DStorage/Limbo/Packages ..
Note that -P <port#> has to be placed before -r (which tells the system to copy the whole directory)
Also, if the directory is too big, directly copying it may be very slow. It's suggested to pack it first, transfer the packed file and unpack at the destination
If the packed file is still too big, it may happen that the scp process is stalled. To resolve this issue, try to use the "-l" option to limit its bandwidth

A very important thing to remember is to add "-r" or "-R" whenever you are moving or copying a whole directory. Not remembering to do so will cause failure

For more info, please refer to rsync, which is another command that can be used to move resources remotely

RR rayluk
Find all posts by this user
Quote this message in a reply
06-08-2018, 03:53 PM
Post: #2
RE: Copy resources across hosts
(06-08-2018 03:36 PM)cchen Wrote:  It's a common need to copy resources between VMs or between VM and localhost. There are two simple techniques that can accomplish this:

P1). make an archive of local dir/ on remote machine using ssh
Code:
tar -c dir/ | gzip | ssh user@remote 'dd of=dir.tar.gz'
It will prompt you for the password of remote host. A zipped directory dir.tar.gz will appear on the remote host's home directory (~/).
To unpack the zipped file:
Code:
tar -xzf dir.tar.gz

P2). copy files between remote hosts using scp
Syntax:
Code:
scp <src> <dst>
To copy a file from B to A while logged into B:
Code:
scp <path-to-file> username@A:<path-to-destination>
To copy a file from B to A while logged into A:
Code:
scp username@B:<path-to-file> <path-to-destination>

RR rayluk

Generally good to me. Please also link rsync to this post.

Also, please move to this board ( http://tab.d-thinker.org/forumdisplay.php?fid=50 )
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: