[Galaxy S II Q&A, Help & Troubleshooting] E:V:A:
All About Contacts Import/Export, Backup/Restore and Data/Sync

Last Change: 2011-12-29

Content:

Details about how to backup and restore Contacts on the

Samsung Galaxy S I & II, including data storage info.

Summary:

Backing up and restoring your contacts on the latest Android OS

based phones is a nightmare as long as you do not want to use

automatic sync via Google or Outlook Express. In fact without

paying money for some specialized Application to do it properly

for you, it is a mess. Here I feel Samsung have really failed.

Why there isn't already a built-in simple-to-use contact backup

functionality, that doesn't depend on Google, Kies, Bluetooth

or other net-based service, is way beyond me.

Anyway, here is my attempt to remedy this situation and the

various options you have to backup and restore all your contacts

without:

a) using Google, Microsoft, Kies or other wireless protocol and

b) paying for specialized applications or software.

To simplify this exercise (a lot) we will assume that we would be

satisfied with backup and restore of only the most essential information.

This means (for me):

- first_name

- last_name

- primary_phone

- primary_email

So let's see how far we can get...

NOTE:

a) All of this tutorial assumes you have a rooted phone and

that you are operating as the root user (su)!

b) AOS phones doesn't generally come with the SQLite3

command-line binary, so this have to be installed manually!

Keywords:

Contacts, Restore, CSV, vCard, SQL,

Backup, Data, Sync, SQLite3, Import, Export, VCF

​

Content​

- Introduction

- Remounting /system as Read/Write

- Installing the SQLite Command-Line Interpreter

- SQLite3 Install Script

- Contacts Database & Storage-Location:

- Contacts DB Location List

- Direct Manipulation of the Contacts Database

- Other Contact Import / Export Applications

- Help Needed!
​

Introduction​

Essentially there are only 4 different ways to backup your Android contacts.

1. By using a backup application like Kies or 3rd party one from Android Market.

2. By syncing your contacts to your Google account.

3. By copying the entire "Contacts" database to your PC.

4. By exporting only the relevant data directly on your phone, using SQLite.

In this post I will only talk about the last 2 options, with focus on (4) as it applies to (3) as well.

​

Remounting /system as Read/Write​

Any manipulation of the system partitions and the root directory require WRITE permission to the file-system. To find out, just use the mount command and look for the "rw" label in the filesystem description of the / and /system partitions. For example:

# mount
...
rootfs / rootfs ro,relatime 0 0
/dev/block/mmcblk0p9 /system ext4 ro,relatime,barrier=1,data=ordered 0 0
/dev/block/mmcblk0p10 /data ext4 rw,nosuid,nodev,noatime,...,discard 0 0
/dev/block/mmcblk0p4 /mnt/.lfs j4fs rw,relatime 0 0
/dev/block/vold/179:11 /mnt/sdcard vfat rw,dirsync,...,utf8,...,discard 0 0
...

See the "rw" following "/system ext4" and "/data ext4" above?

Yes? Good, you can skip to the next section!

No? Then you need to remount the filesystem as READ/WRITE.

("rw" = "Read AND Write" and "ro" = "Read Only".)

!! However, this may depend on your model AND on how you connected your phone to USB. You can enter the phone directly without using the "file-transfer" mode in which case you may or may not have RW, but if you select to use "file-transfer" (aka. USB-disk mode) you may only get RO. In some cases the /mnt/sdcard is simply never mounted. Try and test! [*** check this! ***]

The way to get RW, is to remap the "physical" device (disk) location to the system partition you are interested in. The tricky part is knowing the physical device name of your system disk which may not always be the same as indicated from the mount command. If not, try to inspect the boot-log messages with the "motd" command, or inspecting the file "/init.rc" for the details of everything that is mounted upon boot. (Boot-log messages are normally enabled in most stock Kernels, but not necessarily in modded ROM's.)

For the SGS-2 which uses the EXT4 filesystem, I had to use:

mount -o remount,rw -t ext4 /dev/block/mmcblk0p9 /system

For the SGS-1 when using an RFS based AOS, I had to use:

mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system

To undo this you just replace "rw" with "ro" in the statements above. Although this is not necessary, since it will be automatically reverted after next reboot, which is recomended anyway after manually editing any system DB.

​

Installing the SQLite Command-Line Interpreter​

Most phones doesn't come with the SQLite3 command-line interpreter/interface (CLI) installed, unless they are rooted with a heavily modded ROM. The CLI is needed to manually access the SQLite Contacts database. Thus you need to check that it is not already installed and install it manually, if it is not. The binary (sqlite3) is normally located in either /system/bin or /system/xbin. To check if you have it, try the following command:

find / -iname '*sqlite*'

If it fails to return the files shown below, you need to install the SQLite3 binary AND possibly the relevant library files. You should already have most of the required library (lib*.so) files, as they are constantly used by the system, so look for them first! The files you need are:

/system/bin/sqlite3

/system/lib/libsqlite.so

/system/lib/libsqlite_jni.so

If the binary is compiled from CyanogenMod sources, you will also need the following:

/system/lib/libncurses.so

In the worst case you can go through the exercise to compile the SQLite3 sources for the MIPS32 chipset by yourself. [http://www.sqlite.org/] Also it's worth to note that some custom ROM's now include the latest compiled version. When you have the binary you just put it on your SD card and copy it to the root file system binary directory. Many ROM's and rooted phones use an additional directory /system/xbin where you can put your own binaries. If this directory (and path variable) is not already present, use:

cp <path_to_your_sqlite3_binary> /system/bin/sqlite3
chmod 4755 /system/bin/sqlite3
chown root.shell /system/bin/sqlite3

(Note that this version of chown uses a "." in the "user.group" specification.)

Do the same for the library files, if they are missing. You should now be able to run SQLite3:

# sqlite3 -version
3.6.22

Everything OK? Good!

​

SQLite3 Install Script​

!!MODIFY FIRST!!

#!/bin/sh
# squint.sh - sqlite3 install script for rooted Androids
#
#-rwsr-sr-x root shell 22228 2011-11-10 12:53 /system/bin/su
#-rwsr-xr-x root shell 1075144 2011-12-15 16:55 /system/xbin/busybox
#-rw-r--r-- root root 322120 2011-09-14 14:45 /system/lib/libsqlite.so

# Go to your "sqlite" folder on your (internal) SD card:
cd /mnt/sdcard/sqlite/

cp sqlite3 /system/xbin/sqlite3
chmod 4755 /system/xbin/sqlite3
chown root.shell /system/xbin/sqlite3

cp ./lib/libncurses.so /system/lib/libncurses.so
chmod 644 /system/lib/libncurses.so
chown root.root /system/lib/libncurses.so

Contacts Database & Storage-Location​

Apparently the SQLite3 database used to store the Contacts data is located in different parts on different Android models [?] and different Android OS versions. I have not been able to verify this, as I don't have access to these phones, but from from reading various forum posts for other phones this seem to be the case.

The way you can check is by looking for the raw database file on your system. On the phones I have, the location can be found here:

SGS-1 (2.3.6): /dbdata/databases/com.android.providers.contacts/contacts.db

SGS-2 (2.3.4): /data/data/com.android.providers.contacts/databases/contacts2.db

It may be important to notice that the "com.android.providers.contacts" part of this path, is actually referring to the particular "Contacts.apk" application that is installed in your phone. Therefore this location can differ from other versions of that application and thus also your phone model and OS version. In addition note that the /data and /dbdata or "whatever" partitions may be mounted on a different block device, as noted in the first section above. For example, on my SGS-2, I had /data mounted on /dev/block/mmcblk0p10 .

The more intelligent way is to look for all .db files on you system by doing some shell magic:

find / -iname '*.db'

But today, almost every application is using a database to save information, so to narrow down the long list, filter your search with "ontact":

find /system -iname '*.db' |grep ontact

Contacts DB Location List​

GT-I9000 (2.3.6): /dbdata/databases/com.android.providers.contacts/contacts.db

GT-I9100 (2.3.4): /data/data/com.android.providers.contacts/databases/contacts2.db

[I'd like to keep this list updated with other models and AOS versions, so please let me know what you have.]

​

Direct Manipulation of the Contacts Database​

In order to make any changes to any information contained in the database, you need to have write permissions set on the the relevant partition(s) as explained above. Remount as needed.

If you for some crazy reason need to replace/restore your entire contact database with another one. (This is strongly discouraged!) You also need to make sure that the one you replace with have both the same ownership (chown) and permissions (chmod) as the original one.

Next, let's open the contacts database:

# cd /data/data/com.android.providers.contacts/databases
# sqlite3 contacts2.db

SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"

sqlite> .databases

.databases
seq name file
--- --------------- ----------------------------------------------------------
0 main /data/data/com.android.providers.contacts/databases/contacts2.db

sqlite> .tables

.tables
_sync_state settings
_sync_state_metadata speed_dial
accounts status_updates
activities v1_settings
agg_exceptions view_contacts
android_metadata view_contacts_restricted
calls view_data
contact_entities_view view_data_restricted
contact_entities_view_restricted view_groups
contacts view_raw_contacts
data view_raw_contacts_restricted
groups view_v1_contact_methods
mimetypes view_v1_extensions
name_lookup view_v1_group_membership
nickname_lookup view_v1_groups
packages view_v1_organizations
phone_lookup view_v1_people
properties view_v1_phones
raw_contacts view_v1_photos

sqlite> SELECT _id,name, number FROM view_v1_phones;

[...]

To output the result as a CSV table to a file called "my_contacts_db.csv" with the first row containing the (column) headers and bailing out on errors, do this:

sqlite> .bail ON
sqlite> .headers ON
sqlite> .mode csv
sqlite> .output my_contacts_db.csv
sqlite> SELECT _id,name, number FROM view_v1_phones;
sqlite> .output stdout
sqlite> .quit

As you can see, if we had known the precise query statements we would have already been done with our export. But the Contacts.db tables are rather complex for our purposes of simply extracting the most relevant contact information. I lazily gave up after trying some basic SQL select statements...

[So yeah, please send me a good SQL statement for selecting and printing only the basics as a CSV!]

The AOS java API for using the contacts are MUCH simpler to use than the needed SQLite statements, for doing the extractions/replacements. But then you also need to write the program.

http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

There are many such programs out there, but very few who does the job well. (Everybody want to make money!) So try some Google-Fu on sites such as:

http://www.sourceforge.com/

http://code.google.com/

http://www.github.com/

http://www.stackexchange.com/

http://grepcode.com/

and use the keywords in the top of this document.

​

Other Contact Import / Export Applications​

But I'm not a Java Programmer. (What?!!) So instead I had a look at the most simple applications for exporting and importing you contacts. Being reasonably paranoid as a person, I did not trust huge multifunction backup-applications, knowing that they are full of adware, spyware and other "things" we may not like. With these limits I knew that any such application should not be much larger than 100 kilobytes.

I found 4 small applications (and one very large) on the Market that I tested:

1. Contacts CSV Export (93 K)

https://market.android.com/details?id=dutchandroid.contacts.export

(contacts.export-1060-1.060.apk, dutchandroid.contacts.export)

2. Contacts Backup Trial (168K)

https://market.android.com/details?id=com.saturnat.android.contactsbackup.trial

(contactsbackup.trial-34-2.9.5.apk, com.saturnat.android.contactsbackup.trial)

3. Import Contacts (56 K)

https://market.android.com/details?id=org.waxworlds.edam.importcontacts

(import-contacts_1.1.apk, org.waxworlds.edam.importcontacts)

4. Offline Contacts Sync (27 K)

https://market.android.com/details?id=com.beforemadness

(offlineContactsSync.apk, com.beforemadness)

5. GO Contacts EX (2.7 M!)

https://market.android.com/details?id=com.jbapps.contactpro
​

(1) Is a flexible lightweight CSV contacts exporter. You can manually choose CSV delimiters (",;TAB" etc) or choose a standard (Mozilla, Hotmail and Outlook etc.) CSV format.

(2) $$$, Is buggy with a no-sense UI, doesn't catch Email addresses, don't let you choose filename, and contain trackers from Facebook and Mixpanel. Limited trial to 10 contacts and 3 days before expiration.

(3) Only imports vCard (.vcf) files. One-by-one or scan a directory. Ask's what to do (skip,merge,replace) with duplicates.

(4) No-sense UI. Has export function but it is not working. And import is only working partially.

(5) Exports all contacts to a single vCard file, but missed my Emails. Nice merge and batch-delete functionality. Nice interface, and works. However, this app is huge, and god knows what other bloat junk it contains.

​

Other Useful Links​

[1] http://www.sqlite.org/

[2] http://www.androidadb.com/source/pl...ndroid/contacts/ImportVCardActivity.java.html

[3] http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/

[4] sed Tutorial: http://www.grymoire.com/Unix/Sed.html

​

Help Needed!​

If you have any detailed knowledge about where and how the relevant contact data is stored on your device, please let me know and include details such as:

- Your phone model number

- Your general Android OS version [Settings --> About Phone ]

- Your phone firmware version (PDA / PHONE / CSC) ["dial" --> *#1234#]

- The precise location of the contacts database file

E.g.

GT-I9100, Gingerbread 2.3.4, XWKI4/XXKI1/OXX KI2 (SEB)

/dbdata/databases/com.android.providers.contacts/contacts.db

If you have already been poking around your OS it would also be helpful to know:

- The version and type of database used (SQLite3 etc.)

- The particular DB tables used to obtain:

+ First Name

+ Last Name

+ Phone Number "1"

+ Email "1"

Here I have written "1" to signify the primary number used. I have refrained from using more details as this, as I consider this minimal but sufficient for backing up and restoring my contacts.

​

Some useful SQLite queries​

sqlite3 -csv contacts2.db "SELECT _id,name,primary_email,number FROM view_v1_phones;"
sqlite3 -csv contacts2.db "SELECT _id,mimetype_id,raw_contact_id,data1 FROM data;"
sqlite3 -csv contacts2.db "SELECT _id,display_name FROM raw_contacts WHERE account_type='vnd.sec.contact.phone';"
sqlite3 -csv contacts2.db "SELECT data1 FROM data WHERE raw_contact_id=112 AND mimetype_id=5;"

sqlite3 -csv -header contacts2.db "SELECT _id,package_id,mimetype_id,raw_contact_id,is_primary,is_super_primary,data_version,data1,data2,data3 FROM data WHERE raw_contact_id=21;"
sqlite3 -csv -header contacts2.db "SELECT _id,package_id,mimetype_id,raw_contact_id,is_primary,is_super_primary,data_version,data1,data2,data3 FROM data WHERE is_primary=0 AND data_version=0;"

#SELECT data1 FROM data WHERE raw_contact_id=140 AND mimetype_id=1;
#SELECT data1 FROM data WHERE raw_contact_id=140 AND mimetype_id=5 AND data2=2;
#SELECT data1 FROM data WHERE raw_contact_id=112 AND mimetype_id=5 AND data2=7;
#SELECT data1 FROM data WHERE raw_contact_id=112 AND mimetype_id=5 AND data2=1;

If this helped you in any way, please hit the THANK YOU button!
​