Skip to content

Instantly share code, notes, and snippets.

@vijinho
Last active January 6, 2025 11:32
Show Gist options
  • Save vijinho/be6019a2b53a41ee37a8021c311b9d5a to your computer and use it in GitHub Desktop.
Save vijinho/be6019a2b53a41ee37a8021c311b9d5a to your computer and use it in GitHub Desktop.
Backup all Android apps and data using adb, excluding system apps
#!/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
@vijinho
Copy link
Author

vijinho commented Jan 6, 2025

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:

  1. 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.

  2. 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.

  3. 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 the handle_error function to log the error and exit.

  4. 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.

  5. 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 the handle_error function to log the error.

  6. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment