aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2018-12-29 19:50:55 -0200
committerSilvio Rhatto <rhatto@riseup.net>2018-12-29 19:50:55 -0200
commit310d2a7e5a7fb9a7b6e88cb9e40f80ccc82c7a7a (patch)
tree8da930ad3039c4791dbd6febcd14758a0575eb3c
parent27ea59db714352fa75f835749c06dc165ae38f8d (diff)
downloadscripts-310d2a7e5a7fb9a7b6e88cb9e40f80ccc82c7a7a.tar.gz
scripts-310d2a7e5a7fb9a7b6e88cb9e40f80ccc82c7a7a.tar.bz2
Basic multi-user support for android-backup and adds android-restore
-rwxr-xr-xandroid-backup167
l---------android-restore1
2 files changed, 125 insertions, 43 deletions
diff --git a/android-backup b/android-backup
index 882abc2..ce36f63 100755
--- a/android-backup
+++ b/android-backup
@@ -7,58 +7,139 @@
BASENAME="`basename $0`"
NAME="$1"
-# Check
-if [ -z "$NAME" ]; then
- echo "$BASENAME: missing phone name"
- exit 1
-fi
+# Backup user files
+function android_backup_files {
+ if [ ! -z "$1" ]; then
+ USER_ID="$1"
+ else
+ USER_ID="0"
+ fi
-# Additional parameters
-DATE="`date +%Y%m%d`"
-BASE="/storage/emulated/0"
-STORAGE="/var/backups/remote/$NAME.`facter domain`/"
-PREVIOUS="`sudo ls -1 $STORAGE | tac | head -n 1`"
+ BASE="/storage/emulated/$USER_ID"
-# Work folder
-cd ~/load || exit 1
-mkdir -p $DATE && cd $DATE
+ # Files: full copy
+ #adb pull $BASE files/
-# Check
-if [ -d "$STORAGE/$DATE" ]; then
- echo "backup for $DATE already exists"
- exit 1
-fi
+ # Remove multimedia cache from backup
+ #rm -rf files/Music
-# 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
+ # Files: full basic copy
+ #adb shell ls -1 $BASE | grep -v ^Music | while read file; do
+ # adb pull $BASE/$file files/
+ #done
+
+ mkdir -p files/$USER_ID
+
+ # Files: incremental basic copy
+ for file in `adb shell ls -1 $BASE | grep -v '^Music'`; do
+ adb-sync --delete --reverse $BASE/$file files/$USER_ID/
+ 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
-# Ensure we have a files folder
-mkdir -p files
+ # Work folder
+ mkdir -p $WORK/android-backup-$NAME-$DATE && cd $WORK/android-backup-$NAME-$DATE &> /dev/null || exit 1
-# 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
+ # 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
-# Configurations
-adb backup -all
+ # Ensure we have a files folder
+ mkdir -p files
-# Files: full copy
-#adb pull $BASE 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
-# Remove multimedia cache from backup
-#rm -rf files/Music
+ # User and system information
+ adb shell dumpsys user > users.dump
+ adb shell dumpsys > system.dump
-# Files: full basic copy
-#adb shell ls -1 $BASE | grep -v ^Music | while read file; do
-# adb pull $BASE/$file files/
-#done
+ # 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
-# Files: incremental basic copy
-for file in `adb shell ls -1 $BASE | grep -v '^Music'`; do
- adb-sync --delete --reverse $BASE/$file files/
-done
+ # Backup each user files
+ for USER in $USERS; do
+ android_backup_files $USER
+ done
-# Move backup to storage
-cd .. && sudo mv $DATE $STORAGE/
+ # Move backup to storage
+ cd .. &> /dev/null && sudo mv android-backup-$NAME-$DATE $STORAGE/$DATE
+}
+
+function android_backup_restore {
+ # Check for previous backups
+ if [ ! -e "$STORAGE/$PREVIOUS/files" ]; 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
+
+ # Restore each user files
+ for USER in $USERS; do
+ android_restore_files $USER
+ done
+
+ # 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" ]; then
+ android_backup_backup
+else
+ android_backup_restore
+fi
diff --git a/android-restore b/android-restore
new file mode 120000
index 0000000..6ccc9f5
--- /dev/null
+++ b/android-restore
@@ -0,0 +1 @@
+android-backup \ No newline at end of file