AR create_with

I knew there was AR’s find_or_create_by  method but I didn’t know it had a very useful companion create_with .

This will either find a record  SomeModel.where(foo: "bar")  or create it with SomeModel.create(foo: "bar", some: "stuff") . Very useful.

Classic Programmer Paintings

This is a great idea. 😂

http://classicprogrammerpaintings.tumblr.com/post/142603266504/engineering-manager-handing-out-tasks-to-interns

http://classicprogrammerpaintings.tumblr.com/post/142602959384/sysadmin-grants-sudo-privileges-to-developer-on

http://classicprogrammerpaintings.tumblr.com/post/142586036029/junior-programmer-learns-git-rebase

http://classicprogrammerpaintings.tumblr.com/post/142344840164/developer-looking-at-production-logs-after-a

http://classicprogrammerpaintings.tumblr.com/post/131118350394/army-of-contractors-migrating-to-microservices

Backup And Restore Your Android Phone With ADB (And rsync)

Based on my previous scripts and inspired by two blog posts that I stumbled upon I tackled the “backup all my apps, settings and data” problem for my Android devices again. The “new” solutions both use rsync  instead of adb pull  for file transfers. They both use ADB to start a rsync daemon on the device, forward its ports to localhost and run rsync against it from your host.

Simon’s solution assumes your phone has rsync already (e.g. because you run CyanogenMod) and can become root via adb root . It clones all files from the phone (minus /dev , /sys , /proc  etc.). He also configures udev to start the backup automatically when the phone is plugged in.

pts solves the setup without necessarily becoming root. He also has a way of providing a rsync binary to phones that don’t have any (e.g. when running OxygenOS). He also has a few tricks on how to debug the rsync daemon setup on the phone.

I’ve tried to combine both methods. My approach doesn’t require adb or rsync to be run as root. It’ll use the the system’s rsync when available or temporarily upload and use a backup one extracted from Cyanogen OS (for my OnePlus One). Android won’t allow you to  chmod +x a file uploaded to /sdcard , but in /data/local/tmp it works. 😀

The scripts will currently only backup and restore all of your  /sdcard directory. Assuming you’re also using something like Titanium Backup you’ll be able to backup and restore all your apps, settings and data. To reduce the amount of data to copy it uses rsync filters to exclude caches and other files that you definitely don’t want synced ( .DS_Store  files anyone?).

At the moment there’s one caveat: I had to disable restoring modification times (i.e. use --no-times ) because of an obnoxious error (they will be backuped fine, only restoring is the problem): 😒

mkstemp “…” (in root) failed: Operation not permitted (1)

Additionally if you’re on the paranoid side you can also build your own rsync for Android to use as the backup binary.

The code and a ton of documentation can be found on GitHub. Comments and suggestions are welcome. 😀

Build Rsync for Android Yourself

To build rsync for Android you’ll need to have the Android NDK installed already.

Then clone the rsync for android source (e.g. from CyanogenMod LineageOS) …

… create the missing jni/Application.mk  build file (e.g. from this Gist) and adapt it to your case

… and start the build with

You’ll find your self-build rsync in obj/local/*/rsync . 😀

Update 2017-10-06:

  • Updated sources from CyanogenMod to LineageOS.
  • Added links to Gist and Andoid NDK docs
  • Updated steps to work with up-to-date setups

If you get something like the following warnings and errors …

… you probably need to update config.h and change /* #undef MAJOR_IN_SYSMACROS */ to #define MAJOR_IN_SYSMACROS 1 .