Home

Sync Files Between Devices Using Rsync

Published

- 5 min read


Rsync is a powerful command-line utility used for syncing files between two locations. It is commonly used for backups, migrations, and mirroring data between servers or directories. Rsync uses the delta-transfer algorithm, which means it only transfers the parts of files that have changed, resulting in faster transfers and reduced bandwidth usage. In this post, we will cover the basic usage of rsync and some advanced options.

Prerequisites

A Unix-like system with rsync installed (rsync is usually pre-installed on most Linux distributions). If it’s not installed on your system, you can install it using your package manager like apt on D ebian-based systems or brew on Mac-OS etc.

Basic Usage

The basic syntax of rsync is as follows:

rsync [OPTIONS] SOURCE DESTINATION

Where SOURCE is the source directory or file, and DESTINATION is the destination directory or file.

Here are some of the most useful options commonly used with rsync:

-a: archive mode, which preserves permissions, ownership, timestamps, symlinks and copies directories recursively. -n: dry-run, which shows what files would be transferred without actually transferring them. -v: verbose output, which shows the progress of the transfer. -z: compress file data during transfer to reduce bandwidth usage. -h: human-readable output, which shows the transfer speed and size.

You can use rsync to copy files from one directory to another:

rsync -avnz source destination

Running this command will show a list of files that would be copied from the source directory to the destination directory. You can remove the -n option to actually copy the files.

rsync -avz source destination

Assuming you have a directory structure like this:

source
├── file1.txt
├── file2.txt
└── subdir
    ├── file3.txt
    └── file4.txt

Running the command above will copy the files in the source directory to the destination directory, resulting in a directory structure like this:

destination
├── file1.txt
├── file2.txt
└── subdir
    ├── file3.txt
    └── file4.txt

Note that if you run the command without the trailing / in the source directory, the parent directory will also be copied, resulting in a directory structure like the following

destination
├── source
   ├── file1.txt
   ├── file2.txt
   └── subdir
       ├── file3.txt
       └── file4.txt

Sending files to a remote system

In order to use rsync to send files to a remote system, you need ssh access to the remote system.

This post will assume you are trying to send files to an Android device. If you are trying to send files to a different system, you will need to adapt the steps below to work with your case.

{{% notice note “Using with Android” %}} In order to use rsync to send files to an Android device you need to start an ssh server on your Android device. You can do this using the following steps:

  1. Install Termux from Google Play Store.
  2. Open Termus and Install openssh using pkg install openssh
  3. Run passwd and setup a password for the root user (this is the default user and does not give you root access)
  4. Find your IP address using ifconfig and take note of the ip address assigned to wlan0 e.g. 192.168.1.5
  5. Start the ssh server using by running this command sshd. There will start the ssh server on port 8022 in the background and there will be no output of the command. {{% / notice %}}

Now you are ready to use rsync to send files to your android device.

To transfer files to your remote device, use the following command:

rsync -e "ssh -p 8022" -avz ./ root@192.168.1.5:/storage/emulated/0/books

This will copy all the files in the current directory to the books directory on your android device.

This command tells rsync to use the ssh command to connect to the remote system and use port 8022 and tells it to copy the files in the current directory to the ~/storage/books directory on the android device. This assumes that your android device’s ip address is 192.168.1.5 and assumes that you have a directory called books in your device.

Copying files from a remote system

You can use rsync to copy files from the remote system to your local system. For example to copy files from your android device to your local system, you can use the following command:

rsync -e "ssh -p 8022" -avz root@192.168.1.5:/storage/emulated/0/DCIM ./

This will copy/sync the DCIM directory and its content from your android device to the current directory on your local system.

Exclude files or directories

You can exclude files or directories from the transfer by using the —exclude option followed by the name of the file or directory to exclude. For example:

rsync -e "ssh -p 8022" -avz --exclude mysecretbook.pdf ./ root@192.168.1.5:/storage/emulated/0/books

This command will exclude the file.txt file from the transfer.

You can also use wildcard characters to exclude multiple files or directories. For example:

rsync -e "ssh -p 8022" -avz --exclude *.epub ./ root@192.168.1.5:/storage/emulated/0/books

This command will exclude all epub files from the transfer.

Delete files on the destination

You can delete files on the destination that are not present on the source by using the —delete option. For example:

rsync -e "ssh -p 8022" -avz --delete ./ root@192.168.1.5:/storage/emulated/0/books

This command will delete any files on the destination if they don’t exist in the source folder.

Continue interrupted transfers

If the transfer is interrupted for any reason, you can resume the transfer by using the —partial and —progress options. For example:

rsync -e "ssh -p 8022" -avz --partial --progress ./ root@192.168.1.5:/storage/emulated/0/books

This command will resume the transfer from where it was interrupted.

Conclusion

Rsync is a very powerful tool that you can use to copy files between local and remote systems. In this article you learnt how to use it to copy files between your laptop and your android device, but you can use it to copy files between any two systems that have ssh access to each other.