Mac – software problem: retrieve contacts data from Time Machine

backupcontactstime-machine

My Macbook Pro under Sierra is now at the Repair Shop. I am using another MacBook Pro under Yosemite while my Sierra machine is being repaired. However, I forgot to export Contacts before taking my Sierra machine in this morning and now my Contacts data is 3 years old, which is pretty well useless to me. It should be no problem, because all the necessary info is on my Time Machine linked to Sierra. However, things are not so simple (at least for me).

How can I now get my contacts info onto the Yosemite machine from the Sierra-based Time Machine backup? It seems that none of the published solutions works.

Best Answer

Update: I have updated the script as per @user3439894’s feedback. The script should now work for most users, no matter what flavor their address book raw data is in.


You might want to look at a temporary command-line solution. It gives you a contact <search_term> command that you can run in Terminal.app to look up contacts.

I assume your Yosemite MBP is only a temporary solution while your Sierra MBP is at the repair shop. While the Terminal command has limited functionality, it might work well enough for you to be useful while you wait for your Sierra MacBook Pro to return.

Setting up the temporary command should require not more than 10–15 minutes.

Setting up the contact command

  1. Open the Time Machine backup that contains your (Sierra-based) address book data.

  2. In your backup, look for the folder "${HOME}/Library/Application Support/AddressBook", which is the root folder for all your contact data.

  3. Restore that entire AddressBook folder to a temporary location, e. g.: ${HOME}/SierraAddressBook.

  4. Double check that the whole ${HOME}/SierraAddressBook tree has the correct permissions so you can read it without an administrator password.

  5. For good measure, make a backup copy of your ${HOME}/.bash_profile file.

  6. Open the file ${HOME}/.bash_profile in a text editor of your choice.

  7. Temporarily append to .bash_profile the following lines:

(Note that for some of these lines, indentation matters; try not to change indentation in any of those lines, even if it looks a bit off.)

contact() {
  # We assume "${HOME}/Library/Application Support/AddressBook"
  # has been restored to this directory from a TM backup.
  local contacts_base_dir="${HOME}/SierraAddressBook"

  local search_term="$1"
  local sql_code="$(cat << EOF
.mode column
      SELECT
        TRIM(       IFNULL(R.ZFIRSTNAME, '')
          || ' ' || IFNULL(R.ZLASTNAME, '')
          || ' ' || IFNULL(R.ZORGANIZATION, '')) NAME,
        'created ' || date('2001-01-01', ZCREATIONDATE
          || ' seconds', 'localtime') CREATED,
        'modified ' || date('2001-01-01', ZMODIFICATIONDATE
          || ' seconds', 'localtime') MODIFIED,
        GROUP_CONCAT(DISTINCT TRIM(IFNULL(E.ZLABEL, ''),
          '! _$<>') || ' ' || IFNULL(E.ZADDRESSNORMALIZED, ''))
          EMAIL,
        GROUP_CONCAT(DISTINCT TRIM(IFNULL(P.ZLABEL, ''),
          '! _$<>') || ' ' || IFNULL(P.ZFULLNUMBER, ''))
          PHONE
      FROM
        ZABCDRECORD R
      LEFT JOIN
        ZABCDEMAILADDRESS E ON E.ZOWNER = R.Z_PK,
        ZABCDPHONENUMBER P ON P.ZOWNER = R.Z_PK
      WHERE
        NAME LIKE '%${search_term}%'
      GROUP BY
        R.Z_PK
      ORDER BY
        NAME;
EOF
    )"

  export sql_code
  find "${contacts_base_dir}" -regex '.*/AddressBook-[^/]*\.abcddb$' \
    -exec bash -c "/usr/bin/sqlite3 '{}' <<< \"\${sql_code}\"" \;
}

export -f contact
  1. Save your changes in .bash_profile.

  2. Open Terminal.app.

  3. Open a new terminal window.

Usage

Run the command contact followed by your search term in the Terminal. For example:

$ contact "Bella Napoli"

Bella Napoli  created 2013-10-12  modified 2015-05-29              Home +49 931 51706

$ contact IKEA

IKEA Frankfurt  created 2013-10-12  modified 2013-10-12              Work 0180 5 353435

$ contact Neptun

Hotel Neptun  created 2013-10-12  modified 2013-10-12  Work concierge@hotel-neptun.de
Work +49 381 777-0,WorkFAX +49 381 54023

$ contact

<lists all your contacts>

Caveats

  • The search term only applies to first name, last name, and organization name.
  • The search term is case sensitive.
  • The search term cannot contain spaces or certain punctuation, unless it is enclosed in the proper single or double quotes.
  • To search by full name, use the correct order: <first_name> <last_name> <organization_name>
  • Search terms can also be substrings of names.
  • I do not endorse any of the companies and their products and services I gave in the example. I used those because I cannot disclose personal data for obvious reasons. Besides, I was too lazy to add bogus data to my own address book.
  • I’ve tested the script on El Capitan only but it should work on Yosemite, even when combined with Sierra-based address book data.
  • If you run into issues, feel free to leave a comment below.