Last active
January 6, 2025 11:32
-
-
Save vijinho/be6019a2b53a41ee37a8021c311b9d5a to your computer and use it in GitHub Desktop.
Backup all Android apps and data using adb, excluding system apps
This file contains hidden or 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/bash | |
# Function to handle errors and log them | |
handle_error() { | |
echo "Error: $1" >> backup.log | |
exit 1 | |
} | |
# Check if adb is available | |
command -v adb >/dev/null 2>&1 || { | |
echo >&2 "adb command not found. Please ensure ADB is installed and added to your PATH."; | |
exit 1; | |
} | |
# Ensure a device is connected | |
adb devices | grep -q 'device$' || { | |
echo >&2 "No device detected. Please connect an Android device." | |
handle_error "No device detected" | |
} | |
# Create backup file name | |
BACKUPFILE=$(date "+%Y%m%d.%H%M%S").adb | |
# Start the backup process and log output | |
echo "Starting backup: $BACKUPFILE" >> backup.log | |
if adb backup -apk -obb -shared -all -nosystem "$BACKUPFILE"; then | |
echo "Backup successful: $BACKUPFILE" >> backup.log | |
else | |
handle_error "Backup failed" | |
fi | |
# Optional: Clean up any temporary files or resources if needed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is a Bash script designed to perform a backup of an Android device using the ADB (Android Debug Bridge) tool. Here's a breakdown of what each part does:
Error Handling Function: The
handle_error
function is defined to log any errors encountered during the script execution and then exit with a status code of 1.Check for ADB Command: The script checks if the
adb
command is available in the system's PATH. If not, it prints an error message asking the user to install and add ADB to their PATH, then exits.Device Detection: The script ensures that an Android device is connected by using
adb devices
. It greps for 'device$' which indicates a connected device. If no device is found, it prints an error message and calls thehandle_error
function to log the error and exit.Create Backup File Name: A backup file name is generated with a timestamp using
date "+%Y%m%d.%H%M%S".adb
. This ensures that each backup has a unique name based on when it was created.Backup Process: The script attempts to start the backup process by calling
adb backup -apk -obb -shared -all -nosystem "$BACKUPFILE"
. Here,-apk
backs up application APKs,-obb
backs up any OBB files (additional content for applications),-shared
backs up shared storage,-all
backs up all applications and data, and-nosystem
excludes system apps. If the backup is successful, it logs a success message; otherwise, it calls thehandle_error
function to log the error.Optional Cleanup: The script ends with an optional section where you could clean up any temporary files or resources if needed, though this part is currently commented out.
This script provides a simple way to automate the backup process for Android devices using ADB and includes basic error handling and logging for better maintainability and troubleshooting.