To use rsync for remote synchronization, you can specify the remote server's address in the command. Here’s the general syntax:
rsync [options] source/ user@remote_host:/path/to/destination/
Example Usage
-
Syncing Local to Remote: To sync a local directory to a remote server, use:
rsync -avz /path/to/local/dir/ user@remote_host:/path/to/remote/dir/-a: Archive mode (preserves permissions, timestamps, etc.)-v: Verbose output-z: Compresses data during transfer
-
Syncing Remote to Local: To sync a remote directory to your local machine, use:
rsync -avz user@remote_host:/path/to/remote/dir/ /path/to/local/dir/
Additional Options
-
Delete: To delete files in the destination that are not present in the source, add the
--deleteoption:rsync -avz --delete /path/to/local/dir/ user@remote_host:/path/to/remote/dir/ -
Dry Run: To see what would be transferred without actually doing it, use the
--dry-runoption:rsync -avz --dry-run /path/to/local/dir/ user@remote_host:/path/to/remote/dir/
Authentication
When using rsync with a remote server, you may need to provide a password for the user account on the remote server. Alternatively, you can set up SSH keys for passwordless authentication.
Example with SSH
If you want to specify a different SSH port, you can do so with the -e option:
rsync -avz -e 'ssh -p 2222' /path/to/local/dir/ user@remote_host:/path/to/remote/dir/
Replace 2222 with your desired SSH port.
