Last active
April 25, 2022 20:38
-
-
Save SebiderSushi/4b541ec5eddfe04201ad08f450fb780a to your computer and use it in GitHub Desktop.
Backup & restore android app data. Intended for rooted/recovery terminal or adb shell.
This file contains 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/sh | |
# Create a backup from one app datadir into a tape archive | |
# APP_DATADIR is the corresponding app data directory for the user you want to back up | |
# the primary is located in /data/data, for additional users see /data/user/ | |
function abr_backup { | |
[ $# -ne 2 ] && { echo "usage: $FUNCNAME APP_DATADIR BACKUP_FILEPATH"; return; } | |
local datadir="$1"; local archive="$2" | |
tar cf "$archive" -C "$datadir" --exclude="lib" . && echo "${archive} backup ok!" | |
} | |
# Restore app data from a tape archive and set proper file permissions & SELinux context | |
function abr_restore { | |
[ $# -ne 2 ] && { echo "usage: $FUNCNAME APP_DATADIR BACKUP_FILEPATH"; return; } | |
local datadir="$1" | |
local archive="$2" | |
local own="$(stat -c '%u' "$datadir")" && | |
local grp="$(stat -c '%g' "$datadir")" && | |
local ctx="$(ls -Zd "$datadir")" && | |
ctx="${ctx%% *}" && | |
while read -r item; do | |
[ "$item" == "lib" ] && continue | |
rm -fr "${datadir}/${item}" | |
done <<<"$(cd "$datadir" && ls -A)" && | |
tar xf "$archive" -C "$datadir" --exclude="lib" && | |
while read -r item; do | |
[ "$item" == "lib" ] && continue | |
chown -hR "$own":"$grp" "${datadir}/${item}" || break | |
chcon -hR "$ctx" "${datadir}/${item}" || break | |
done <<<"$(cd "$datadir" && ls -A)" && | |
chown "$own":"$grp" "${datadir}" && | |
chcon "$ctx" "${datadir}" && | |
echo "${archive} restore ok!" | |
} | |
# Make sure that the application is stopped prior to backing up | |
function abr_backupruntime { | |
[ $# -ne 3 ] && { echo "usage: $FUNCNAME USER_ID PACKAGE_NAME BACKUP_FILEPATH"; return; } | |
local user="$1"; local pkg="$2"; local archive="$3" | |
am force-stop --user "$user" "$pkg" && | |
abr_backup "/data/user/${user}/${pkg}" "$archive" | |
} | |
# Make sure that the application is stopped prior to restoring | |
function abr_restoreruntime { | |
[ $# -ne 3 ] && { echo "usage: $FUNCNAME USER_ID PACKAGE_NAME BACKUP_FILEPATH"; return; } | |
local user="$1"; local pkg="$2"; local archive="$3" | |
am force-stop --user "$user" "$pkg" && | |
abr_restore "/data/user/${user}/${pkg}" "$archive" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment