iphone backup via ssh
- August 10th, 2010
- Posted in IT
- Write comment

Yes, i know itunes keeps backups of the iphone contents but really, i didn’t want to trust an application and take the risk to loose my contacts, sms, and so on, especially when i was going to do something not expected by that application ( yes, i’m talking of the jailbreak
So, since my iphone was already out of jail, i was able to login via ssh into the device system. It simply keep all of your stuffs in (sqlite) databases and folders, so if you know where-is-what the backup process is really simple. So i wrote a simple shell script to do the backup, and here it is. In order to use it, you should:
- Mkdir the backup folder you wish to use and copy the script inside
- Connect your iphone to a network reachable from the machine you are going to run the script from
- Fill the IPHONE_ADDRESS field with the ip address on the given network assigned to the iphone
- Toggle flags to decide what you want to backup or not
- Run the script
You should also ( if you did not before ) append your public rsa id to your iphone root ‘authorized_keys’ file in order to avoid being prompted for password at each step, as explained in this previous entry.
I did this as root, but you may try running it as mobile if you wish, just replace any root@ with mobile@ using vim or sed ‘s/root@/mobile@/g’.
Also, this is a good chance ( to add the security tag
) to remember you, jailbreaked iphone users, to change the default ssh “alpine” password, because otherwise, if you connect to untrusted (public) networks with the ssh daemon running, stealing all of your personal data may be easy as running this script
( If your first thought is “they won’t know my ip!”, give up: a simple nmap scan will reveal you quickly and easily )
You can download the script here
#!/bin/bash ################################################################################# # A simple script to backup iphone critical data # # Author: n0on3 - http://www.n0on3.net # ################################################################################# # You should append your public rsa id to your iphone # # 'authorized_keys' file in order to avoid being # # prompted for password at each step, e.g. running: # # --- example command --- # # ssh root@iphone 'if [ ! -d ~/.ssh ];then mkdir ~/.ssh; fi' # # cat ~/.ssh/id_rsa.pub | ssh root@iphone 'cat >> ~/.ssh/authorized_keys' # ################################################################################# # Configuration # This should be your iphone address, eg. 192.168.0.5 or 10.0.1.4 IPHONE_ADDRESS="10.0.1.3" # BACKUP_CONTACTS='YES' BACKUP_NOTES='YES' BACKUP_SMS='YES' BACKUP_CALENDAR='YES' BACKUP_BOOKMARKS='YES' BACKUP_HISTORY='YES' BACKUP_PICTURES='YES' BACKUP_PHOTOS='YES' BACKUP_RECORDINGS='YES' BACKUP_MAIL='YES' BACKUP_CYCORDER='YES' ################################################################################# # # = Address Book = if [ $BACKUP_CONTACTS = 'YES' ]; then scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/AddressBook/AddressBook.sqlitedb AddressBook.sqlitedb scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/AddressBook/AddressBookImages.sqlitedb AddressBookImages.sqlitedb scp root@$IPHONE_ADDRESS:/var/root/Library/AddressBook/AddressBook.sqlitedb RootAddressBook.sqlitedb fi # = Notes = if [ $BACKUP_NOTES = 'YES' ]; then scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/notes.db notes.db scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/Notes/notes.db notes.db scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/Notes/notes.idx notes.idx fi # = SMS = if [ $BACKUP_SMS = 'YES' ]; then scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/SMS/sms.db sms.db scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/SMS/sms-legacy.db sms-legacy.db fi # = Calendar = if [ $BACKUP_CALENDAR = 'YES' ]; then scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/Calendar/Calendar.sqlitedb Calendar.sqlitedb fi # = Safari Bookmarks = if [ $BACKUP_BOOKMARKS = 'YES' ]; then scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/Safari/Bookmarks.plist Bookmarks.plist fi # = Safari History = if [ $BACKUP_HISTORY = 'YES' ]; then scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/Safari/History.plist History.plist fi # = Cam Taken Pictures = if [ $BACKUP_PICTURES = 'YES' ]; then ssh root@$IPHONE_ADDRESS "tar czvf /private/var/mobile/Media/taken-pics.tar.gz /private/var/mobile/Media/DCIM/100APPLE" scp root@$IPHONE_ADDRESS:/private/var/mobile/Media/taken-pics.tar.gz taken-pics.tar.gz ssh root@$IPHONE_ADDRESS "rm /private/var/mobile/Media/taken-pics.tar.gz" fi # = Loaded Pictures = if [ $BACKUP_PHOTOS = 'YES' ]; then scp -r root@$IPHONE_ADDRESS:/private/var/mobile/Media/Photos Photos fi # = Voice Recordings = if [ $BACKUP_RECORDINGS = 'YES' ]; then ssh root@$IPHONE_ADDRESS "tar czvf /private/var/mobile/Media/Recordings.tar.gz /private/var/mobile/Media/Recordings" scp root@$IPHONE_ADDRESS:/private/var/mobile/Media/Recordings.tar.gz Recordings.tar.gz ssh root@$IPHONE_ADDRESS "rm /private/var/mobile/Media/Recordings.tar.gz" fi # = Mail = if [ $BACKUP_MAIL = 'YES' ]; then ssh root@$IPHONE_ADDRESS "tar czvf /private/var/mobile/Library/Mail.tar.gz /private/var/mobile/Library/Mail" scp root@$IPHONE_ADDRESS:/private/var/mobile/Library/Mail.tar.gz Mail.tar.gz ssh root@$IPHONE_ADDRESS "rm /private/var/mobile/Library/Mail.tar.gz" fi # = CyCorder Videos = if [ $BACKUP_CYCORDER = 'YES' ]; then ssh root@$IPHONE_ADDRESS "tar czvf /private/var/mobile/Media/Videos.tar.gz /private/var/mobile/Media/Videos" scp root@$IPHONE_ADDRESS:/private/var/mobile/Media/Videos.tar.gz Videos.tar.gz ssh root@$IPHONE_ADDRESS "rm /private/var/mobile/Media/Videos.tar.gz" fi
Cheers! Heve fun



Hello,
the backup was working fine, but how can I restore the Data in the iphone, because if I play the data f.f the *.sqlitedb back to the phone may kontakts are still not there?
Can you help me?
@Gerrit
@Gerrit: If the structure of the database is not changed ( and i guess it isn’t but check first ) you just have to copy back the database files to the phone in the given location.
Please note that this will overwrite the current database and delete any contact on your phone that is not on the backup.
Otherwise, if you wish to merge the backup and the current database files, you may play with sqlite3 to select records from the backup and insert them into the current databases.
This is also the way to proceed if the dabase schema has changed.
Of course in order to do that you will have to manage some SQL.
Hope this helps, good luck!
trying to follow you on twitter but cant find your name
Hi mate,
that is exactly what I was looking for, a script that saves all the relevant files from my phone via SSH.
Saved me a lot of work.
Thanks heaps.
Cheers, Chris
Trying to use this bash script within Ubuntu 10.04. I am trying to perform your suggestion first of all by issuing the 2 following commands:
#ssh root@iphone ‘if [ ! -d ~/.ssh ];then mkdir ~/.ssh; fi’ #
# cat ~/.ssh/id_rsa.pub | ssh root@iphone ‘cat >> ~/.ssh/authorized_keys’
BUT, upon entering the second one I get the following errors in my terminal
sh: -c: line 0: syntax error near unexpected token `;&’
sh: -c: line 0: `cat >> ~/.ssh/authorized_keys’
So I am not sure why this is. Currently my user has an id_rsa which requires me to enter a passphrase, so I enter that when prompted and then I enter the root password to ssh into the iphone. I can get on to the iPhone just fine.
The problem is that when the script runs it’s first asking me for my passphrase for key ‘/home/daniel/.ssh/id_rsa AND then it’s asking me the root password to login. Now it sucks having to do that 10 times because my passphrase is very complex. ANyway to solve that?
@Anonymous @n0on3
@Chris you’re welcome
I’m actually thinking of making another version of this, as soon as I’ll be able to get my hands on another jb iphone
@Daniel
First of all, what you should copy to your phone is your id_rsa.pub (i.e., PUBLIC key), so anything you get is not related to the passphrase to unlock the private key encryption. You probably just copied the command from the web with some escaped char in it, is that possibile?
Anyway, you can use whatever way you want to put your public key on your phone, e.g.:
1) just ssh into your phone
2) make sure that ~/.ssh/ dir exists
4) vim into the authorized_keys file
5) directly paste the content of your ~/.ssh/id_rsa.pub
This should keep you from having to insert the password.
As far as concern inserting the passphrase, the purpose of having a passphrase is that you have to enter it in order to use the private key each time. You cannot avoid that. If that really bothers you, you might instead consider a different strategy: rather than locking your id_rsa with a passphrase, just encrypt it manually (use something strong like ccrypt which uses something as strong as AES).
In this way, once you decrypt it, you can use it as long as you want without entering the passphrase.
However, this has still a number of disadvantages:
- first of all, you have to decrypt/re-encrypt the file yourself manually (and there’s a fair chance that you forget to do that sometimes)
- unlike passphrase-lock, during the time the file is decrypted, anyone with access to the file will be able to read it
Unfortunately, security has always its price: choose your poison
cheers