Set Up Automatic Folder Synchronization for iPhone

francisois

New Member
Nov 22, 2007
20
0
0
I've been toying around with iPhone/iPod Touch file management for the last few days and I was quite impressed with MobileFinder's synchronization utility. The Terminal vt-100 utility is a godsend. MobileFinder basically runs a script through the terminal app when you click the sync buttons. The utility used is called 'rsync' and is commonly used by backup programs to synchronize folders on 2 local drives and even remote drives. But as much as I am impressed with the ease of use of MobileFinder's sync feature, I am disappointed in the fact that it has no automation. In this tutorial I will show you how to use crontabs and registered ssh auth keys within terminal to streamline and automate the backup/synchronization process.

For this tutorial, you will require:

- A jailbroken iPod Touch/iPhone (tested on version 1.1.1 but should work on others)
- Term-vt100.app from Installer
- A Mac (Windows does not have built-in ssh server functionality and thereforee no remote login from iPhone)
- Access to a Wi-Fi connection

1. Go into your iPhone settings module, click wifi and then click the blue arrow next to the name of your connected access point. Write down the iPhone IP address.
2. Run Term-vt100.app and type the following commands:

ssh-keygen -t dsa -f ~/.ssh/id_dsa -C "Key"
(when prompted for a password, just hit enter, as the presense of a password will not allow for automation)

Once the auth key is created, you must store it in your ~/.ssh directory:

cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

You must then send your auth key to your Mac. Retrieve your Mac's IP address from the network preferences module on your computer and your SHORT username from the Accounts module:

cat ~/.ssh/id_dsa.pub | ssh macusername@macipaddress 'cat - >> ~/.ssh/authorized_keys'

3. At this point, everything is set up, all you need to do now is tell your iPhone when and where to sync. I created a folder in my Home directory called /iPhone where I place files and folders that I want to have on my iPhone. The address for this folder is ~/iPhone. You could also place a /Sync folder in your Documents and its address would be ~/Documents/Sync . You are free to choose your own directory here but putting the dir in your home folder simplifies the process. Create a directory called Sync in your iPhone's home dir (the folder containing Library and Media - absolute address is /private/var/root/) using any tool of your liking, I prefer to use SFTP on cyberduck.

Open textedit on your mac and copy the following, replace all instances of macipaddress with your ip address and macusername with your mac's short username (ie if your user name is John Doe, the short unix name is John):

0 * * * * /usr/bin/rsync -avz macusername@macipaddress:~/iPhone/ ~/Sync
20 * * * * /usr/bin/rsync -avz macusername@macipaddress:~/iPhone/ ~/Sync
25 * * * * /usr/bin/rsync -avz ~/Sync/ macusername@macipaddress:~/iPhone
40 * * * * /usr/bin/rsync -avz macusername@macipaddress:~/iPhone/ ~/Sync


Make sure you press enter at the end of the last line to leave a line space below the commands. Go to Format>Make Plain Text or press SHIFT-COMMAND-T to convert the text into plain text. Save the file as root.txt. Upload it to your iPhone's home directory.

4. Restart term-vt100.app. Type the following command:

crontab root.txt

5. Type the following command to check if the procedure went smoothly:

crontab -l

The output should look like the code in root.txt

6. You're done, now place some files/folders in your iPhone dir on your mac, and wait until the cronjob runs and check your iPhone's Sync dir.

Now for some explanation. I used the unix cron utility to set up a schedule for running rsync every few minutes. I have chosen to do 3 local syncs and 1 remote sync simply because you will most likely be wanting to send files to your iPhone rather than the other way around, but on the offchance that you have a file that you somehow managed to download off the internet into your iPhone's /Sync dir, every hour on the 25th minute, the iPhone will send this file to your mac.

Cron uses an interesting way to setup schedules, the first 5 arguments allow you to choose between hourly, daily, weekly, monthly and yearly runs. Say you want the schedule to run every hour, you will be able to pick at which minute this cronjob runs, but you will not be able to get it to run every 2 hours unless you set up 12 separate runs, on hour 0,2,4,6,8 and so on. As you can see, this is not a very efficient method. I chose to have 4 syncs an hour simply because having it any more frequently would result in a huge crontab list (eg. every 5 minutes would have 12 cronjobs in the list). You can edit the sync time as you please, but I would advise tending toward making it less frequent rather than more frequent).

If you have any questions or require help troubleshooting, I'm here. If you are familiar with unix scripts and have suggestions for making the procedure simpler (specifically, how to run .sh scripts from the iPhone - I have not been able to bypass the permission denied error). If we can somehow find a way of running scripts, we would be able to completely automate the procedure by simply running one script to perform all the above tasks.