How to Transfer Files with Rsync over SSH


rsync files

In this guide we will show you how to How to Transfer Files with Rsync over SSH step by step, so let us start!

What is Rsync?

Rsync (Remote Sync), is the most used command line tool for transfer and synchronize files and directories remotely as well as locally in Linux and Unix systems.

It’s widely used by Systems Administrators for data mirroring, performing backups, or migrating files to other servers across disks and networks.

This tool is fast and efficient, it can copy or transfer only the file changes from the source and offers a lot of customization options, will explain it later.

Installing Rsync

If you don’t have rsync installed on your system, you can easily install it using your distribution’s package manager, follow this steps for your Linux distribution.

For Ubuntu or Debian, install rsync using apt:

sudo apt install rsync

For Centos and Fedora, install rsync using yum:

sudo yum install rsync

Rsync Syntax

Lets start with the rsync command basic syntax for copying files:


1. Local to Local:  rsync [OPTION]... [SRC]... DEST

2. Local to Remote: rsync [OPTION]... [SRC]... [USER@]HOST:DEST

3. Remote to Local: rsync [OPTION]... [USER@]HOST:SRC... [DEST]

Rsync Options

There is quiet a bunch of options available for rsync, I will just enlist the ones I use more often, you can list all by typing: rsync -h

 -a, --archive, archive mode, equivalent to -rlptgoD. This option tells rsync to syncs directories recursively, transfer special and block devices, keeps symbolic links, modification times, groups, ownership, and permissions. I use it all the time.

-v, --verbose, Verbose output. Displays the details of the transfer.

-z, --compress, This option forces rsync to compresses the data as it is sent to the destination machine. Use this option only if the connection to the remote machine is slow.

-P, equivalent to --partial --progress. When this option is used, rsync shows a progress bar during the transfer and keeps the partially transferred files. It is useful when transferring large files over slow or unstable network connections.

-q, --quiet, Use this option if you want to suppress non-error messages.

--exclude=PATERN, exclude files matching PATTERN. So they are skipped.

Rsync example for transfer local files

The following command will transfer, or sync, all the files of from one directory to a different directory on the same machine.

In this example, /root/myfiles contains some files and you want that directory to be copied inside the /tmp/myfilesbackup folder:

lag@core:~$ rsync -av /root/myfiles /tmp/myfilesbackup

sending incremental file list

created directory /tmp/myfilesbackup/myfiles
myfiles/
myfiles/file1.txt
myfiles/file2.txt
myfiles/file3.txt

sent 263 bytes  received 118 bytes  762.00 bytes/sec
total size is 0  speedup is 0.00

If the destination directory doesn’t exist, rsync will create it.

Note: Notice that rsync gives different treatment to the source directories with a trailing slash (/). If the source directory has a trailing slash, the command will copy only the directory contents to the destination directory. When the trailing slash is omitted, rsync copies the source directory inside the destination directory.

Example of local to remote file transfer over SSH with Rsync

In the following example, we are transferring a directory from a local to a remote machine:

lag@core:~$ rsync -av backup.tar.gz root@192.168.1.100:/backups/

lag@192.168.1.100's password:

sending incremental file list
backup.tar.gz

sent 44.76M bytes  received 31 bytes  1.28M bytes/sec
total size is 16.18M  speedup is 1.10ia/

If you haven’t set a passwordless SSH login to the remote machine, you will be asked to enter the user password. For using rsync to transfer files over ssh it’s recommended to setup your SSH Keys.

Rsync Example of remote to local file transfer / sync

To transfer data from a remote to a local machine, use the remote location as a source.

This command will help you sync a remote directory to a local directory :

lag@core:~$ rsync -av lag@192.168.1.100:/home/lag/mydir /home/lag

lag@192.168.1.100's password:

receiving incremental file list
created directory /home/lag/mydir

mydir/
mydir/nagios.tar.gz
mydir/exim.centos.i386.rpm
mydir/mod_ssl.centos.i386.rpm

sent 81 bytes  received 10.99M bytes  833K bytes/sec
total size is 10.99M  speedup is 1.00

Transfer files with Rsync over SSH example with exclusions

The --exclude option allows you exclude files and directories in the rsync command, it uses relative paths to the source directory. Lets see an example:

rsync -av --exclude={'*/error_log','*/.Trash/*','*.log'} /home/username root@1.1.1.1:/home

In the above rsync command example, were are syncing from a local folder to a remote machine. It will exclude the ‘error_log’ file from all the /home/user subdirectories, also it will exclude any folder named ‘.Trash’ and any file that has the ‘log’ extension.

Conclusion

Rsync is a versatile tool for any Systems Admin, it helps you to keep your servers backups updated and to easily copy files for migrations.

If you to want learn more about this tutorial or have any questions, feel free to send your comments down below.

Don’t forget to check our other Tutorials, we are constantly submitting new ones every week.

Tags: , ,

Buy us a Coffee

Categories

Recent Posts

RSS Other Tutorials