-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore_sdcard.sh
executable file
·110 lines (99 loc) · 2.78 KB
/
restore_sdcard.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
set -e -u
set -o pipefail
# restore android sdcard, both personal and work profiles
function usage ()
{
cat << EOF
Usage: ${0##*/} [OPTION]...
Options: --help, -h: show this help dialog
--dry-run: do not do anything - just show what would be done
--user, -u: restore this user
EOF
}
function android_users ()
{
adb shell pm list users|awk 'match($0, /^\s*UserInfo\{([0-9]+):.*:[0-9]+}/, arr) {print arr[1]}'
}
function remount_readable ()
{
dry_run="${1}"; shift
# source: https://android.stackexchange.com/questions/221122/how-to-access-storage-emulated-10-multi-users-env-in-adb-shell-on-android-9
cmd="umount /mnt/runtime/*/emulated; /system/bin/sdcard -u 1023 -g 1023 -m -w -G /data/media emulated"
remount_script=$(mktemp)
cat > "${remount_script}" << EOF
${cmd}
EOF
chmod +x "${remount_script}"
rundroid "${remount_script}"
rm "${remount_script}"
}
function restore_sdcard_for_user ()
{
user="${1}"; shift
cd "${user}"
# shellcheck disable=SC2086
adb-sync -t "/storage/emulated/${user}/" . --delete ${dry_run} --exclude Android,osmand
}
function main ()
{
local dry_run=""
# shellcheck disable=SC2154
local basedir="${android}/storage/emulated"
local user=""
while getopts "u:h-:" opt; do
case ${opt} in
h) # help message
usage
exit 0
;;
u)
user="${!OPTIND}"
OPTIND=$((OPTIND + 1))
;;
-)
case "${OPTARG}" in
basedir)
basedir="${!OPTIND}"
OPTIND=$((OPTIND+1))
;;
help)
usage
exit 0
;;
dry-run)
dry_run="--dry-run"
;;
user)
user="${!OPTIND}"
OPTIND=$((OPTIND + 1))
;;
*)
printf 'Unknown option, exiting now\n' >&2
exit 1
;;
esac
;;
?)
printf 'Unknown option, exiting now\n' >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
[[ "${1:-}" == '--' ]] && shift
basedir="$(realpath "${basedir}")"
remount_readable "$dry_run"
if [ -n "${user}" ]; then
cd "${basedir}"
restore_sdcard_for_user "${user}"
else
for u in $(android_users); do
cd "${basedir}"
restore_sdcard_for_user "${u}"
done
fi
}
if [ "${BASH_SOURCE[0]}" == "$0" ]; then
main "$@"
fi