Last active
February 22, 2022 05:42
-
-
Save gema-arta/83781972e5f034bf0e7f9ba1c8f41db8 to your computer and use it in GitHub Desktop.
A simple script to backup my Android phone to my Macbook.
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 | |
# | |
# backupAndroidDevice.sh | |
# | |
# A simple script to backup my Android phone to my Macbook. | |
# | |
# Syntax: backupAndroidDevice.sh output_folder | |
# output folder must exist | |
# | |
# Requirements/assumptions: | |
# * Device is running in TWRP recovery | |
# * Device has an external SD card with sufficient space to back up /data/media/0 (internal SD card) | |
# * Device can respond to 'adb shell' commands | |
# | |
# Actions: | |
# Creates a subfolder with the current data_time as the name. | |
# | |
# sanitize command line | |
if [ $# -lt 1 ] | |
then | |
echo "Usage: $0 output_folder" | |
exit 1 | |
fi | |
# input folder exists | |
if [ ! -d "$1" ]; then | |
echo "$1 does not exist" | |
exit 1 | |
fi | |
# Create name of output folder | |
dt=$(date '+%Y%m%d_%H%M%S'); | |
# Check existence of output folder and create new folder | |
cd $1 | |
if [ $? -ne 0 ]; then | |
echo "Could not find $1" | |
exit 1 | |
fi | |
mkdir $dt | |
if [ $? -ne 0 ]; then | |
echo "Could not create $dt" | |
exit 1 | |
fi | |
cd $dt | |
if [ $? -ne 0 ]; then | |
echo "Could not find $dt" | |
exit 1 | |
fi | |
# Get ready | |
echo "Put your device into TWRP recovery and swipe to unlock." | |
echo "Ensure your device has an external SD card mounted, formatted as portable storage, with room to store archive." | |
read -n1 -r -p "Press space to continue..." key | |
ext="/external_sd/internal_backup" | |
# Test SD card backup dir | |
if [ ! -d "$ext" ]; then | |
adb shell mkdir $ext | |
if [ $? -ne 0 ]; then | |
echo "Could not create $ext" | |
exit 1 | |
fi | |
fi | |
# Remove dense cache folders. | |
# NOTE: This will be different for everyone | |
echo "Deleting caches..." | |
adb shell rm -rf /data/media/0/Android/data/com.google.android.youtube/cache | |
adb shell rm -rf /data/media/0/Android/data/com.android.providers.media/albumthumbs/ | |
adb shell rm -rf /data/media/0/Android/data/com.google.android.apps.maps/cache/ | |
adb shell rm -rf /data/media/0/Android/data/com.dropbox.android/cache/ | |
adb shell rm -rf /data/media/0/Android/data/com.dropbox.android/asset_cache/ | |
adb shell rm -rf /data/media/0/Android/data/pl.solidexplorer2/cache/ | |
# Backup partitions. | |
echo "Backing up /boot..." | |
adb backup -f boot.ab --twrp --compress boot | |
if [ $? -ne 0 ]; then | |
echo "Could not backup /boot" | |
exit 1 | |
fi | |
# I'm not 100% certain these adb reboots are necessary, but without them, I get | |
# errors about multiple threads running. It may be alright to just wait 20 secs | |
# rather than wait for user to press a key | |
adb reboot recovery | |
read -n1 -r -p "Press space when recovery is ready..." key | |
echo "Backing up /system..." | |
adb backup -f system.ab --twrp --compress system | |
if [ $? -ne 0 ]; then | |
echo "Could not backup /system" | |
exit 1 | |
fi | |
adb reboot recovery | |
read -n1 -r -p "Press space when recovery is ready..." key | |
echo "Backing up /data..." | |
adb backup -f data.ab --twrp --compress data | |
if [ $? -ne 0 ]; then | |
echo "Could not backup /data" | |
exit 1 | |
fi | |
echo "Backup up internal storage..." | |
adb shell tar czvpf /external_sd/internal_backup/media.tar.gz /data/media/0/ | |
if [ $? -ne 0 ]; then | |
echo "Could not backup /data/media/0" | |
exit 1 | |
fi | |
echo "Copy internal storage to computer..." | |
adb pull /external_sd/internal_backup/media.tar.gz | |
if [ $? -eq 0 ]; then | |
adb shell rm /external_sd/internal_backup/media.tar.gz | |
else | |
echo "Could not pull /external_sd/internal_backup/media.tar.gz from device. You can manually retrieve this file." | |
exit 1 | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment