Vote Charlie!

Back up OSX Messages attachments incrementally

Posted at age 27.
Edited .

The Apple OS X Messages app stores files sent and received in a folder with a structure that doesn’t lend well to browsing. If you want to separately back up the family photos and videos received via iMessage on your computer, you can use some ideas here to help.

OSX Messages attachments folder

OSX Messages attachments folder" class="mt-image-none" height="602

Let’s say you have a folder messages_backup on your desktop where you have been manually copying received files into a separate folder for each family member. Within that folder, you might have two folders, to_sort and sorted. Within sorted is a folder for each family member. You used to manually drag and drop the files into the folders, but it has been hard to determine where you left off.

You can run the commands below to find the most recently created file (ignoring the annoying .DS_Store files) in the backup directory, and then copy all the attachments that are newer than that file into the to_sort folder.

SOURCE_DIR=~/Library/Messages/Attachments
BACKUP_DIR=~/Desktop/messages_backup/sorted
DEST_DIR=~/Desktop/messages_backup/to_sort
NEWEST=$(find $BACKUP_DIR -type f ! -iname ".*" -print0 | xargs -0 gstat --format '%Y :%y:%n' | sort -nr | cut -d: -f5- | head -1)
find $SOURCE_DIR -type f ! -iname ".*" -newer $NEWEST -exec cp -a {} $DEST_DIR  \;

Now you’ll have all the newer files in the to_sort directory, and you can drag them into the proper folders as you see fit.

Ideally we would copy the files directly into the contact folders, but that would require a more complicated script that searches for a match in the *.ichat files in ~/Library/Messages/Archive, and I didn’t need that. If you improve this, feel free to comment!

Note the above won’t work if the backup directory does not yet exist. You can insert these touch and mkdir commands just before the line beginning with NEWEST… to create the folders and create a dummy file with an old timestamp if you want to start using this process for the first time.

touch -t 198012311200 $BACKUP_DIR/temp_file
mkdir -p $DEST_DIR $BACKUP_DIR

Finally, to make this even more convenient, you could create an alias for these commands. For example, I added this line to my ~/.zshrc file (since I’m using Oh My Zsh:

alias getattachments='SOURCE_DIR=~/Library/Messages/Attachments && BACKUP_DIR=~/Desktop/messages_backup/sorted && DEST_DIR=~/Desktop/messages_backup/to_sort && NEWEST=$(find $BACKUP_DIR -type f ! -iname ".*" -print0 | xargs -0 gstat --format "%Y :%y:%n" | sort -nr | cut -d: -f5- | head -1) && find $SOURCE_DIR -type f ! -iname ".*" -newer $NEWEST -exec cp -a {} $DEST_DIR \;'

Now, after restarting Terminal, I just need to execute getattachments to update my to_sort folder.