Every once in a while, there are some files that I want kept in sync between my Android-based phone and one of my Linux (or Mac, or any Unix, or maybe Windows too, but I use Linux…) boxes. Yeah, I can copy them over manually via USB or even something a bit simpler like AndFTP (assuming you can SCP to the target machine). But that’s a real pain for anything like my KeePass (well, actually KeePassX and KeePassDroid) password database, that I might add something to at any time and forget to sync. I also try to occasionally (waiting in line?) backup SMS, call logs, etc. on my phone, and like to have those synced back to the desktop automatically.
Enter the solution: rsync backup for Android, a rsync client for Android that includes Tasker plugins (there are a few things about the app that I don’t like, but it seems to be the only option at the moment), and Tasker, an automation framework for Android.Tasker is one of the few Android apps that I’ve actually bought (i.e. not free/no-cost), and is currently selling for $6.49. It’s an incredibly capable task automator, very much akin to Locale on steroids. On the down side, Tasker can eat up battery life if you don’t configure it intelligently, and it’s not always 100% reliable when interacting with the system. On the positive side, Tasker can identify practically any combination of states in the android system (from hardware and software events to GPS location, time, signal status, etc.) and perform almost any task on the system based on this information. Sure, this specific problem could be solved with a cron replacement (which Android lacks, of course), but Tasker can do things like play specific audio files when you get an SMS from a specific number, mute audio at certain GPS locations, or turn WiFi on when I get home and off when I leave the house. It also has a plugin architecture, and rsync backup for Android happens to have a plugin that works with it.
So, our goal is to have a daily, bi-directional, newest-file-wins sync between a directory on our Android phone and a directory on a computer. I’m not going to go into a lot of the computer-side stuff, mainly because that varies quite a bit between operating systems, and also because my personal setup is a bit paranoid in terms of security. For the computer side, we’ll need a machine that can be SSHed to from the Internet (either a static IP or a known hostname/dynamic DNS), a user that can run rsync over SSH, and a directory that’s writable (obviously).
Setup:
- Buy and install the Tasker app.
- Install the rsync backup for Android app.
- Configure the rsync stuff on the computer. In the simplest form, we’ll just need a user that can login and run rsync, and a directory to sync from/to (note: this should be a directory used only for syncing the phone…).
- Open the rsync backup for Android app. Use Menu -> Generate Keys to generate a new pair of SSH keys, and then get the public key setup on the target computer. See the developer’s web site for instructions.
- Once keys are setup, create a new profile called “PC-droid”. Set the local directory to a new empty directory (I used /sdcard/sync), enter the remote host address, port, username, and remote directory, and select the private SSH key that you created. Check off “rsync on reverse direction”. As this program is just a GUI wrapper around normal rsync binaries, you can specify additional options to the rsync command; my string ended up being -vHrltDuO --chmod=Du+rwx,go-rwx,Fu+rw,go-rw --no-perms. If it helps, at the bottom of the screen you can see the actual rsync command line that will be run. Save when done.
- Save the profile, then long-press it and select “Duplicate”. Change the name to “droid-PC”, uncheck “rsync in reverse direction”, and change your additional options as needed (mine became -vHrltDu --chmod=Dug+rwx,o-rwx,Fug+rw,o-rw --no-perms). Save when done.
- Create a test file in the sync directory on the PC, and a different one in the sync directory on the droid.
- One at a time, in the rsync backup app, tap on the profile names. If all goes well, the syncs should run, and both files will now be in both places. If there are any problems, the output should help; the most likely issues are probably permissions, rsync command options, or SSH keys.
- Long-press each profile, select “Edit”, and check off “Close log window after job is done”. Save profile.
- Now fire up Tasker. Click the “+” at the bottom of the screen to create a new profile, call it “sync”, and click the check mark.
- On the First Context panel, tap Time, and select when you want the jobs to run; I chose 03:01. Tap the check mark.
- On the Task Selection panel, tap New Task. Give it a name, like “sync2″.
- On the Task Edit panel, tap the “+” button at the bottom left, tap Plugin, tap “rsync backup for Android”, click the “Edit” button on the Configuration line, and select the PC-droid rsync profile. Tap the check mark in the lower left to save.
- Repeat the last step for the droid-PC rsync profile.
- Tap the check box in the lower left. This saves the profile.
- In the main Tasker screen, make sure there’s a green check to the right of the profile you just added, and that the button at the bottom right of the screen is set to “On”.
Assuming this all went well, the next time the time you specified rolls around, your sync should run. If you gave the task a name in step 12, you can setup additional profiles to run it at other times (or use the repeat logic builtin).
very helpful. Saved me a lot of time from digging into tasker and rsync docs
Hey I tried running the application but it fails. I generated the keys from the app and put it in the .ssh/authorized_keys on my server and also created the profile as you said but when I try to run the app it shows nothing. No logs are displayed. Is there any way to troubleshoot it?
Sathyam,
Yes, you have two troubleshooting tools; one is the SSH logso n your server to make sure it’s at least connecting, and the other, if you long-press each profile in the Rsync Backup app and select “Edit”, near the bottom of the screen is a check box “Close log window after job is done.” If you uncheck this, save the profile, and then tap the profile once to run it, you’ll be able to see the output of the rsync process on your phone. If you can paste that here, I might be able to give you a hint at what the problem is.
-Jason
I saw the ssh logs. It is not connecting even. And I did see the logs after disabling the tick in the edit menu of the profile. Nothing is shown. I hope tasker is not needed to run rsync. I basically require to run rsync either continuously at regular intervals or from inside an android application(If this is possible somehow it would be great). Just like it is possible to run tcpdump-arm on a rooted phone if rsync is also possible then i request you to guide me there.
I’m trying to use this to backup my phone to a NAS drive that runs Linux. I am able to get the files to the box but I’d like to be able to open them under windows. Any chance you can help with the command line options so that this will work? Also, the file I sent from the server back to the phone transferred ok but I can’t see it in the folder. There are two files in the folder, I just don’t seem to be able to see one of them.
Mike,
I can’t really help you with opening the files under Windows, as I don’t really know much about it. The documentation for the NAS should have some instructions on how to mount the NAS as a network drive under Windows. As to seeing the files on the phone, what file manager are you using? Is it possible that it’s just not a supported file type? I’d give Astro a try if you’re not already using it.
-Jason
Just spent a whooooole day getting this to work on a Windows machine. Kept running into an error where the Android rsync client would just give a blank log screen for two minutes and eventually the connection would just timeout.
This may seem obvious to some, but I thought I’d share anyway – Windows Firewall was blocking port 22. Once that got fixed, it was smooth sailing.
Thanks for this handy guide.
Hmm… I didn’t even know there was anything for windows that used port 22… cool to know.