Tuesday, November 28, 2017

Traveling Picture Backup Solution 2.0 - Overcoming Android's Permission Annoyances

We just got back from cruise in the Caribbean where I shot a hefty number of both DSLR and cell phone pics. On the ship, WiFi is both pricey and sluggish, so I couldn't rely on the Cloud as a backup solution for the images. Given the close call I had on trip last year, I couldn't go without some sort of backup solution either.

That left me with copying files between various media cards. In the past, I used FolderSync to automate this procedure. However, changes in Android 6.0 vastly complicate matters, because you need to grant external permission to gain write access to an SD card. In FolderSync you can only grant write permission to one SD card, and in my scenario, I needed both SD cards writeable. So FolderSync was a no-go.

Using the file manager, I could manually copy folders to the various cards, but that was error prone to say the least.

Ultimately, Termux saved the day. Termux is a Unix command line environment, that's effortless to install rsync on (pkg install rsync should do the trick). With rsync to do the heavy lifting, and bash to do the scripting, I was able to put together an automatic backup solution that was as easy to use as FolderSync.

The only catch is that Termux has the same SD card writing limitations that FolderSync has. Heck, they're worse, because in FolderSync you can grant access to one SD card. In Termux, there appeared no way to grant permission to any cards. What saved the day was actually reading the instructions. It's not that Android doesn't give you access to the SD card, it's that it doesn't give you write access to the *whole* card. It does, however, grant you write access to a particular folder. In the case of Termux, that folder was:

  /storage/[SD CARD ID]/Android/data/com.termux/files/

When you run the termux-setup-storage command, this directory is created and permissions are setup properly. By popping my DSLR's Micro SD card into the phone and running termux-setup-storage I was able to initialize the Android/data directory structure. Once this was done, accessing my Camera's Micro SD card via a USB-C card reader allowed me write access to this 'files' directory.

Here's a screenshot of the /storage/ directory with the Phone's SD card mounted internally in the phone, and the camera's SD card mounted via a reader:

The funky looking identifiers (6EFF-8FBA and 5564-3C29) uniquely identify the micro SD card, and ultimately give you read-only access to the entire card, and write access to the specific directory noted above.

Once I had all this untangled, writing the script to rsync files around was easy:

#!/data/data/com.termux/files/usr/bin/bash

##
## Backup phone and dslr pics to each other's card
##

SD_IDS="5564-3C29 6EFF-8FBA"

skipping() {
  src_id=$1 ; shift
  dest_id=$1 ; shift
  msg="$@"
  
  echo "Skipping: $src_id => $dest_id"
  echo "  $msg"
}

for src_id in $SD_IDS ; do
  for dest_id in $SD_IDS ; do
    if [ "$src_id" = "$dest_id" ] ; then
     continue
    fi
    src_path=/storage/$src_id/DCIM
    dest_base=/storage/$dest_id/Android/data/com.termux/files
    if [ ! -d $src_path ] ; then
     skipping $src_id $dest_id "$src_path does not exist"
     continue
    fi

    if [ ! -d $dest_base ] ; then
     skipping $src_id $dest_id "$dest_base does not exist"
     continue
    fi
    dest_path="$dest_base/dcim_backup/$src_id"
    mkdir -p $dest_path

    if [ ! -d $dest_path ] ; then
     skipping $src_id $dest "Unable to create backup path $dest_path"
     continue
    fi

    echo "Backing up: $src_id => $dest_id"
    rsync -vr --size-only $src_path/ $dest_path
  done
done

Note the specific SD card identifiers at the top of the script. The script attempts to copy files between every available drive (in this case, there's only two). My plan is to add at least a third card which will server as backup only location.

Termux offers a slick widget add-on which gives you home-screen access to run arbitrary commands. Using it to launch this backup script means that I don't have to type anything at the Termux prompt to perform the backup. I need only plug in the various cards, and click on the widget:

In short: Termux rocks, rsync rocks, Android permissions are hell but deal-able, and using the command line from your phone is more than just a party trick. Now if only I could write some code that made my vacation longer...

No comments:

Post a Comment