(securely and automatically)
In this article I’m going to cover a secure yet automatic method for backing up (almost) everything on your jailbroken iPhone [or iPod Touch, possibly].
First off, I’m assuming you’ve already jailbroken your iDevice and can ssh into it. If you haven’t gotten that far yet, there are many tutorials available on the tubes. It is smart to run Insomnia on your phone so you don’t have to keep poking it to keep it awake while you are connected to it.
I’m also assuming you’re running OS X. This should work on Linux as well, excepting the bit about setting up a sparse bundle disk image (more on that in a minute). You can probably get a similar thing working on Windows using cygwin and the DOS “at” command instead of cron.
Our first step is to setup the security aspect of this process. I find it’s generally best to start with security, rather than trying to bolt it on later.
So, what we need to do is setup an RSA key. Fire up terminal and follow along:
ssh-keygen -t rsa
After typing this command you should get output like this:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:
Use the default location to save the file. While you can use an empty passphrase I highly recommend against it (we want this to be secure, right?). Just don’t forget what you entered here, or you’re going to be crying later.
So now you should have a public and a private key. Why this is important is outside the scope of this article. If you’re curious, you can find out more on the tubes (you probably should be, if you have no idea how the concept of public key encryption works).
For the purposes of this article I’m going to be using all command-line tools, simply because they are built-in to the OS. You are welcome to use GUI tools (such as Fetch for SFTP, etc).
The next step is to find out the IP address of your iPhone. Note that it is important that both your phone and your computer are on the same network. It’s doubtful that this will work over a cell network connection. If you have SBSettings installed, this info will show up there (labeled “Wi-Fi IP Address”). If you haven’t installed SBSettings you can find the info in Settings -> Wi-Fi -> then click the arrow next to your wireless network.
Armed with your iPhone IP address, we need to SFTP into it. If SSH is enabled, you should be able to connect.
sftp root@iphone-ip (where iphone-ip is your iPhone’s IP… that you just looked up)
By default the password is “alpine” (change it).
put ~/.ssh/id_rsa.pub /private/var/root/.ssh/
exit
What this does is copies your RSA public key to the iPhone. Now we need to add your public key to the authorized keys.
ssh root@iphone-ip
cd .ssh
cat id_rsa.pub >> authorized_keys
chmod 755 /var/root
chmod 700 /var/root/.ssh
chmod 644 /var/root/.ssh/authorized_keys
rm id_rsa.pub
(again replacing iphone-ip with the IP address of the iPhone in question)
Install the nano and rsync packages from Cydia. nano is a text editor and rsync is what we are going to use to actually do the backup. I don’t advise actually trying to run nano on the iPhone – the screen is too small. So we are going to do this in SSH:
nano /etc/ssh/sshd_config
Find the line that says “#PasswordAuthentication yes” – remove the # and change the yes to a no, then add these lines to the end of the file:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
Save the file (Control+O) and quit nano (Control+X). You may have to restart the iPhone or at least SSH in order for the changes to take effect (you can test by SSHing into the phone… does it ask you for your SSH password? It may still ask for your RSA passphrase).
Now the security portion is complete. Close SSH.
OPTIONAL [RECOMMENDED]: Setup an encrypted sparse bundle disk image to store the iPhone backup in. Don’t forget the password to that either. If you want the backup to be fully automatic, check the option to save the password for the sparse bundle in your keychain, otherwise when the backup runs it will prompt you for this password. Personally I did this step and I store the sparse bundle on my Time Capsule.
Here is the command for the actual backup process:
rsync -avz --stats --progress --exclude "private/var/mobile/Media/iTunes_Control/Music" root@iphone-ip:/ /Volumes/iPhoneBackup --delete (replace iphone-ip with the phone’s IP, and /Volumes/iPhoneBackup with the path you want to backup the phone to).
The initial backup could take a (very) long time, depending on how full your phone is. For me it took a little over half an hour. Note that this intentionally does not backup music, as that should all be stored in iTunes anyway.
The last step is to setup the automation. Some of us don’t like to admit it, but if backups aren’t automagical, we don’t do them. Or, at least, we don’t do them frequently enough.
Run this command in Terminal:
crontab -e
This opens up your cron file in vi. Press “i,” (as in “insert”) then paste in this line:
0 0 * * * hdiutil mount /Volumes/Backup/iPhone.sparsebundle; rsync -avz --stats --progress --exclude "private/var/mobile/Media/iTunes_Control/Music" root@iphone-ip:/ /Volumes/iPhoneBackup --delete > ~/.iphone_backup_log
(making the usual replacements)
Press escape then type “:qw” to quit vi. This command assumes you followed my above recommendation of creating a sparse bundle (replace /Volumes/Backup/iPhone.sparsebundle with the path to your sparse bundle). If you didn’t, remove this section of the command: hdiutil mount /Volumes/Backup/iPhone.sparsebundle;
This tells your machine to backup the phone every day at 1AM.
Sources:
http://justinhileman.info/articles/how-to-back-up-your-iphone-with-rsync
http://blog.release78.org/?p=55
http://www.macosxhints.com/article.php?story=2001020700163714