Skip to content

Instantly share code, notes, and snippets.

@ftalbrecht
Created November 5, 2021 12:14
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 ftalbrecht/0fa9e9a6c7b38ccb4e46617ddd0ae6dd to your computer and use it in GitHub Desktop.
Save ftalbrecht/0fa9e9a6c7b38ccb4e46617ddd0ae6dd to your computer and use it in GitHub Desktop.
#!/bin/bash
# The display of the device needs to be turned on during the backup:
# System > Advanced > Developer options > Stay awake
# We require the position of the "Backup my data" button, find it with
# System > Advanced > Developer options > Input: Show taps + Pointer location
# and a manual data backup triggered by something like
# export APP=$(adb shell cmd package list packages -3 | tail -1)
# adb backup -f /tmp/to_be_removed ${APP:8}
export BUTTON_X=1150
export BUTTON_Y=2000
export BASEDIR=user-apps
mkdir -p "${BASEDIR}"
export TIME=$(date +%Y-%m-%dT%H-%M-%S)
#adb shell cmd package list packages -d > packages_disabled.txt
#adb shell cmd package list packages -e > packages_enabled.txt
#adb shell cmd package list packages -s > packages_system.txt
adb shell cmd package list packages -3 > ${BASEDIR}/installed_user_packages__${TIME}.txt
for LINE in $(adb shell cmd package list packages -3 -f); do
export WORDS=$(echo ${LINE} | sed "s/^package://" | sed "s/base.apk=/base.apk /")
export WORDS=($WORDS)
export REMOTE_APKPATH=${WORDS[0]}
export APP=${WORDS[1]}
echo "${APP}:"
if [ ${REMOTE_APKPATH::10} == "/data/app/" ]; then
export APPDIR="${BASEDIR}/${APP}"
mkdir -p "${APPDIR}"
echo -n " - backing up apk ... "
export LOCAL_APKPATH="${APPDIR}/${APP}__${TIME}.apk"
export DONE_APK="${APPDIR}/.${APP}__${TIME}.apk.SUCCEEDED"
adb pull -a "${REMOTE_APKPATH}" "${LOCAL_APKPATH}" &> /dev/null && touch "${DONE_APK}"
if [ -e "${DONE_APK}" ] ; then
echo "done"
rm "${DONE_APK}"
else
echo "FAILED! Retrying verbosely and continuing afterwards:"
adb pull -a "${REMOTE_APKPATH}" "${LOCAL_APKPATH}"
fi
echo -n " - backing up data ..."
export LOCAL_DATAPATH="${APPDIR}/${APP}__${TIME}.data"
export DONE_DATA="${APPDIR}/.${APP}__${TIME}.data.SUCCEEDED"
# start the backup process in the background, will open a pop-up on the device
adb backup -f "${LOCAL_DATAPATH}" "${APP}" &> /dev/null && touch "${DONE_DATA}" &
# record the pid of the just started process
PID=$!
# wait for the pop-up to appear on the device
sleep 1
# press the correct button
adb shell input tap $BUTTON_X $BUTTON_Y
# wait for the backup process to finish
while ps -p $PID > /dev/null; do sleep 1; done
if [ -e "${DONE_DATA}" ] ; then
echo "done"
rm "${DONE_DATA}"
else
echo "FAILED! Retrying verbosely and continuing afterwards:"
adb backup -f "${LOCAL_DATAPATH}" "${APP}"
fi
else
echo " - cannot backup (not residing in /data/app/)!"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment