Remove All Ads from XDA

[GUIDE] Extract and Restore Contacts, SMS, Call logs from nandroid backups(TWRP)

1,452 posts
Thanks Meter: 634
By Mirhawk, Senior Member on 29th November 2015, 02:59 AM
Post Reply Email Thread
So yesterday My phone, Sony Xperia S, decided to go into bootloop and not boot at all, no matter what I did. Tried flashing stock PRF, stock firmware, some stock based firmware, but it would get stuck in bootloop. The reason for this being I was not formatting the data partition, which I needed to preserve since it had all My sms, contacts, call logs and other app data. Luckily, I had a working recovery(TWRP) and was able to take a backup. Then using that I was able to recover My contacts, sms, call logs and app data. After a lot of Google searches, I was finally able to work it out. The process is as follows:

Needed software:
Code: from here.
Firefox Browser
Firefox addon of SQLite manager : link.
Excel macro to convert .db contacts to .csv : link.
Software for csv to vcard conversion (Windows) : link.
CSV to vcard conversion (online) : link.
SMS Backup & Restore android app : link.
Call Log Backup and Restore app : link.
The procedure is a lot crude as of yet, I would update as more universal and convenient options are found.
I know there is Titanium backup app which can restore contact, sms etc from nandroid backups, but You need a paid version of the app.

First we need to extract the data partition of nandroid
  1. Rename the to data.ext4.tar.gz . If Your files are like data.ext4.win000, data.ext4.win001, then rename each of them to data.ext4.tar.gz in separate folders.
  2. On windows, now You can directly extract them using WinRaR or 7zip application, although You don’t need to extract it entirely.
  3. On linux, You can do so using the following command. It is recommended to store the file in a separate folder as the file gets extracted directly.
    tar –xf data.ext4.tar.gz
  4. Now we need to find two files contacts2.db and mmssms.db . The contacts2.db files is for contacts as well as the call logs and the mmssms.db is for messages. The contacts2.db is constant across most ROM’s and OEM firmware apps. The mmssms.db file may vary as per app and ROM’s. So what You need to do is look for a .db file inside the SMS app You have been using with the firmware/ROM.

We will restore the SMS and Call logs first.
Currently it is limited to Windows OS only.
  1. Extract the contents of to a folder.
  2. Copy the mmssms.db file and contacts2.db file to the above folder.
  3. Now open a command prompt at location of the above folder. This can be done by opening the folder, right clicking using shift key pressed in blank space. You get the “Open command window here” option.
  4. In it type:
    extract -s mmssms.db
  5. It will ask You to enter a name for the output .xml file, You can press enter to let it give a default name. This will convert Your sms to a .xml file .
  6. Then type:
    extract –c contacts2.db
  7. It will also ask You to enter a name for the output .xml file, You can press enter to let it give a default name. This will convert Your call logs to a .xml file .
  8. Copy both of these files to Your newly formatted phone and then use the two before mentioned apps (Call Log backup& Restore and SMS Backup & Restore) to restore Your SMS and call logs.

Now retrieving contacts. It is a bit lengthy, but simple process.
  1. Start firefox browser. In it, after You have installed the addon do following to enable the addon
    1. Go to options (3 horizontal lines).
    2. Select “Customize” and drag the “SQLite Manager” to the blue bordered box.
  2. Now open options and select SQLite Manager. In it select Database>Connect Database. In the opened dialog box, select All Files(*.*). Then select Your contacts2.db .
  3. From the left side tree structure in the SQLite Manager window, right click on “data” and select “Export Table”.
  4. Now in right side part of the window, You will get CSV tab and SQL tab. Under CSV tab, simply press OK button without changing any other options.
  5. It will ask You to save Your .csv file. Save it.
  6. Now if You try to open this file directly, You will notice that file is not properly arranged.
  7. To arrange it properly, open the convert-contacts2.db-data-to-vcard-vcf.xlsm file. If You don’t know how to enable Macros, then do the following:
    1. It will show You a warning “Security Warning Macros have been disabled. Options..”
    2. Press the Options button. Select “Enable this content” and press ok.
    3. Now Press the office button present at left top corner. In it select “Excel Options”.
    4. Select Customize tab from left side. Then from “Choose commands from:” drop down menu, select Macros.
    5. Now add convertDatatoContacts and writeContactstovCard to right pane from left pane by selecting them and pressing add. Press OK button.
  8. Now as You can see two white square icons have been added at top right corner next to save undo redo options.
  9. Select the first white square icon. Select “Enable Macros” button. Now select Your .csv file in the opened dialog box.
  10. Now the file must have opened in a separate excel. The format should be 4 rows with they being First Name, Last Name, Phone 1 and Phone 2.
  11. Then select Save As>Excel Workbook>Save Type as CSV(Comma delimited).csv . Press ok and yes on the two warnings generated.
  12. At the left bottom, right click on “contacts” sheet and press delete. Save and close the file.
  13. Run CSV to vCard software and open the above saved .csv file. Here You can save all contacts as single vCard file or separate files.
  14. In the “Properties” section on the window, in front of “Phone 1” and “Phone 2” select “Mobile Phone” from options. Press “Convert” button.
  15. Alternative to above two steps, You can also convert .csv to vcard using the website.
  16. Now a .vcf file will be created where the .csv file was stored.
  17. Now simply copy the .vcf file to phone and import using the phone contacts app!
  18. Done!

Credits and sources:
[TOOL] yaffs extractor, mmssms.db & contacts2.db converter
The Following 23 Users Say Thank You to Mirhawk For This Useful Post: [ View ] Gift Mirhawk Ad-Free
29th November 2015, 02:59 AM |#2  
Mirhawk's Avatar
OP Senior Member
Thanks Meter: 634
29th November 2015, 01:01 PM |#3  
odie70's Avatar
Senior Member
Flag Pontianak
Thanks Meter: 30
This is complicated, but youre great mate...

Sent from my C6903 using Tapatalk
30th November 2015, 04:24 AM |#4  
Mirhawk's Avatar
OP Senior Member
Thanks Meter: 634
Originally Posted by odie70

This is complicated, but youre great mate...

Sent from my C6903 using Tapatalk

Ah yes, it's a lot complicated, but the need of recovering data was a lot important too!
23rd March 2016, 12:08 AM |#5  
Junior Member
Thanks Meter: 0
Praise to you for saving me !
And if I may, I'd like to state my experience, which brings a few interesting facts into light:
1/ I was unable to open my tar.gz archives with 7-zip v9.20. It kept displaying an error message. Only the v15.14 version could handle them flawlessly.
2/ as a textra user, I discovered that it stores a duplicate sms database, which IS NOT compatible with yaffs-mmssmsdb-calls-extractor (which screams a message about a "missing sms table" or something) (praise to the author as well)
BUT, the system sms database can be found here, and is fully handled by the extractor tool:
data\data\\database s\mmssms.db

Now my sms are back and I can resume my thread about the best knitting techniques.
Thanks again
27th September 2016, 08:30 PM |#6  
Junior Member
Thanks Meter: 0
Add this cool service Online converter contacts2.db to vCard
27th October 2016, 10:12 AM |#7  
Junior Member
Thanks Meter: 5
OP - you're a deadset legend.
28th October 2016, 11:59 AM |#8  
Senior Member
Thanks Meter: 920
This is damn useful!
Even if I do not need it right now but it is very good to know that there is a way.
14th March 2017, 06:34 AM |#9  
Junior Member
Thanks Meter: 0
Hello @Mirhawk, I am having this error when running the macro. What could be the issue?
12th December 2017, 04:31 PM |#10  
Thanks Meter: 16
Thanks for this. I wanted to comment to help others using Microsoft Office 2016 & now that Firefox Quantum has made old extensions incompatible. Call logs & SMS are easily imported by Nandroid Manager app (god bless the dev). This a short summary of steps to restore contacts, presuming you have the contacts2.db file extracted:

- Install Firefox 39 from here. Might work with FF 57, but did not want to take chances.
- Download the Excel Macro [which will help us later correctly arrange our data via pre-set changes, aka macros, on the Excel spreadsheet][see link above]
- Install & enable the SQLite extension in Firefox (already linked above by OP)
- Follow OP's steps until you obtain the .csv file
- Open the macro file you had downloaded earlier & click enable the content
- In Excel 2016, you need to add Developer Tab to your top menu: Go to File -> Options -> Customize Ribbon -> Check mark Developer option on the right side column. Click OK.
- Now we need to run the macros: Click on Developer Tab at the top -> Macros -> Run the first macro: convertDatatoContacts
- Select the .csv file & proceed
- Now run the second macro: writeContactstovCard
- Vcard (.vcf) is saved to same folder as your original macro file.

In Nougat, Contacts app can open and import .vcf file or you can use third-party app.
18th December 2017, 12:10 PM |#11  
Xelasarg's Avatar
Senior Member
Thanks Meter: 74
Great tutorial, thank you very much! This saved 6000 SMS when Titanium Backup had messed up. 🙂👍
Post Reply Subscribe to Thread

call logs, contacts, restore, sms, twrp
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes