Wirelessly syncing photos and videos from your iPhone to your Ubuntu home server

I have been trying unsuccessfully to setup automatic wireless sync of photos and videos from our iPhones to my home server running Ubuntu. After failures with Plex camera roll upload which does not seem to work consistently, I have finally found a good alternative. The method is inefficient but has been reliable for the last two months.

It involves setting up camera backup from your iPhone to OneDrive through the OneDrive app and then syncing down the camera roll from OneDrive to your home server using rclone. You can use other cloud services like Amazon, Google or Dropbox as well based on your preference – I prefer OneDrive as the value of Office 365 Home + 1 TB storage for 6 users works great for me. You don’t have to pay for the subscription of course, in which case you just have to manage the free storage aggressively.

Here’s the setup – download the OneDrive app for your phone and go to Settings > Camera Upload and turn it on so it uploads to the account of your choice. You can keep the app open and check the camera upload progress to ensure your photos and videos get uploaded faster for the first sync.

Then on your home server, download and setup rclone which is an excellent equivalent of rsync for cloud providers. Setting up rclone to sync to OneDrive is very straightforward using rclone config and following the directions. You can verify that you’ve set it up correctly by running rclone lsd onedrive: where onedrive is the name you gave to the remote you configured in rclone. If your configuration was successful, you should see a listing of OneDrive folders and files.

After that, you can use the rclone copycommand to copy your OneDrive upload folder (in my case it is called Skydrive camera roll – but it might be just Camera roll for newer accounts). You should use the --dry-run option to check that this will do what you want. For example, this is the command I used to check what the rclone copy would do –

rclone copy "onedrive:SkyDrive camera roll" /storage/Pictures/MyiPhone/OneDrive/ --dry-run

After verifying that everything works as expected, the final step is to setup a daily cron job to do this everyday automatically. Here is the base script I use for doing the sync from OneDrive (called rclone_sync.sh):

#!/bin/bash

function mailuser () {
    mailto=$1
    mailsubject=$2
    mailbody=`/usr/bin/awk -f /home/me/rclone_parse.awk "$3"`
    /bin/cat <<EOF | /usr/sbin/ssmtp $mailto
To: $mailto
Subject: $mailsubject

$mailbody

EOF

}


SRC_DIR="$1"
DST_DIR="$2"
LOG_FILE="/tmp/$1_rclone.txt"

rclone copy "$1" "$2" --progress --log-level INFO --log-file "$LOG_FILE"

rc=$?

if [ $rc -ne 0 ]; then
    mailuser "[email protected]" "Sync of $SRC_DIR failed with $rc" "$LOG_FILE"
else
    mailuser "[email protected]" "Sync of $SRC_DIR succeeded" "$LOG_FILE"
fi

And here is the simple AWK script to parse the output logs from rsync for creating the daily email summary:

#!/usr/bin/awk

BEGIN { copied_new = replaced_existing = 0 }

/Copied \(new\)/ {
    copied_new++
}

/Copied \(replaced existing\)/ {
    replaced_existing++
}

END {
    total_operations = copied_new + replaced_existing
    print "New Files      : " copied_new
    print "Modified Files : " replaced_existing
    print "Total Files    : " total_operations
}

To use these, simply create one shell script per user/camera folder you want to sync daily that calls rclone_sync.sh with the correct source (remote folder) and destination (local folder) and then schedule them using cron.

This setup has worked very well for me – it addresses several problems like managing storage on our iPhones (we can just delete older videos when we’re running low on storage space without worrying about whether we saved it elsewhere or not), full resolution photo and video backups to the home server automatically, availability of the photos and videos we take on our phones in Plex and our home network for easy access on a variety of devices.

Leave a Reply

Your email address will not be published. Required fields are marked *