January 4, 2019 · backup macos

Backing up iCloud Keychain on macOS 10.14

iCloud Keychain makes it extremely difficult to export your password data to a machine readable format, such as a .csv file. The reasoning behind this, is that Apple tries to keep its users within a closed ecosystem and by preventing easy migration of password data, they are increasing the barrier to switch to new platforms. Although I foresee myself being a longtime Mac user, I am not happy with the inability to backup my password manager. Fortunately, I have found an AppleScript that allows me to export my iCloud Keychain to a .csv file.

iCloud Keychain Backup Guide

This guide is for people running macOS Mojave 10.14.

  1. Open Script Editor on your Mac. Copy and paste the script at the bottom of the page.
  2. Look for the line after -- Type password - update to yours, that starts with keystroke "password", and change "password" to your user login password.
  3. Open a blank TextEdit document, and change the mode to Format -> Make Plain Text if it already isn't.
  4. Open Keychain Access and click on the iCloud Keychain at the top left. Then click the Passwords category at the bottom left.
  5. Sort the Keychain entries by Kind, descending. Select all the Web form password's, right click the selection, and note how many items there are. Then select just the first Keychain entry.
  6. In the Script Editor change repeat 1 times to what you noted earlier, e.g. repeat 201 times. Save the AppleScript.
  7. Click the play button in the Script Editor and wait a few minutes. My backup took around 30 minutes.
  8. You're done! Save the text file.

Backup AppleScript

repeat 1 times
 delay 0.2
 tell application "Keychain Access"
     activate
     tell application "System Events"
         delay 0.2
         keystroke return
         -- Press ⌘C to copy item title
         delay 0.2
         keystroke "c" using command down
     end tell
 end tell

 --Switch to TextEdit
 delay 0.2
 tell application "TextEdit"
     activate
     tell application "System Events"
         -- Press ⌘V
         delay 0.2
         keystroke "v" using command down
         -- Type a comma
         delay 0.2
         keystroke ","
     end tell
 end tell

 --Switch to Keychain
 tell application "Keychain Access"
     activate
     tell application "System Events"
         -- Press Tab x3 to get to item url 
         delay 0.1
         keystroke tab
         delay 0.1
         keystroke tab
         delay 0.1
         keystroke tab
         -- Press ⌘C
         delay 0.2
         keystroke "c" using command down
     end tell
 end tell

 --Switch to TextEdit
 delay 0.2
 tell application "TextEdit"
     activate
     tell application "System Events"
         -- Press ⌘V
         delay 0.2
         keystroke "v" using command down
         -- Type ','
         delay 0.2
         keystroke ","
     end tell
 end tell

 --Switch to Keychain
 tell application "Keychain Access"
     activate
     tell application "System Events"
         -- Press Shift-Tab to get to item account name
         delay 0.1
         keystroke tab using shift down
         -- Press ⌘C
         delay 0.2
         keystroke "c" using command down
     end tell
 end tell

 --Switch to TextEdit
 delay 0.2
 tell application "TextEdit"
     activate
     tell application "System Events"
         -- Press ⌘V
         delay 0.2
         keystroke "v" using command down
         -- Type ','
         delay 0.2
         keystroke ","
     end tell
 end tell

 --Switch to Keychain
 tell application "Keychain Access"
     activate
     tell application "System Events"
         -- Click the “Show password:” checkbox.
         delay 0.2
         keystroke "c" using {shift down, command down}
         
         -- Type password - update to yours
         delay 0.5
         keystroke "password"
         delay 0.2
         keystroke return
         -- Close keychain item window
         delay 0.3
         keystroke "w" using command down
         -- Go to next keychain item
         delay 0.2
         key code 125
     end tell
 end tell

 --Switch to TextEdit
 delay 0.2
 tell application "TextEdit"
     activate
     tell application "System Events"
         -- Press ⌘V
         delay 0.2
         keystroke "v" using command down
         -- Press ',' to end the item
         delay 0.2
         keystroke ","
         -- Press Return to start new item line
         delay 0.1
         keystroke return
     end tell
 end tell
end repeat
end run

References

https://discussions.agilebits.com/discussion/comment/455708/#Comment_455708