Skip to content

Instantly share code, notes, and snippets.

@maiatoday maiatoday/getdb
Last active Dec 7, 2015

Embed
What would you like to do?
getdb - little shell script to pull the db for an app on an android emulator or device.
#!/bin/bash
display_usage() {
echo -e "\nUsage:\n`basename $0` -p [package] -n [dbname] \n"
}
# if less than two arguments supplied, display usage
if [ $# -le 2 ]
then
display_usage
exit 1
fi
while getopts "p:n:h" optname
do
case "$optname" in
"p")
echo "Using package $OPTARG"
packagename=$OPTARG
;;
"n")
echo "Using database $OPTARG"
dbname=$OPTARG
;;
"h")
display_usage
exit 1
;;
"?")
echo "Unknown option $OPTARG"
display_usage
exit 1
;;
":")
echo "No argument value for option $OPTARG"
display_usage
exit 1
;;
*)
# Should not occur
echo "Unknown error while processing options"
exit 1
;;
esac
done
DATE=$(date +"%Y%m%d%H%M%S")
devices=`adb devices`
devices=$(echo $devices | tr "\t" "\n" | tr -d "\r")
end=`expr "$devices" : ".*attached\s"`
devices=${devices:end}
devices=${devices//device/}
OIFS="$IFS"
IFS=' '
read -a arr <<< "${devices}"
IFS="$OIFS"
serial=${arr[0]}
arrLen=${#arr[@]}
if [ "$arrLen" -gt 1 ]; then
i=0
echo; echo "Choose device: "; echo " ------------"; echo
for device in "${arr[@]}"
do
echo " $i] $device"; let i++
done; echo
read -n 1 -p "Enter device number: " dnumber
if [ "$dnumber" -le "$arrLen" ]; then
serial=${arr[dnumber]}
else echo; echo "Device number incorrect."; exit;
fi
echo; echo;
fi
adb -s $serial shell run-as $packagename chmod 666 /data/data/$packagename/databases/$dbname.db
adb -s $serial shell run-as $packagename chmod 666 /data/data/$packagename/databases/$dbname.db-wal
adb -s $serial pull /data/data/$packagename/databases/$dbname.db new.db
adb -s $serial pull /data/data/$packagename/databases/$dbname.db-wal new.db-wal
adb -s $serial shell run-as $packagename chmod 600 /data/data/$packagename/databases/$dbname.db
adb -s $serial shell run-as $packagename chmod 600 /data/data/$packagename/databases/$dbname.db-wal
sqlite3 new.db VACUUM;
echo "Renaming db: $dbname-$DATE.db"
mv new.db $dbname-$DATE.db
echo "Done.."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.