Skip to content

Instantly share code, notes, and snippets.

@varenc
Created February 3, 2021 01:16
Show Gist options
  • Save varenc/473b21917fa72b480537a4124b2d7d2e to your computer and use it in GitHub Desktop.
Save varenc/473b21917fa72b480537a4124b2d7d2e to your computer and use it in GitHub Desktop.
Fork of PeterKaminski09 baskup.sh with some improvements
#!/bin/sh
## this is a fork of https://github.com/PeterKaminski09/baskup/blob/master/baskup.sh with some bug fixes
# - improve the "Me/Friend" regex so that it only matches what its supposed to doesn't match things at the end of the line
# - The NEEDS_MODIFICATION logic was broken so just set it to always true since most everyone is running 10.13+
# - Create "OG_<contact>.txt" files that have the original, pre-regex, content in case the regex destroyed info
BACKUP_DIR=./backup
OS_Version=$(sw_vers -productVersion)
LAST_VERSION=10.13
# NEEDS_MODIFICATION=$(echo $OS_Version '>=' $LAST_VERSION | bc -l)
NEEDS_MODIFICATION=1
BACKUP_ATTACHMENTS=0
while getopts ":a" opt; do
case $opt in
a)
echo "Running baskup for text + attachments"
BACKUP_ATTACHMENTS=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
function select_rows () {
sqlite3 ~/Library/Messages/chat.db "$1"
}
for line in $(select_rows "select distinct guid from chat;" ); do
contact=$line
arrIN=(${contact//;/ })
contactNumber=${arrIN[2]}
echo "YAY Working on contact: $contactNumber (line = $line)";
#Make a directory specifically for this folder
mkdir -p $BACKUP_DIR/$contactNumber/Attachments
#Perform SQL operations
if [[ $NEEDS_MODIFICATION == 1 ]]; then
select_rows "
select is_from_me,text, datetime((date/1000000000) + strftime('%s', '2001-01-01 00:00:00'), 'unixepoch', 'localtime') as date, handle_id from message where handle_id=(
select handle_id from chat_handle_join where chat_id=(
select ROWID from chat where guid='$line')
)" | tee $BACKUP_DIR/$contactNumber/OG_${line}.txt | sed 's/^1\|/Me: /g;s/^0\|/Friend: /g' > $BACKUP_DIR/$contactNumber/$line.txt
else
select_rows "
select is_from_me,text, datetime(date + strftime('%s', '2001-01-01 00:00:00'), 'unixepoch', 'localtime') as date from message where handle_id=(
select handle_id from chat_handle_join where chat_id=(
select ROWID from chat where guid='$line')
)" | tee $BACKUP_DIR/$contactNumber/OG_${line}.txt | sed 's/^1\|/Me: /g;s/^0\|/Friend: /g' > $BACKUP_DIR/$contactNumber/$line.txt
fi
if [[ $BACKUP_ATTACHMENTS == 1 ]]; then
select_rows "
select filename from attachment where rowid in (
select attachment_id from message_attachment_join where message_id in (
select rowid from message where cache_has_attachments=1 and handle_id=(
select handle_id from chat_handle_join where chat_id=(
select ROWID from chat where guid='$line')
)))" | cut -c 2- | awk -v home=$HOME '{print home $0}' | tr '\n' '\0' | xargs -0 -I fname cp fname $BACKUP_DIR/$contactNumber/Attachments
fi
done
@mattneub
Copy link

mattneub commented Aug 5, 2021

Very nice; works much better than the original script. What's the purpose of the OG variants of the text files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment