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
tars by a pipe and you don't need a temporary file, nor the disk space! To specify stdin or stdout specify the file name
Putting that all together:
tar -c -v -f - . | ssh $DEST_HOST "mkdir -p $DESTDIR && cd $DESTDIR && tar -x -p -f -"
tar -c -v -f - . | ssh other.example.com "mkdir -p $DESTDIR && cd /home/user12 && tar -x -p -v f -"
&& 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.
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":
tar -c -v -f - user12 | ssh other.example.com "mkdir -p /home && cd /home && tar -x -p -v f -"
See the difference?