Skip to content

Instantly share code, notes, and snippets.

@npike
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npike/1726ac84c7b7df5576eb to your computer and use it in GitHub Desktop.
Save npike/1726ac84c7b7df5576eb to your computer and use it in GitHub Desktop.
Given the package name of an Android app on the device, trigger a backup via ADB and then extract the resulting backup file into the current directory. Useful for debuging sql databases in the app.
#!/bin/bash
# @author npike@phunware.com
#
# 1. Download script and store somewhere that makes you happy. Perhaps ~/Downloads on a Mac
# 2. From a terminal: chmod +x dump_appdata.sh
# 3. From a terminal: ./dump_appdata.sh com.my.app
# 4. Click "Back up my data" on connected device
# 5. Wait for backup and extraction to complete. An "app" folder will be written in the same directory
# of where this script is saved.
#
# If you have more than one device connected, you can specify a 2nd argument that is the device
# serial from "adb devices"
if [ "$#" -lt 1 ]; then
echo "dump_app.sh app_package [target_device_serial]"
exit 1
fi
APP_PACKAGE=$1
BACKUP_FILE=$APP_PACKAGE.adb_dump
DEVICE_TARGET=""
if [ $# -eq 2 ]; then
DEVICE_TARGET="-s $2"
echo "Will connect to device with serial $2 instead of auto."
fi
echo "Dumping app with package name of $1"
if [ -e ${BACKUP_FILE} ]; then
echo "Cleaning up existing backup file $BACKUP_FILE"
rm $BACKUP_FILE
fi
adb $DEVICE_TARGET backup -f ${BACKUP_FILE} -noapk $APP_PACKAGE
# Do not proceed if ADB couldn't initiate the backup on the device
if [[ $? != 0 ]] ; then
exit $?
fi
echo "Waiting for backup to finish...."
while :
do
lsof | grep ${BACKUP_FILE}
if [ $? -eq 0 ]; then
sleep 2
echo "."
else
break
fi
done
echo "Backup finished... extracting..."
if [ -e ${BACKUP_FILE} ]; then
dd if=${BACKUP_FILE} bs=1 skip=24 | openssl zlib -d | tar -xvf -
fi
echo "Finished."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment