Android, Programming, Tips & Tricks

How to Backup Contacts and SMS Messages using command line in Android

The messages are available via the Telephony and contacts via the Contacts provider. The data is stored in SQLite databases in the following locations:

/data/data/com.android.providers.telephony/databases/mmssms.db
/data/data/com.android.providers.contacts/databases/contacts2.db
If you have the Android SDK installed and the phone connected to you computer, to do the backup, you simply need to copy these files from the phone to the computer. You can do this with the “adb pull” command. Here are the commands:

cd <android-sdk-dir>/platform-tools
./adb pull /data/data/com.android.providers.telephony/databases/mmssms.db
./adb pull /data/data/com.android.providers.contacts/databases/contacts2.db
After executing this commands you’ll have the databases in your “<android-sdk-dir>/platform-tools” folder. If you want to examine the databases you can use some SQLite browser. I’m using the SQLite Manager addon for Firefox.

If you want to put the backed up files back on the phone, you’ll have to remount the /system partition in read-write mode, delete the present database files (if there are [present) and push the files back on the phone.

First check to see where the which physical partition is mounted on “/system” to know where you should remount it:

./adb shell mount
Search for the “/dev/block/” part on the line with “/system”. On my phone which is running CyanogenMod 6 it is “/dev/block/mtdblock3″.

After discovering which physical partition is mounted at “/system” you will have to remount that partition as read-write, delete the databases and push the backed up ones.

./adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
./adb shell rm /data/data/com.android.providers.telephony/databases/mmssms.db
./adb shell rm /data/data/com.android.providers.contacts/databases/contacts2.db
./adb push mmssms.db /data/data/com.android.providers.telephony/databases/
./adb push ~/Desktop/phone/contacts2.db /data/data/com.android.providers.contacts/databases/
After doing the restoration of the databases if it happens that you don’t see either your SMS messages or your contacts you can try copying the databases to your SD card and then by using some file manager with root privileges to place them on the appropriate places.

For more details please refer http://chombium.wordpress.com/2012/09/30/android-how-to-backup-contacts-and-sms-messages/