Backing up photos from Android to Digital Ocean Spaces

Backing up family photos has always been an important task for me, especially the ones taken from my phone (which is often). I do periodic backups to an external hard drive, however, I never really found a cloud backup solution I liked and where I had control over. For a while I was backing up photos to OneDrive, but I found the cost of upgrading to an Office365 account a bit overkill. Recently over the past year I have been experimenting with Digital Ocean Spaces, which is an object storage service with S3 compatibility.

Since it is compatible with S3 and I regularly use Termux (a linux command-line emulator app), I wonder if I could schedule nightly backup jobs using the s3cmd tool.

Here are the steps I took.

Installing s3cmd and Termux API

I referenced the scripts provided in this Github repo to install the necessary dependencies for s3cmd to run properly. Here are the commands I ran on Termux.

pkg install python
pkg install termux-api
pip install s3cmd

After installing the above packages run the following command to configure s3cmd for your DO Spaces account. I referenced this document when running the command.

s3cmd --configure

Creating the sync script

***Please use caution when testing your scripts so that you do not burn through your cellular data***

I wanted any videos or photos taken on my phone each day to be synced to my DO Spaces bucket. To avoid using cell data I needed a way to ensure that my phone was connected to the wifi network. I utilized the Termux API , which offers commands that can have access to or provide hardware information of your Android device. The specific command would be termux-wifi-connectioninfo. The command prints information about your current wifi statuses.

Here is an example:

$termux-wifi-connectioninfo
{
"bssid": "xx:xx:xx:xx:xx:xx",
"frequency_mhz": 2412,
"ip": "xxx.xxx.x.xxx",
"link_speed_mbps": 78,
"mac_address": "xx:xx:xx:xx:xx:xx",
"network_id": 1,
"rssi": -42,
"ssid": "xxxxxxxxxxx",
"ssid_hidden": false,
"supplicant_state": "COMPLETED"
}

What we are looking for is the “supplicant_state” field to have a value of “COMPLETED”.

Using the results printed I wrote the below script that would check the “supplicant_state” field before running the s3cmd command. It will only run if the field value contains the word “COMPLETED”. If so, it will sync all photos and videos in the Camera folder for the current date.

#!/bin/bash

WIFISTATUS=$(termux-wifi-connectioninfo | grep supplicant)
CURRDATE=$(date +%Y%m%d)

echo $WIFISTATUS

if [[ "$WIFISTATUS" =~ "COMPLETED" ]]
then
s3cmd put /sdcard/DCIM/Camera/$CURRDATE* s3://Path/To/My/Bucket

else
echo NOWIFI

fi

Scheduling the cron job

In order to have the script run in a nightly basis I added the following entry to the crontab by running crontab -e.

0 23 * * * /data/data/com.termux/files/home/scripts/dailyphotobacknew.sh >> ~/camerasync.log

The only caveat with the above approach would be that you need to have termux running in the background in order for the script to run. Besides that, this setup has worked out well for me and all my photos and videos have been backed up to cloud storage without much of a hiccup. Once in a while if I power off my phone I have remember to open termux, but that rarely occurs.

I hope this helps any DigitalOcean and Android users out there. If you do not have a DO account and you are thinking about signing up for one, use this link to get some free credits (I will receive some credits for the referral as well).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.