Steven Jewel Blog RSS Feed

22 Feb 2014

Moving Photos Off Android Automatically

I've been looking for a good way to automatically move photos and videos off of an android phone and onto a computer for backup, since it's a hassle to have the phone fill up.

There are lots of potential solutions to this problem. This one combines BitTorrent Sync and a little ruby script.

  1. Create a directory for your photos on your computer. I'll call this phone-photos.
  2. Create a subdirectory, phone-photos/sync.
  3. Create another subdirectory, phone-photos/archive.
  4. Add the sync directory to BitTorrent Sync on your computer.
  5. Connect this share to the DCIM/Camera folder on the android device.
  6. Run the following script daily via cron:
#!/usr/bin/env ruby

MIN_AGE = 14 * 24 * 60 * 60  # 14 days
SOURCE_DIR = "#{ENV['HOME']}/phone-photos"

require 'shellwords'
def se str
  Shellwords.shellescape str

def sync src_dir
  sync_dir = "#{src_dir}/sync"
  archive_dir = "#{src_dir}/archive"

  # Do not use --del here!
  system( "rsync -va #{se sync_dir}/ #{se archive_dir}/" )
  abort "rsync failed" unless $? == 0

  Dir.glob( "#{sync_dir}/*" ) do |path|
    next if path )
    age = - File.mtime(path)
    next if age < MIN_AGE

    # Make extra sure that the file in question exists in the archive
    # before deleting it.
    archive_path = "#{archive_dir}/#{File.basename(path)}"
    if !File.exists? archive_path
      abort "#{archive_path.inspect} does not exist!"

    if File.size( archive_path ) != File.size( path )
      abort "#{archive_path.inspect} has wrong size"

    puts "Deleting #{path}"
    File.unlink path


The script will copy everything from sync into archive. It will then delete everything older than 14 days in the sync directory. BitTorrent Sync in turn will delete these files from the phone.

The 14 day period can be adjusted to fit, or the files could be sorted by date and then removed until the photos fit within a certain size limit.

Further Thoughts

An obvious defect in this strategy is that BitTorrent Sync is closed source software, which uses its own wire encryption that is difficult to audit for security. That's why I've been working on the clearskies project.