Skip to content

Commit d73da8a

Browse files
committed
Split APK support!
Apps shouldn't be broken after a restore anymore under most circumstances.
1 parent 2b99b04 commit d73da8a

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Open Android Backup is a tiny shell script & Flutter app that makes securely bac
1414

1515
The following data types can be automatically restored back to the device.
1616

17-
- Apps (.apk files of installed apps - app data not included - split APK support is experimental and can be found in the `split-apk-support` branch)
17+
- Apps (app data not included due to system limitations)
1818
- Internal storage (pictures, downloads, videos, Signal backups if enabled, etc)
1919
- Contacts (exported in vCard format)
2020

@@ -33,11 +33,12 @@ These things are the majority of what most people would want to keep safe, but e
3333
- Works on the 3 major operating systems, and supports *any* modern Android device.
3434
- Wireless backups that allow you to normally use your phone while it's being backed up.
3535
- Backs up data not normally accessible through ADB using a native companion app.
36-
- Tiny - the script is 5KB in size, and the companion app is around 15 megabytes.
36+
- Tiny - the whole package is about 20 MB.
3737
- Doesn't use proprietary formats - your data is safe even if you can't run the script. Simply open archives created by this script using 7-Zip.
3838
- Backups are encrypted along with their metadata.
3939
- Optionally securely erases all unencrypted temporary files created by the script.
40-
- All data is compressed using 7-Zip with maximum compression settings.
40+
- All data is compressed using 7-Zip with configurable compression settings.
41+
4142
## Installation
4243

4344
### Linux

functions/backup_func.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ function backup_func() {
5252
(
5353
apk_path=${app%=*} # apk path on device
5454
apk_path=${apk_path/package:} # strip "package:"
55-
apk_clean_name=$(echo "$app" | awk -F "=" '{print $NF}' | tr -dc '[:alnum:].' | tr '[:upper:]' '[:lower:]') # package name
56-
apk_base="$apk_clean_name-$RANDOM$RANDOM.apk" # apk filename in the backup archive
55+
apk_clean_name=$(echo "$app" | awk -F "=" '{print $NF}' | tr -dc '[:alnum:]_.') # package name
56+
#apk_base="$apk_clean_name-$RANDOM$RANDOM" # apk filename in the backup archive. Unused, removal pending?
5757
# e.g.:
5858
# app=package:/data/app/~~4wyPu0QoTM3AByZS==/org.fdroid.fdroid-iaTC9-W1lyR1FxO==/base.apk=org.fdroid.fdroid
5959
# apk_path=/data/app/~~4wyPu0QoTM3AByZS==/org.fdroid.fdroid-iaTC9-W1lyR1FxO==/base.apk
@@ -62,8 +62,14 @@ function backup_func() {
6262

6363
echo "Backing up app: $apk_clean_name ($apps_exported/$app_count)"
6464

65-
get_file "$(dirname "$apk_path")" "$(basename "$apk_path")" ./backup-tmp/Apps
66-
mv "./backup-tmp/Apps/$(basename "$apk_path")" "./backup-tmp/Apps/$apk_base" || cecho "Couldn't find app $(basename "$apk_path") after exporting from device - ignoring." 1>&2
65+
# Get all the APKs associated with the package name, including split APKs
66+
# TODO: Ensure the changes made to apk_clean_name don't break this under certain conditions
67+
for apk in $(adb shell pm path "$apk_clean_name" | sed 's/package://g' | tr -d '\r'); do
68+
# Create a directory for the app to store all the APKs
69+
mkdir -p ./backup-tmp/Apps/"$apk_clean_name"
70+
# Save the APK to its directory
71+
get_file "$(dirname "$apk")" "$(basename "$apk")" ./backup-tmp/Apps/"$apk_clean_name"
72+
done
6773
)
6874
done
6975

functions/restore_func.sh

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,45 @@ function restore_func() {
4848
cecho "Restoring applications."
4949
# We don't want a single app to break the whole script
5050
set +e
51-
# There's a 15 minute timeout for app installs just in case there is a
52-
# misbehaving app blocking the whole restore process.
53-
# Please note that this doesn't forcibly kill adb, rather it sends a simple SIGTERM signal.
51+
# Apps containing their own directories may contain split APKs, which need to be installed using adb install-multiple.
52+
# Those without directories were created by past versions of this script and need to be imported the traditional way.
53+
54+
# Handle split APKs
55+
# Find directories in the Apps directory
56+
apk_dirs=$(find ./backup-tmp/Apps -mindepth 1 -maxdepth 1 -type d)
57+
for apk_dir in $apk_dirs; do
58+
# Install all APKs in the directory
59+
# the APK files are sorted to ensure that base.apk is installed before split APKs
60+
apk_files=$(find "$apk_dir" -type f -name "*.apk" | sort | tr '\n' ' ')
61+
if [[ "$(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip')" ]]; then
62+
cecho "Windows/WSL detected"
63+
# shellcheck disable=SC2086
64+
timeout 900 ./windows-dependencies/adb/adb.exe install-multiple $apk_files
65+
else
66+
cecho "macOS/Linux detected"
67+
# shellcheck disable=SC2086
68+
timeout 900 adb install-multiple $apk_files
69+
fi
70+
done
71+
72+
# Now all that's left is ensuring backwards compatibility with old backups
73+
# Look for APK files in the Apps directory
74+
apk_files=$(find ./backup-tmp/Apps -maxdepth 1 -type f -name "*.apk" | sort)
75+
# Notify if an old backup is being restored
76+
if [ -n "$apk_files" ]; then
77+
cecho "Old backup with no split APKs detected."
78+
fi
79+
# Install all APKs
5480
if [[ "$(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip')" ]]; then
5581
cecho "Windows/WSL detected"
56-
find ./backup-tmp/Apps -type f -name "*.apk" -exec timeout 900 ./windows-dependencies/adb/adb.exe install {} \;
82+
for apk_file in $apk_files; do
83+
timeout 900 ./windows-dependencies/adb/adb.exe install "$apk_file"
84+
done
5785
else
5886
cecho "macOS/Linux detected"
59-
find ./backup-tmp/Apps -type f -name "*.apk" -exec timeout 900 adb install {} \;
87+
for apk_file in $apk_files; do
88+
timeout 900 adb install "$apk_file"
89+
done
6090
fi
6191
set -e
6292

0 commit comments

Comments
 (0)