I have a question about rsync. I have two servers. I would like to run a rsync command on server 1 that synchronizes all data/files on server 2 to my server. Say I have on server 2 a folder /var/www/html/hello/* All files should be synchronized with the permissions from server 2 to server 1 but the command must be executed on server 1. How can I do this with rsync ?
2 Answers
rsync -ai user2@server2:/var/www/html/hello/ local/directory/hello
This would connect using SSH to server2
as user user2
. This user must have access to all the files and directories that will be copied. The directory contents of /var/www/html/hello/
will be replicated under local/directory/hello
on the local machine.
Note the trailing /
on the source directory path. Without this, you would put the files and directories into a directory called hello
in the destination folder.
If you add --delete
, you would additionally remove local files in the destination directory that are not available on the source side.
The -i
option turns on a itemisation of the things that are getting transferred. Some people like using -v
instead of -i
. The -a
option is the option that preserves as much as possible of permissions and other meta-data, and also enables recursive replication of files in subdirectories.
rsync behaves like a simple copy programm. So syncing from remote is like (without additional options like recourse or permissions ...)
rsync remoteuser@remoteserver:/var/www/html/hello/* /to/local/folder/
This will copy the remote files - not folders to your local folder.
Use the options -avz to archive a kind of backup (include permissions and so on):
rsync -avz remoteSource localDestination
For more options see man page ...
Edit 1:
Please consider first comment from kusalananda about using the tailing *
at source folder.
-
Note that by using
*
, you will skip transferring any hidden files. Usersync -a --exclude='*/' remote@remote:/var/www/html/hello/ local/folder
to transfer all the files in that single directory.– Kusalananda ♦Commented Apr 2, 2019 at 13:48