#!/bin/bash # # Android backups using ADB # # Basic parameters BASENAME="`basename $0`" NAME="$1" # Backup user files function android_backup_files { if [ ! -z "$1" ]; then USER_ID="$1" else USER_ID="0" fi BASE="/storage" EMULATED="$BASE/emulated/$USER_ID" # Files: full copy #adb pull $EMULATED files/ # Remove multimedia cache from backup #rm -rf files/Music # Files: full basic copy #adb shell ls -1 $EMULATED | grep -v ^Music | while read file; do # adb pull $EMULATED/$file files/ #done mkdir -p files/$USER_ID # Files: incremental basic copy for file in `adb shell ls -1 $EMULATED | grep -v '^Music'`; do adb-sync --delete --reverse $EMULATED/$file files/$USER_ID/ done # SD cards for file in `adb shell ls -1 $BASE | grep -v "^self" | grep -v "^emulated"`; do adb-sync --delete --reverse $BASE/$file files/ done } # Restore user files function android_restore_files { if [ ! -z "$1" ]; then USER_ID="$1" else USER_ID="0" fi BASE="/storage/emulated/$USER_ID" # Files: complete copy #for file in `ls files`; do # adb push files/$file $base/$file #done # Files: incremental copy for file in `ls $WORK/android-backup-$NAME-$DATE/files/$USER_ID`; do adb-sync --delete $WORK/android-backup-$NAME-$DATE/files/$USER_ID/$file/ $BASE/$file/ done } function android_backup_backup { # Check previous backup if [ -d "$STORAGE/$DATE" ]; then echo "backup for $DATE already exists" exit 1 fi # Work folder mkdir -p $WORK/android-backup-$NAME-$DATE && cd $WORK/android-backup-$NAME-$DATE &> /dev/null || exit 1 # If you have a previous backup you might want to use it with hardlinks if [ -e "$STORAGE/$PREVIOUS/files" ]; then sudo cp -alf $STORAGE/$PREVIOUS/files files fi # Ensure we have a files folder mkdir -p files # Contacts. Export also to a .vcf directly from the contact app adb-export.sh -e content://com.android.contacts/contacts adb shell content query --uri content://com.android.contacts/contacts > contacts.rows # User and system information adb shell dumpsys user > users.dump adb shell dumpsys > system.dump # Configurations # Right now this is possible only for the main user # https://stackoverflow.com/questions/50978678/adb-backup-restore-multiple-users-apps # https://android.stackexchange.com/questions/43043/non-root-backup-with-multiple-users-non-owner-or-secondary-users #adb backup -apk -shared -all adb backup -all # Backup each user files for USER in $USERS; do android_backup_files $USER done # Move backup to storage cd .. &> /dev/null && sudo mv android-backup-$NAME-$DATE $STORAGE/$DATE } function android_backup_restore { # Check for previous backups if [ -z "$PREVIOUS" ]; then echo "$BASENAME: no previous backups for device $NAME" exit 1 fi # Copy files to workfolder mkdir -p $WORK && sudo cp -alf $STORAGE/$PREVIOUS $WORK/android-backup-$NAME-$DATE && chown -R `whoami`. android-backup-$NAME-$DATE # Check if copy was successful if [ ! -e "$WORK/android-backup-$NAME-$DATE" ]; then echo "$BASENAME: could not copy from $STORAGE/$PREVIOUS" exit 1 fi # Restore each user files if [ -e "$WORK/android-backup-$NAME-$DATE" ]; then for USER in $USERS; do android_restore_files $USER done fi # Configurations adb restore android-backup-$NAME-$DATE/backup.ab # Cleanup rm -rf android-backup-$NAME-$DATE } # Syntax check if [ -z "$NAME" ]; then echo "$BASENAME: missing phone name" exit 1 fi # Additional parameters WORK="/var/data/load" DATE="`date +%Y%m%d`" STORAGE="/var/backups/remote/$NAME.`facter domain`/" USERS="`adb shell pm list users | grep '{' | cut -d '{' -f 2 | cut -d ':' -f 1 | xargs`" # Dest folder and previous backups sudo mkdir -p $STORAGE PREVIOUS="`sudo ls -1 $STORAGE | tac | head -n 1`" # Dispatch if [ "$BASENAME" == "android-backup-adb" ]; then android_backup_backup else android_backup_restore fi