Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ hooks.sh
.vscode
.idea
.cxx
*~
\#*#
75 changes: 15 additions & 60 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,6 @@ set -e
# TODO: load this dynamically, i.e. configure our build system to automatically update the APP_VERSION
APP_VERSION="v1.2.0"

# We use whiptail for showing dialogs.
# Whiptail is used similarly as dialog, but we can't install it on macOS using Homebrew IIRC.
# So we need to fall back to dialog if whiptail is not available.
# Check if whiptail is installed
if command -v whiptail &> /dev/null; then
# Whiptail is installed, no action needed. Do nothing.
:
else
# Check if dialog is installed
if command -v dialog &> /dev/null; then
echo "Whiptail is not installed, but dialog is. Defining whiptail as a function that calls dialog."
# Define whiptail as a function that calls dialog with the same arguments
whiptail() {
dialog "$@"
}
else
# Neither whiptail nor dialog are installed
echo "Neither whiptail nor dialog are installed, can't continue. Please refer to the README for usage instructions."
exit 1
fi
fi

# Check if other dependencies are installed: adb, tar, pv, 7z, bc, timeout
# srm is optional so we don't check for it
commands=("tar" "pv" "7z" "adb" "bc")

# Add zenity to the list of commands if we're running in WSL
if [ "$(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip')" ]; then
commands+=("zenity")
fi

# Add gtimeout to the list of commands if we're running on macOS
if [ "$(uname)" = "Darwin" ]; then
commands+=("gtimeout")
else
# For the rest of the systems, we use the standard timeout command
commands+=("timeout")
fi

for cmd in "${commands[@]}"
do
# adb is a function in WSL so we're using type instead of command -v
if ! type "$cmd" &> /dev/null
then
echo "$cmd is not available, can't continue. Please refer to the README for usage instructions."
exit 1
fi
done


SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
Expand All @@ -65,24 +15,29 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# ---

# Load all functions in ./functions
for f in "$DIR"/functions/*.sh; do source "$f"; done

# Ensure that there's enough space on the device
# TODO: Check this based on the size of the backup (or the device's storage capacity) instead of a hardcoded value of 100GB
if ! enough_free_space "."; then
cecho "Less than 100GB of free space available in the current directory. You may encounter issues if working with large backups."
fi

check_adb_connection

mode=Wireless
if [ ! -v mode ]; then
modes=( 'Wired' 'Wireless' )
select_option_from_list "Choose the connection method. Wireless is experimental and still requires a device connected for pairing." modes[@] mode
fi

#For now require connection to be pre-existing for wireless mode.
if [ "$mode" = 'Wireless' ]; then
do_connection='false'
else
do_connection='true'
fi


check_install

# ---

check_adb_connection

if [ "$mode" = 'Wireless' ]; then
# See ./functions/wireless_connection.sh
wireless_connection
Expand Down
59 changes: 59 additions & 0 deletions functions/check_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function check_install () {
# We use whiptail for showing dialogs.
# Whiptail is used similarly as dialog, but we can't install it on macOS using Homebrew IIRC.
# So we need to fall back to dialog if whiptail is not available.
# Check if whiptail is installed
if command -v whiptail &> /dev/null; then
# Whiptail is installed, no action needed. Do nothing.
:
else
# Check if dialog is installed
if command -v dialog &> /dev/null; then
echo "Whiptail is not installed, but dialog is. Defining whiptail as a function that calls dialog."
# Define whiptail as a function that calls dialog with the same arguments
whiptail() {
dialog "$@"
}
else
# Neither whiptail nor dialog are installed
echo "Neither whiptail nor dialog are installed, can't continue. Please refer to the README for usage instructions."
exit 1
fi
fi

# Check if other dependencies are installed: adb, tar, pv, 7z, bc, timeout
# srm is optional so we don't check for it
commands=("tar" "pv" "7z" "adb" "bc")


# Add zenity to the list of commands if we're running in WSL
if [ "$(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip')" ]; then
commands+=("zenity")
fi

# Add gtimeout to the list of commands if we're running on macOS
if [ "$(uname)" = "Darwin" ]; then
commands+=("gtimeout")
else
# For the rest of the systems, we use the standard timeout command
commands+=("timeout")
fi

for cmd in "${commands[@]}"
do
# adb is a function in WSL so we're using type instead of command -v
if ! type "$cmd" &> /dev/null
then
echo "$cmd is not available, can't continue. Please refer to the README for usage instructions."
exit 1
fi
done


# Ensure that there's enough space on the device
# TODO: Check this based on the size of the backup (or the device's storage capacity) instead of a hardcoded value of 100GB
if ! enough_free_space "."; then
cecho "Less than 100GB of free space available in the current directory. You may encounter issues if working with large backups."
fi

}
6 changes: 5 additions & 1 deletion functions/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ function cecho() {
}

function check_adb_connection() {
adb kill-server &> /dev/null || true
#FIXME: this breaks ability to do pure wireless. Reinstate to allow wireless initiated using USB.
if [ "$do_connection" = "true" ]
then
adb kill-server &> /dev/null || true
fi
cecho "Please enable developer options and USB debugging on your device, connect it to your computer and set it to file transfer mode. Then, press Enter to continue."
cecho "Samsung users may need to temporarily disable 'Auto Blocker' first."
wait_for_enter
Expand Down
39 changes: 21 additions & 18 deletions functions/wireless_connection.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,30 @@ function wireless_connection() {
# `device_ip` is the device ip address *without the port*, available on all devices.
# `device_ip_port` is the device ip address AND port, available on android 11+.
android_version="$(adb shell getprop ro.build.version.release | tr -d '\r' | bc)"
if (( android_version > 10 )); then
cecho "Running on Android 11 or higher - automatic wireless connections are not supported."
cecho "Please open the settings app on your device, and search for 'Wireless debugging'. Enable the option, press 'Pair device with pairing code', and enter the IP address and port of your device below:"
# TODO: use get_text_input instead of read
#get_text_input "Device IP & Port:" device_ip_port "$device_ip"
read -p "Pairing IP address & Port: " device_ip_port
cecho "Pairing device..."
adb pair "$device_ip_port"
cecho "You now have to enter the IP address and port that's shown in the settings app (not the one shown in the pairing screens)."
read -p "Device IP address & Port: " device_ip_port
adb connect "$device_ip_port" # this is necessary yet not mentioned in the official documentation
else # Running on Android 10 or lower
cecho "Establishing connection..."
adb tcpip 5353
sleep 5
adb connect "$device_ip:5353"
fi


if [ "$do_connection" = "true" ]; then
if (( android_version > 10 )); then
cecho "Running on Android 11 or higher - automatic wireless connections are not supported."
cecho "Please open the settings app on your device, and search for 'Wireless debugging'. Enable the option, press 'Pair device with pairing code', and enter the IP address and port of your device below:"
# TODO: use get_text_input instead of read
#get_text_input "Device IP & Port:" device_ip_port "$device_ip"
read -p "Pairing IP address & Port: " device_ip_port
cecho "Pairing device..."
adb pair "$device_ip_port"
cecho "You now have to enter the IP address and port that's shown in the settings app (not the one shown in the pairing screens)."
read -p "Device IP address & Port: " device_ip_port
adb connect "$device_ip_port" # this is necessary yet not mentioned in the official documentation
else # Running on Android 10 or lower
cecho "Establishing connection..."
adb tcpip 5353
sleep 5
adb connect "$device_ip:5353"
fi
fi
cecho "Please unplug your device from the computer, and press Enter to continue."
wait_for_enter
adb devices
cecho "If you can see an IP address in the list above, and it says 'device' next to it, then you have successfully established a connection to the phone."
cecho "If it says 'unauthorized' or similar, then you need to unlock your device and approve the connection before continuing."
}
}