Skip to content

Instantly share code, notes, and snippets.

@jonsowman
Created March 12, 2011 21:55
Show Gist options
  • Save jonsowman/867606 to your computer and use it in GitHub Desktop.
Save jonsowman/867606 to your computer and use it in GitHub Desktop.
iPhone SMS Backup to CSV
#!/bin/bash
#
# Retrieve the SMS database from the iPhone and convert it to a
# CSV file
#
# Usage: $ ./sms-backup.sh myiphone
#
# Dependencies: sqlite3
#
# Jon Sowman 2011
# jon@hexoc.com
#
# Mobile user for the phone
MOBILEUSER="mobile"
# The name of the SMS SQLite database on the phone
SMSDB="sms.db"
echo -e "--- SMS iPhone Backup Script ---"
echo -e "--- Jon Sowman 2011 ---"
echo -e "--- jon@hexoc.com ---\n"
if [[ $# -ne 1 ]]; then
echo "[USAGE] Usage: ./sms-backup.sh <iPhone hostname>"
echo "[USAGE] For example:"
echo "[USAGE] $ ./sms-backup.sh myiphone"
exit 1
fi
if [[ ! -x /usr/bin/sqlite3 ]]; then
echo "[ERROR] This script requires sqlite3 to be installed and executable."
echo "[ERROR] Exiting."
exit 1
fi
# Get the host option from the command line argument
HOST=$1
TIMESTAMP=`date +%d%m%y`
CSVFILE="sms-${TIMESTAMP}.csv"
TARFILE="sms-${TIMESTAMP}.tar.gz"
# Try to copy the SMS database from the phone to the local machine
echo -e "[INFO] Trying to connect to host using ${MOBILEUSER}@${HOST}"
scp -Bq ${MOBILEUSER}@${HOST}:~/Library/SMS/${SMSDB} .
# If we managed to retrieve the database, and use sqlite3 to convert
# it to a CSV file, before archiving both
if [[ $? -eq 0 ]]; then
echo -e "[INFO] Successfully got SMS database from ${HOST}"
sqlite3 -csv -separator ',' "${SMSDB}" "select * from message;" > \
"${CSVFILE}"
echo -e "[INFO] Wrote the SMS database on ${HOST} to ${CSVFILE}."
tar czf ${TARFILE} ${SMSDB} ${CSVFILE}
echo -e "[INFO] Archived ${SMSDB} and ${CSVFILE} to ${TARFILE}"
rm -f ${SMSDB} ${CSVFILE}
echo -e "[INFO] Removed ${SMSDB} and ${CSVFILE}"
echo -e "[INFO] Done!"
exit 0
else
echo -e "[ERROR] Could not retrieve ${SMSDB} from ${HOST}"
echo -e "[ERROR] Exiting."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment