Saturday, 12 September 2009

iPhone Backup Script

If you jailbreak your iPhone there are multiple ways of doing so. One method requires you to restore your phone, other issues can arise from some of the jailbreak specific applications when upgrading firmware. The best method when upgrading in this situation is to do a clean install and re-apply your jailbreak software (something like package backup can help in this situation). To re-apply the core settings such as SMS / Voicemail / AddressBook can be more hassle.

To get around this I have started to write a backup script (Mac / *nix specific). This will, via SSH, backups installed applications Documents folders, all core settings like SMS / AddressBook / Calendar etc. You will then be able to restore set up the phone as a new phone and then sync all apps and media. Once done you will be able to use the restore function of the script to apply the backed up data.

The script is work in progress and does need to be tidied up a little. The restore function is being written (testing to be done when next firmware / jailbreak is release :-) )

The script and instructions provided below:


  • Run the backup script - this should be used in conjunction with iTunes. If anything fails restore from iTunes backup. To run the backup script SSH will need to be turned on, on the phone. You will need the mobile (or root if you prefer) password and the IP address of the phone.
  • Run the backup script as follows:
    • ./backup.sh -i 192.168.2.3 -u mobile -s
  • This will setup the SSH keys and ensure everything is setup for the script to work.
  • To run the backups issue the following:
    • ./backup.sh -i 192.168.2.3 -u mobile backup
  • After you have set up an synced the iPhone you will be able to use:
    • ./backup.sh -i 192.168.2.3 -u mobile -d 20090911 restore
  • There are options the script can take -a for application only backup, -c for core components and other features will be added as I require them or users request them in the comments!


The script:
#--- START ---
#!/bin/sh
################################################################################
# TITLE: iPhone Backup Script
# AUTHOR: Cranie
# DATE: 2009-08-29
# UPDATED: 
################################################################################

DATE=`date +%Y%m%d`
TMPAPP=/tmp/iphone_backup.$$

usage() {
    echo "    NAME
        ps -- process status"
    echo "  backup.sh -u username -i 127.0.0.1 -c -a -A backup --- is c or a or A not and!"
    echo "  backup.sh -u username -i 127.0.0.1 -o output_folder restore "
    echo "Initial Setup Run:"
    echo "  backup.sh -s -i 127.0.0.1 -u mobile"
    exit 1
}

echo "Thanks For Using iPhone Backup"

backup() {
    echo "Starting backup"
    if [ ! -d ${DATE} ]
    then
        mkdir ${DATE}
    else
        echo "Backup for today exists. Please remove / rename"
        exit 1
    fi
    if [ "${TYPE}" == "apps" -o "${TYPE}" == "" ]
    then
        ${SSH} "find /var/mobile/Applications -type d -name \*.app -ls | sed -e 's/.*Applications\///g' -e 's/ /§/g'" > ${TMPAPP}
        for file in $( cat ${TMPAPP} )
        do
             APP=`echo ${file} | sed 's/.*\///g'`
             APPID=`echo ${file} | sed 's/\/.*//g'`
             echo "Archiving ${APP}"
             mkdir ${DATE}/${APP}
             ${SCP}:/var/mobile/Applications/${APPID}/Documents ${DATE}/${APP}
         done
         rm ${TMPAPP}
    fi
    if [ "${TYPE}" == "core" -o "${TYPE}" == "" ]
    then
        mkdir ${DATE}/Library
        for core in $( echo "SMS Voicemail Notes AddressBook Calendar" )
        do
            mkdir ${DATE}/Library/${core}
            ${SCP}:/var/mobile/Library/${core} ${DATE}/Library/
        done

        mkdir ${DATE}/Media
        for core in $( echo "Audio DCIM Photos Recordings Videos" )
        do
            mkdir ${DATE}/Media/${core}
            ${SCP}:/var/mobile/Media/${core} ${DATE}/Media/
        done
    fi
}

setup() {
    echo "Setting up ssh / checking dependencies"
    echo "At passphrase prompts just press enter"
    if [ -f ./iphone ]
    then
        rm ./iphone 2> /dev/null
        rm ./iphone.pub 2> /dev/null
    fi
    ssh-keygen -t rsa -b 2048 -q -f iphone
    cat ./iphone.pub | ${SSH} "cat > ~/iphone.pub
        if [ ! -d /var/mobile/.ssh ]
        then
            mkdir /var/mobile/.ssh
        fi
        mv ~/iphone.pub ~/.ssh/authorized_keys"
}


if [ $? != 0 ]
then
        usage
fi

while getopts ":hcaAsu:i:" options
do
    case $options in
        s ) echo "Running initial setup. This can be run multiple times but only needed once"
            export RUN=setup
            ;;
        A ) export TYPE=""
            ;;
        a ) export TYPE=apps
            ;;
        c ) export TYPE=core
            ;;
        u ) export USER=${OPTARG}
            ;;
        i ) export IP=${OPTARG}
            export SSH="ssh -i ./iphone ${USER}@${IP}"
            export SCP="scp -r -i ./iphone ${USER}@${IP}"
            ;;
        h ) usage
            ;;
        * ) usage
            ;;
    esac
done
shift $(($OPTIND - 1))

if [ "${RUN}" == "setup" ]
then
    setup ${IP}
fi

if [ "${1}" == "backup" ]
then
    echo "Starting ${TYPE} Backup..."
    backup
fi
#--- END ---

http://pastie.org/614729


No comments: