Skip to content

Instantly share code, notes, and snippets.

@replete
Created May 5, 2021 16:22
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 replete/1ce9e46cd99b8b0371bf3b24875137af to your computer and use it in GitHub Desktop.
Save replete/1ce9e46cd99b8b0371bf3b24875137af to your computer and use it in GitHub Desktop.
Android adb Backup bash script with notifications/checks on Mac
#!/usr/bin/env bash
# Backup Android via ADB
set -euf -o pipefail
# configuration
ADB_DEVICE_ID="beefdead" # your device id from `adb devices`
OUT_DIR="/Volumes/SOMEDRIVE/Android Backups"
FILENAME="android-$(date +%Y-%m-%d-%H%M).ab"
# change to script directory
cd "$(dirname "$0")"
# check for adb
if [[ ! -x $(which adb) ]] ; then
echo "[-] ERROR: Required dependencies unmet. Please verify that the following are installed, executable, and in the PATH: adb" >&2
exit 1
fi
# Check device is connected first
ADB_DEVICES=$(adb devices)
if [[ $ADB_DEVICES != *"$ADB_DEVICE_ID"* ]]
then
echo "$ADB_DEVICE_ID is not connected - Aborting backup"
osascript -e 'display notification "Android Device is not connected" with title "Android Backup Failed"'
exit
fi
# Check base directory
if [[ ! -d "$OUT_DIR" ]]; then
echo "[-] ERROR: '$OUT_DIR' doesn't exist or isn't a directory" >&2
exit 1
fi
echo "[+] Verifying that destination directory is writable..." >&2
if [[ ! -w "$OUT_DIR" ]]; then
echo "[-] ERROR: '$OUT_DIR' isn't writable by current user" >&2
exit 1
fi
echo "[*] Using destination directory = $OUT_DIR" >&2
# Do the backup:
adb backup -apk -obb -shared -system -all -f "$OUT_DIR/$FILENAME"
# retval=$?
# echo $retval
# if [ $? -eq 0 ]; then
# echo "$(tail -1)"
# osascript -e 'display notification "Android Backup completed" with title "Android ADB Backup"'
# else
# echo FAIL
# osascript -e 'display notification "Android Backup FAILED" with title "Android ADB Backup FAIL"'
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment