Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to exclude arbitrary directories from backup #86

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
*.apk
hooks.sh
.vscode
.idea
.idea
backup-tmp/
open-android-backup-temp/
11 changes: 11 additions & 0 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,18 @@ elif [ "$use_hooks" = "yes" ]; then
exit 1
fi

# Allow the user to exclude specific files
# Leaving this block after the hooks integration, as we do not know if the user wants their hooks to operate on only the chosen files or not.
if [ ! -v exclusions ] && [ "$export_method" = "tar" ]; then
cecho "You may optionally choose to exclude certain directories and/or files from being backed up."
cecho "Press Enter to continue."
wait_for_enter

excluded_files=( 'no' 'yes' )
select_option_from_list "Do you wish to add any exclusions?" excluded_files[@] exclusions
fi

clear

if command -v srm &> /dev/null
then
Expand Down
38 changes: 32 additions & 6 deletions functions/backup_func.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,42 @@ function backup_func() {
# Get call logs
mkdir ./backup-tmp/CallLogs
mv ./backup-tmp/open-android-backup-temp/Call_Logs.csv ./backup-tmp/CallLogs
# Cleanup
# Export internal storage
if [ "$exclusions" = "yes" ]; then
# Continue with user interaction
cecho "Please list files or directories you wish to exclude from the backup."
cecho "Entries should be space-delimited, and relative to '/storage/emulated/0/'. Directories do NOT need a trailing slash."
cecho "---"
cecho "For example, the input: 'Documents Pictures/my_secret_pictures somefile.txt'"
cecho "will exclude the entire folder '/storage/emulated/0/Documents/', the subfolder '/storage/emulated/0/Pictures/my_secret_pictures', and the file '/storage/emulated/0/somefile.txt'."
cecho "---"
cecho "Top-level device folders:"
# TODO: better way to show this? The adb shell is pretty much limited to toybox/coreutils.
adb shell ls -A /storage/emulated/0/ | grep -v open-android-backup-temp
cecho "---"
# Receive user input
# TODO: this could be much less flaky by utilizing 'toybox vi' to edit the exclusions list. Ignoring for now.
adb shell printf "Exclusions:\ " && IFS= read -r exclusions_response
# This command MUST be contained in double quotes, or else adb shell chokes on the input and our 'tar -x' fails.
adb shell "echo "$exclusions_response" | sed 's/ /\n/g' > /storage/emulated/0/open-android-backup-temp/exclusions.txt"
cecho "Exporting internal storage - this will take a while."
mkdir ./backup-tmp/Storage
get_file_exclude /storage/emulated/0 . ./backup-tmp/Storage
else
# Fall back to old behavior
cecho "Failed to apply exclusions. Falling back to standard backup..."
cecho "Exporting internal storage - this will take a while."
mkdir ./backup-tmp/Storage
get_file /storage/emulated/0 . ./backup-tmp/Storage
fi

# Cleanup
cecho "Removing temporary files created by the companion app."
adb shell rm -rf /storage/emulated/0/open-android-backup-temp
rm -rf ./backup-tmp/open-android-backup-temp

# Export internal storage
cecho "Exporting internal storage - this will take a while."
mkdir ./backup-tmp/Storage
get_file /storage/emulated/0 . ./backup-tmp/Storage



# Run the third-party backup hook, if enabled.
if [ "$use_hooks" = "yes" ] && [ "$(type -t backup_hook)" == "function" ]; then
cecho "Running backup hooks in 5 seconds."
Expand Down
12 changes: 11 additions & 1 deletion functions/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ function select_option_from_list() {
whiptail_options+=("$i" "${options[$i]}")
done

# Use whiptail to display a menu and get the selected index
local selected_index=$(whiptail --title "Select an option" --menu "$prompt" $LINES $COLUMNS $(( $LINES - 8 )) "${whiptail_options[@]}" 3>&1 1>&2 2>&3)

# Check if whiptail exited with a non-zero status or if no option was selected
Expand Down Expand Up @@ -189,6 +188,17 @@ function get_file() {
fi
}

# There's probably a much better way to do this, but it's easier to build on existing functionality and style
# Usage: get_file_exclude <directory> <file> <destination>
function get_file_exclude() {
if [ "$export_method" = 'tar' ]; then
(adb exec-out "tar -c -C $1 $2 -X $1/open-android-backup-temp/exclusions.txt 2> /dev/null" | pv -p --timer --rate --bytes | tar -C "$3" -xf -) || cecho "Errors occurred while backing up $2 - this file (or multiple files) might've been ignored." 1>&2
else # we're falling back to adb pull if the variable is empty/unset
# leave this intact in case of tar failure
adb pull "$1"/"$2" "$3" || cecho "Errors occurred while backing up $2 - this file (or multiple files) might've been ignored." 1>&2
fi
}

# Usage: directory_ok <directory>
# Returns 0 (true) or 1 (false)
function directory_ok() {
Expand Down