Created
February 21, 2021 05:23
-
-
Save bryanjhv/ecfc9ab5128b7d1d7b9c95a9997e9442 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ADB=${ADB:-adb} | |
DEST=${DEST:-apks} | |
if [ $# -lt 1 ]; then | |
echo "ERROR: Specify operation (backup/restore)." | |
exit 1 | |
fi | |
if [ "$1" = backup ]; then | |
rm -rf "$DEST" | |
mkdir -p "$DEST" | |
cd "$DEST" | |
OLDIFS="$IFS" | |
IFS=$': =\r' | |
while read -r -u 3 _1 package _2 installer; do | |
user=0 | |
system=0 | |
echo "Backing up '$package'..." | |
mkdir -p "$package" | |
while read -r -u 4 _3 path; do | |
case "$path" in | |
/data/*) | |
((user++)) | |
[ "$system" -gt 0 ] && break # happens? | |
$ADB pull "$path" "$package" | |
;; | |
*) | |
((system++)) | |
;; | |
esac | |
done 4< <($ADB shell pm path "$package") | |
if [ "$system" -gt 0 ] || [ "$user" -eq 0 ]; then # happens (user)? | |
rm -rf "$package" | |
else | |
echo "$package $installer" >> packages.dat | |
fi | |
done 3< <($ADB shell pm list packages -i -u) | |
IFS="$OLDIFS" | |
exit 0 | |
fi | |
if [ "$1" = restore ]; then | |
if ! [ -d "$DEST" ]; then | |
echo "ERROR: Directory '$DEST' does not exist." | |
exit 3 | |
fi | |
cd "$DEST" | |
if ! [ -f packages.dat ]; then | |
echo "ERROR: File 'packages.dat' does not exist." | |
exit 4 | |
fi | |
while read -r -u 3 package installer; do | |
opts="-i $installer" | |
echo "Restoring '$package'..." | |
[ "$installer" = null ] && opts="" | |
pushd "$package" > /dev/null | |
$ADB install-multiple -r "$opts" ./*.apk | |
popd > /dev/null | |
done 3< packages.dat | |
exit 0 | |
fi | |
echo "ERROR: Unsupported operation specified." | |
exit 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment