Skip to content

Instantly share code, notes, and snippets.

@forkloop
Created May 12, 2014 00:17
Show Gist options
  • Select an option

  • Save forkloop/d8d17453b66eaf667deb to your computer and use it in GitHub Desktop.

Select an option

Save forkloop/d8d17453b66eaf667deb to your computer and use it in GitHub Desktop.
Backup Android photos
#!/usr/bin/ruby
require 'set'
require 'yaml'
# sync the Android DCIM foler periodically
PATH = '/mnt/sdcard/DCIM/Camera'
DEST = "#{ENV['HOME']}/Pictures/Android"
IGNORED_FILES = Set.new(['thumbnails'])
def check_file
res = `adb shell ls #{PATH}`
if res =~ /No such file or directory/
files = nil
else
files = res.split
end
end
def load_exist
if File.exist?("#{DEST}/manifest.yml")
manifest = YAML.load_file("#{DEST}/manifest.yml")
exist_files = Set.new manifest[:files]
else
exist_files = Set.new Dir.entries(DEST)
end
end
def main
new_files = check_file
return unless new_files
exist_files = load_exist
new_files.each do |f|
if !IGNORED_FILES.include?(f) && !exist_files.include?(f)
`adb pull #{PATH}/#{f} #{DEST}/#{f}`
exist_files << f
end
end
File.open("#{DEST}/manifest.yml", 'w') do |o|
o.write({files: exist_files.to_a}.to_yaml)
end
end
if $0 == __FILE__
main
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment