Exporting messages and attachments from iPhone to Mac

Yesterday I accidentally deleted an important sms conversation from my Mac (for which I didn’t have a Time Machine backup). I thought it would be fairly easy to copy all my messages from my iPhone to my Mac, but it wasn’t that easy…

This guides helps you copy all your messages from iOS to OS X. You could invert the process to copy your messages from OS X to iOS.

Copying the databases

Before continuing, quit Messages app on your Mac.

First you need sms.db and Attachments folder from the iPhone. You can use a backup extractor tool to restore the file structure.
You can also manually find the backup folder in ~/Library/Application Support/MobileSync/Backup. The files in that backup folder are named with the SHA-1 hash of the original file in the iOS filesystem. The file for sms.db is called 3d0d7e5fb2ce288813306e4d4636395e047a3d28. Just rename it to sms.db and you have your SMS database. (I extracted the attachments folder with a third-party app)

Now backup ~/Library/Messages on your Mac and replace all its contents with sms.db and Attachments from your iPhone. Rename sms.db to chat.db.

Remove the ~/Library/Containers/com.apple.iChat folder.

Fixing attachments

On iOS, the messages are stored in ~/Library/SMS/ directory. This is not the case on OS X, where they are stored in ~/Library/Message/. This results in your attachments not showing op in Messages on Mac. In order to solve this, we need to execute a Sqlite query to rename SMS to Messages for each attachment:

UPDATE attachment
SET 
  filename = replace(filename, '/Library/SMS/Attachments/', '/Library/Messages/Attachments/')
WHERE
  filename LIKE '%/Library/SMS/Attachments/%';

I used DB Browser for SQLite to browse and edit the SQL database. You can execute the above SQL query by going to the Execute SQL tab.

Fixing version

It is also possible that you need to update the Sqlite version number. Otherwise Messages.app will mark the database file as incompatible. Again, we do this by executing a query:

UPDATE _SqliteDatabaseProperties
SET
    value = '9007'
WHERE key = '_ClientVersion';

I got the 9007 value from the chat.db database.

Restart

Once all of this is done, restart your Mac and verify that your messages have arrived in Messages app.