Backup your photos on your Android phone

I will assume the following:

  1. you already have termux installed on your Android phone.
  2. you have SSH access to a linux server

To backup your photos, we will go through those steps:

  1. Install dependencies
  2. Prepare the environment
  3. Setup the sync program

Install dependencies

In this first part, we will install the program we will need, namely cron which is used to automate tasks that will execute at a certain time and rsync which is the sync program. To do this, open up Termux and type the following commands:

~/$ pkg install cronie rysnc
Checking availability of current mirror: ok
Reading package lists... Done
Building dependency tree... Done
The following additional packages will be installed:
  krb5 ldns libdb libedit liblz4 libpopt openssh
  openssl openssl-tool termux-auth zstd
Suggested packages: termux-services
The following NEW packages will be installed: cronie krb5 ldns libdb libedit liblz4 libpopt
  openssh openssl-tool rsync termux-auth zstd The following packages will be upgraded:
  openssl 1 upgraded, 12 newly installed, 0 to remove and 41 not upgraded.
Need to get 4628 kB of archives.
After this operation, 18.0 MB of additional disk space will be used.
Do you want to continue? [Y/n]
 y
Get:1 https://termux.mentality.rip/termux-packages-24 stable/main aarch64 openssl aarch64 1.1.1l [1150 kB]
Get:2 https://termux.mentality.rip/termux-packages-24 stable/main aarch64 cronie aarch64 1.5.7 [49.3 kB]
Get:3 https://termux.mentality.rip/termux-packages-24 stable/main aarch64 libdb aarch64 18.1.40-2 [471 kB]
...
..

Prepare the environment

In this stage, we will need to look after 2 things:

  1. termux needs to have access to your pictures which it doesn’t have by default. To change this:
    settings -> apps -> permission -> permission -> storage -> termux -> allow
    
  2. setup the mechanism known as RSA authentication that will allow the sync program to securely transfer photos onto your server without you having to type your password:
~/$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/data/data/com.termux/files/home/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /data/data/com.termux/files/home/.ssh/id_rsa
Your public key has been saved in /data/data/com.termux/files/home/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:plowPA4fLXd7QVQFsEjNwzR3bHa3gsGrqpL0nn+dueN u0_a292@localhost
The key's randomart image is:
+---[RSA 3072]----+
|          .oo.++.|
|         . .o.o.=|
|         o...= .o|
|        o * + *..|
|        SX . . o.|
|      ..+ O   =..|
|      .+   = S  +|
|     .. = . . =..|
|    .=     =.Z=..|
+----[SHA256]-----+
~/$ ssh-copy-id username@example.com
/data/data/com.termux/files/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/data/data/com.termux/files/home/.ssh/id_rsa.pub"
The authenticity of host 'rick.kerjean.me (116.203.202.210)' can't be established.
ED25519 key fingerprint is SHA256:LVtQRuU18v7sFCc5P365iUt1U+YApmt9r+eeKx9cYgA.</br> This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?
yes
/data/data/com.termux/files/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/data/data/com.termux/files/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@rick.kerjean.me's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'username@example.com"
and check to make sure that only the key(s) you wanted were added.

Sync setup

Finally, let’s setup the cron:

~/$ # run the cron daemon
~/$ crond
~/$ # actual setup of the cron
~/$ crontab -l
0 * * * * rsync -avz /storage/emulated/0/DCIM/ username@example.com:~/incoming/

The way I have it setup, the sync is running every hour but if that’s not what you want, you can use this cron calculator to find the right syntax for the timing you want to use