Back up Files from Android with rsync and Termux

This guide shows how to set up and use rsync in the Termux app to transfer files between an Android device and a computer. rsync is a utility that is used to copy files and make backups on Unix and Linux systems. Because Android is based on Linux, it makes sense to use rsync for file and photo backups.

If you don't want to install the Termux app, I also have another guide that runs rsync in the Android Debug Bridge (adb) shell instead: Back up Files from Android with rsync and adb

Created
June 17, 2023
A screenshot of a terminal window running the rsync client and a screenshot of Android running the Termux app

Overview

This guide installs rsync in the Termux app on the Android device and uses an rsync client on the computer to transfer files between the Android device and the computer over SSH.

This method can copy or backup files that are stored on the internal storage of the Android device and accessible in the Files app, such as photos, videos, and downloaded files. It can't back up Android applications or application data.

Prerequisites

In this guide, I use:

A computer
I used a computer with Fedora Workstation 38 as the operating system, but it is possible to use another Linux distribution, Unix system, macOS, and maybe even Windows. The computer needs to have an rsync client and an SSH client installed.
Android device
This device stores files and photos that I want to back up to the computer. It can also be used the other way around — as a destination for files from the computer. I used the Google Pixel 4a phone with Android 13.
Network
The computer needs to be able to open an SSH connection to the Android device over the network. I used a home LAN.

Internet access is needed only to download the Termux app and to install packages in it. The rsync file transfer itself can use the local network and doesn't access the Internet.

Step 1: Install rsync and SSH Client on the Computer

Install rsync and SSH client on the computer, if they are not already installed. On Fedora Linux, rsync and SSH are most likely already preinstalled. If not, rsync can be installed with this command:

sudo dnf install rsync

And an SSH client can be installed with this command:

sudo dnf install openssh-clients

Step 2: Install Termux App on the Android Device

The Termux app is a terminal emulator for Android. It provides a Linux environment with many popular Linux utilities available. Termux doesn't need any kind of virtualization, and it doesn't require the Android device to be rooted. Programs installed in Termux run on Android's Linux kernel and in Android's file system. The Termux app's homepage is https://termux.dev.

Because recent Android versions increased security requirements with regard to storage access and executing code within applications, the Termux app isn't available on the Play Store anymore. The Play Store has an old, abandoned version of Termux that shouldn't be used. Instead, Termux can be installed from F-Droid, which is a repository of open-source software and allows applications to target old Android APIs.

Install Termux from F-Droid. First, install the F-Droid app (F-Droid.apk), then install Termux from the F-Droid app. Once Termux is installed, open it:

A screenshot of the Termux app when opened for the first time

In Termux, update the installed packages. I answer "yes" to any prompt that asks me whether I want to update configuration files.

pkg upgrade

It may be a good idea to run the upgrade command once again after the updates are installed just in case the repository configuration was updated as a part of the first upgrade. I also exit and reopen Termux after installing updates to make sure that the shell doesn't use outdated configuration.

Step 3: Enable Storage Access in Termux

Next, enable storage access in Termux:

termux-setup-storage

Android will ask you if you want to grant storage access to the Termux app. The command creates a new ~/storage folder with symbolic links to various folders in the Android storage. Now is a good time to check the symlinks inside the ~/storage directory to see where they point. In my case, Android stores the user files in /storage/emulated/0/.

A screenshot of the Termux app running ls -la on the storage folder and showing the targets of its symbolic links

Step 4: Install rsync in Termux

Install rsync in Termux:

pkg install rsync

Step 5: Start SSH Server in Termux

Install OpenSSH in Termux. This will also generate SSH keys.

pkg install openssh

Change the default SSH port. The Termux app doesn't have permission to use the default SSH port 22, so use port 60022 instead. Edit the ../usr/etc/ssh/sshd_config file:

nano ../usr/etc/ssh/sshd_config

Edit or add a line with the port number:

Port 60022

Save the file and exit nano with Ctrl+X.

Next, find the IP address of the Android device. The IP address is 172.27.27.193 in my case.

Create a user password in Termux:

passwd

Start the SSH daemon:

sshd -D

The -D option tells the SSH daemon to run in the foreground. The daemon can then be terminated with Ctrl+C. Without this option, the daemon runs in the background and can be killed with a pkill sshd command.

A screenshot of the Termux app running the SSH daemon

When the SSH daemon in Termux is running, you can SSH to the Android device from the computer. Enter the IP address of the Android device as the hostname. Termux will accept any user name, but you have to correctly enter the password that you set a moment ago.

ssh -p 60022 172.27.27.193
A screenshot of a terminal window with an open SSH connection to the Termux environment

To avoid having to type the password every time you want to open an SSH connection, you can set up key-based SSH authentication. First, generate a new key on the computer, if a key doesn't already exist.

ssh-keygen -t ed25519

Copy a public key from the computer to the Termux session on the Android device:

ssh-copy-id -p 60022 172.27.27.193

Step 6: Back up Files with rsync

Run rsync from the computer and specify the Android (Termux) device as the source. This command uses SSH to connect to the Termux instance on the Android device, and it executes the rsync command in Termux and receives its data. Because of SSH, the network transfer is encrypted.

First, I store the IP address of the Android device in a variable so that I don't have to type it in the commands:

ipaddr=172.27.27.193

I use this command to back up the internal storage of the Android device into a folder named Internal/ on my computer:

rsync -anv --delete --exclude='/Android' -e 'ssh -p 60022' ${ipaddr}:/storage/emulated/0/ Internal/

The command only prints the files that would be transferred. Remove the option n from -anv to actually transfer the files.

On my old Motorola phone, I used this command to copy files from the SD card to the Samsung SD card/ directory on my computer:

rsync -anv --delete --exclude='/Android' --exclude='/.android_secure' -e 'ssh -p 60022' ${ipaddr}:/storage/0000-0000/ Samsung\ SD\ card/

If the SD card is formatted with exFAT and rsync gives permission errors, replace -a with -vrtlD in the rsync command.

Don't forget to change the commands for your situation and remove the -n option to actually copy anything! You can swap the source and destination to copy files from the computer to the Android device.

A screenshot of a terminal window running the rsync client

This is how I use rsync to periodically backup my files while checking that there is no issue with the files, in this order (paths omitted for brevity):

  1. First, do a dry run to see which files would be copied or deleted. This command only prints the files and doesn't copy or delete anything.

    rsync -anv --delete
  2. Then, copy only new files that don't exist in the destination. First, print the files, then copy them.

    rsync -anv --ignore-existing
    rsync -a --ignore-existing
  3. After that, update files that already exist in the destination.

    rsync -anv
    rsync -a
  4. Finally, delete files in the destination if they don't exist in the source.

    rsync -anv --delete
    rsync -a --delete
  5. By default, rsync uses the file size and the time stamps to detect if two files differ. To verify that the destination and source files are exactly the same bit-by-bit, I run:

    rsync -anvci --delete

Step 7: Exit Termux

When you are done, stop the SSH daemon in Termux by pressing Ctrl+C if it runs in the foreground, or by running pkill sshd if it runs in the background. Otherwise, the SSH daemon could be running even when the Termux app is closed.

Exit Termux by typing exit or by pressing Ctrl+D.

Next Time Run

Because everything is now configured, these are the only steps that need to be done the next time you want to do a backup:

  1. Open Termux on the Android device.

  2. Find the IP address of the Android device:

    ifconfig
  3. Start the SSH daemon in Termux:

    sshd -D
  4. And then run the rsync command from the computer with the IP address of the Android device used as a source or as a destination host.

  5. After the backup is complete, terminate the SSH daemon (Ctrl+C) and exit Termux (Ctrl+D).