Aug 08

Your SSH server on the iPhone is already set up. Now you want to make sure you get all the files from you iPhone backed up? You plan to change a few files on you iPhone and want to make sure you can revert to the original files?

iTunes will not backup all neccessary files. And even if it did backup your specific file, it is not that easy to get the specic file out of an iTunes backup. (I will give you a post how to extract files from iTunes backup files tomorrow.)

So my advice is to copy all files from your iPhone to your computer. Its quite simple!

You need a backup directory on your computer, i used “iPhone-Backup” in my home directory for this example.

(1) Make sure your computer and your iPhone are connected to the same WiFi network and you know the bonjour name or the IP-address of your iPhone.

(2) Make sure the iPhone auto-lock is set to off: Go to the iPhone “Settings”: Then to “General”, then “Auto-Lock” and choose “Never”. You can change this after backup but now you want to make sure the WiFi connection stays on while the backup runs.

(3) Open the Terminal.app on your computer

(4) Make sure you got the backup directory in your home folder already set up. If not, just type: mkdir ~/iPhone-Backup

(5) For the backup (and for all subsequent backups later) type in: rsync --exclude private/var/mobile/Media/iTunes_Control/ -av root@(your-iPhone-IP):/ ~/iPhone-Backup/ --delete

So if your iPhone address is 10.0.1.3 the command would look like this: rsync --exclude private/var/mobile/Media/iTunes_Control/ -av root@10.0.1.3:/ ~/iPhone-Backup/ --delete

Now you got to wait a little. An iPhone running on firmware 2.x will probably take a few minutes for the first backup. Without applications the copy folder should be about 600 MB.

What does the command actually do?

(a) rsync will copy files via SSH from the SSH-server account root folder (root@10.0.1.3:/) to the local folder (~/iPhone-Backup/) that are not already copied. So the first backup will take quite a long time, all next backups will only copy new or changed files.

(b) the copy will exclude all files in your iPhones “iTunes Media” folder. (actually all files starting with private/var/mobile/Media/iTunes_Control/) This is neccessary since all your synced music and fotos from iTunes are stored here. Otherwise you would get a backup of all this data already stored in your iTunes!

(c) The “--delete” tag at the end makes sure all files already backup up and not on the iPhone anymore will be deleted with the next backup. So if you delete e.g. some camera pictures on your iPhone, the backup pictures on your computer will be deleted as well with the next backup.

(d) The “-av” tag specifies the backup mode: Archiving (a) and verbose (v). In verbose mode you get a long list of files that were backup up. There will be some files that cannot be backed up: Some of them are only links to devices on the phone. And some are part of running processes on the iPhone, e.g. your temporary mail file. Do not worry too much about them. You could change “-av” to “-a” and avoid the long list at all after your backup is set up correctly.

A little correction:

The Photo database will be copied: It is in ~/Media/Photos/ So the correct syntax for not copying the photo database would be: rsync --exclude private/var/mobile/Media/Photos/ --exclude private/var/mobile/Media/iTunes_Control/ -av root@10.0.1.3:/ ~/iPhone-Backup/ --delete

Hint: You can exclude any other folders – as long as you add an --exclude before any of them. Listing them after the --exclude command (space or comma separated) will not work!

written by TwiPhone \\ tags: , , ,

2 Pings to “How to backup your iPhone regularly via SSH using rsync (Updated)”

  1. Corrupted backups « Box of Clams Says:

    [...] Instead of using the buggy iTunes to backup your iPhone, you can also do it in a standard UNIX way with the UNIX command “rsync”. This blog explains how. [...]

  2. JayCagey » Blog Archive » Backup iPhone with rsync Says:

    [...] Trying to backup the iPhone through iTunes has become impossible. I can let it run for 10 hours and it will only be a quarter of the way done. BUT – I can do it myself using rsync. HT: http://www.twiphone.com/2008/08/08/how-to-backup-your-iphone-regularly-via-ssh-using-rsync/ [...]


22 Responses to “How to backup your iPhone regularly via SSH using rsync (Updated)”

  1. 1. Jim Says:

    Question: This looks as thou it only backs up media; is that the case? I am suddenly concerned by my iPhone frequently crashing and needing to be recovered. It is primarily a phone and when I am away from home without my computer, one of these crashes would be disastrous. I have found that even when all I see is the pineapple logo I can still SSH into the phone. This suggests to me that I could use Terminal from any nearby computer to restore necessary system files (provided I keep a backup on a thumbdrive with me.) What I am really getting at is; what directories or files would I have to back up in order to restore my phone to a working state? I would hate to make matters worse by touching any system files that would make it impossible to recover the phone at all. The fact that SSH is running seems to suggest to me that I can leave the root directory alone. But for all I know the solution may even be as simple as cleaning out the cache. Admittedly this is all a little beyond my understanding.

    Anyway: thanks for listening. Your help is appreciated.
    Jim

  2. 2. TwiPhone Says:

    Hello Jim,

    sorry – I´ve been on vacation. In theory you get a full 1:1 copy of your iPhone content.

    The problem is that you cannot easily just restore the content as far as I know. Because the moment your iPhone is running it will open and lock some of the files. To be honest, I never tried to fully restore the iPhone by using a “reverse” rsync command. But I will try with one of our iPhones in the next few days and write something about it.

    For me, much more important was the ability to restore different system settings, configuration or data files. Probably you realized that by updating an application the data files could be lost. Same with a bricked phone and lets say text message or notes. All these files will be backed up with the mentionend rsync command. So restoring your notes would be an easy copying of the “notes.db” file back to your phone. Or restoring a

    I will write some more articles about retrieving files from iTunes backup. And some about restoring from your 1:1 rsync copy.

  3. 3. ML Says:

    Just to let you know, folder /private/var/mobile/Media/iTunes_Control/iTunes is important. Without it, the iPhone will not synchronise with iTunes. Learned it the hard way :)

  4. 4. twiphone Says:

    ML,

    are you 100% positive about this? In my case iTunes created the folder. I will investigate a little further. Workaround should be easy: login into your phone and use “mkdir /private/var/mobile/Media/iTunes_Control/” – still, got to investigate…

  5. 5. v1ncen7 Says:

    How do I restore my iPhone with this backup please?

    rsync –exclude private/var/mobile/Media/Photos/ –exclude private/var/mobile/Media/iTunes_Control/ -av ~/iPhone-Backup/ root@10.0.1.3:/ –delete

    or else?

    thks

  6. 6. Kia Says:

    Is it possible to set up a cron job to do this automatically? I guess that one problem is that it only works when the iphone is on the same network as the “server”?

  7. 7. Jon Says:

    I was wondering if you could run the rsync command from the iPhone, (the app is available via Cydia) and possibly have it run as a daemon, (I’m not sure how much resource that would hog.) But that would allow you to backup the files from your phone to your server no matter where you were, by using a dynamic DNS for your home server. It take a while the first time if you’re not using wifi, but if all you want to do is backup user files, it could come in handy being able to backup from any connection.

    Another obstacle to having it run in the background would be automatic ssh authentication. I’m still learning more about this, so if anyone has more insight I’d love to hear it…

    – jon

  8. 8. Jon Says:

    Ok, so I’ve been playing around with it a little bit, running rsync from my iPhone to put the files on my home computer, through ssh, over edge. Here’s my command:

    rsync -e “ssh -p 222″ -razvn –exclude /User/Media/iTunes_Control /User/Media User@my.dyndns.com:~/iphone_backup

    To explain the flags:

    -e “ssh -p 222″ tells rsync to use ssh on port 222
    -razvn r: recursive a: archive, z: compress, v: verbose, n: dry-run (don’t actually copy files just tell me what would have been copied)
    –exclude exclude the following directories
    User login
    my.dyndns.com dynamic dns to my home server (not the real address though ;) )
    ~/iphone_backup the path to the folder on my home server

    So I ran it from my iPhone, and it connects just fine and seems to work, but it won’t exclude /User/Media/iTunes_Control

    It definitely works from your iphone, over edge, I just need to figure out how to run it as a daemon (if possible) and how to properly exclude directories. But all I want to do is backup any files I may download through different apps, and maybe extend this to other files to backup…

    Again, this is all new to me, so I would love any help I could get…

    – jon

  9. 9. Jon Says:

    I got the exclude to work… exclude=iTunes_Control

    Final command:
    rsync -e “ssh -p 222″ -razv –exclude=iTunes_Control –exclude=Photos /User/Media User@my.dyndns.com:~/iphone_backup

    Since the folder is a sub folder of /User/Media which I specify as the beginning target, I only needed a relative path, not a direct path, I also added another exclude for Photos.

    So I ran the command and the first time it ran like a second because all the folders specified were empty. Then I took a picture with my camera, and ran rsync again, this time it took around 30 secs to a minute. So probly not too practical over edge, but very practical to be able to rsync without having to be on the same local network as your server.

    Now to figure out how to make the process of running the command easier… ;)

    – jon

  10. 10. yaya Says:

    I’m running 2.0..and have my full rsync backup on my pc… good..
    If I want to upgrade from 2.0 to 2.1 for exemple…. I can’t replace all files from backup .. because it will replace new 2.1 system files by old 2.0 files..
    So does someone know which files contain the data for each default application ? So I could replace only these “data” files to recovers my data and settings…

  11. 11. FelixAdam Says:

    OK, I broke my phone …. then I installed the firmware again … how is the command to get all my stuff back from the backup-folder to my phone?

  12. 12. DistortedLoop Says:

    Great tip. I’d written a pretty lame shell script to just back up the data files of my favorite apps using scp and the appropriate path names. Did the job, but I see benefits in your rsync method.

    re: the -delete flag.

    By deleting anything from your backup set that has been deleted off the phone, aren’t you running the risk of losing something accidentally deleted or lost from some sort of crash?

    I can see the pros and cons of including it or not.

  13. 13. Miguel Says:

    The backup shows same errors of cahe files ,..is this normal?

  14. 14. Miguel Says:

    To restore the iphone via rsync?
    the lines command?is posible?The backup is in my imac.

  15. 15. Caue Rego Says:

    thanks for the help (specially the rsync syntax)

    you might want to add that rsync must also be installed on the iphone otherwise it won’t work. at least that’s what it seemed to me.

  16. 16. TwiPhone Says:

    Caue, you are right. You would have to open Cydia and install “rsync”

    Thanks for pointing it out!

    I am on 3.0 now and can´t wait for the jailbreak :) So far all good!

  17. 17. justin Says:

    what does SSH stand for

  18. 18. Distorted Loop Says:

    @Justin- if you don’t know what SSH stands for, this whole article was probably way beyond your skill sets, but it stands for Secure Shell. Read more about it here: http://en.wikipedia.org/wiki/Secure_Shell

  19. 19. TwiPhone Says:

    Dist.Loop > Thanks

  20. 20. fullofquestions Says:

    Questions:
    1. if you run the rsync command from the PC/Laptop I believe that the iPhone does not need to have rsync. Could someone please confirm?
    2. can you use rsync/ssh to copy non OS files to the iPhone? This tutorial shows how to backup the iPhone’s important files. Once you ssh into a non jailbroken/unlocked iPhone can you copy files to the entire partition or are there certain locations that are writeable?
    3. does anyone recommend any SSH apps? I see that after jailbraking an iPhone you can install third party apps. For a buck or so you can get OpenTerm from iTunes and I would rather just spend that money and get going quicker (so far my iPhone 3G s is neither unlocked or jailbroken)

    Thanks

  21. 21. hackencrack Says:

    fullofquestions:

    Answers:
    1. rsync and ssh must be present on both devices to facilitate secure synchronization.

    2. Yes, you can use rsync to synchronize files on either device in either direction. You could also backup critical files from your computer on your phone. Invoke:

    man rsync

    from a terminal shell on your computer for a complete description of the arguments. Google rsync if you want to learn more. rsync has been a very popular program for *many* years.

    Certain files on every (unix) file system cannot be synchronized or copied for various reasons, but in general you don’t need to be concerned with these files.

    You can rsync an entire partition. It’s done all the time.

    3. There’s really only one ssh. OpenSSH is part of the core unix tools in Cydia. That’s what you need. The other ssh-related stuff you see in iTunes and Cydia are tools that are *client* tools. You need sshd, the server portion of ssh, running on the phone. It’s part of the OpenSSH package. I doubt you will find THAT in iTunes store anytime soon. ;-)

    Good luck…

  22. 22. avid reader Says:

    Dear TwiPhone,

    Could you please tell us how you “reverse rsync” to restore the entire partition of the iPhone? I’m guessing you have to put it in some mode other than the normal mode such as the recovery mode, but can you make the iPhone connect to wifi and ssh in recovery mode? Please tell us your experience.

    Thanks.

Leave a Reply

TwiPhone.com is Digg proof thanks to caching by WP Super Cache