Before rsync
existed here's a trick that is worth knowing. Sometimes you find yourself on a system without rsync.
The trick is to make a tar file of the files, copy the file to the new server, then extract the tar file with -p
to preserve the file permissions.
"But wait!" you ask. "That temporary file might be huge! Bigger than the free space that I have!" Don't worry. You can write connect the two tar
s by a pipe and you don't need a temporary file, nor the disk space! To specify stdin or stdout specify the file name -
(hyphen).
Putting that all together:
cd $SRCDIR
tar -c -v -f - . | ssh $DEST_HOST "mkdir -p $DESTDIR && cd $DESTDIR && tar -x -p -f -"
For example:
cd /home/user12
tar -c -v -f - . | ssh other.example.com "mkdir -p $DESTDIR && cd /home/user12 && tar -x -p -v f -"
The &&
is like ;
(semicolon) but it means "only execute the next command if this one was successful."
Remember that you have to do this as root if you want to copy the file owner:group too. Your normal user account usually can't chown
a file, so neither can tar. The command will work even if ssh
is going to ask for a password because ssh
is smart enough to not connect the pipe until the login is complete.
Note: tar
will update the permissions on all files except the root directory because it didn't create it. You created it with the mkdir -p
. You can fix this if you use tar to capture the directory instead of ".". Here's the last example repeated in a way that will update the permissions on "user12":
cd /home
tar -c -v -f - user12 | ssh other.example.com "mkdir -p /home && cd /home && tar -x -p -v f -"
See the difference?