]> gitweb.fluxo.info Git - scripts.git/commitdiff
Basic multi-user support for android-backup and adds android-restore
authorSilvio Rhatto <rhatto@riseup.net>
Sat, 29 Dec 2018 21:50:55 +0000 (19:50 -0200)
committerSilvio Rhatto <rhatto@riseup.net>
Sat, 29 Dec 2018 21:50:55 +0000 (19:50 -0200)
android-backup
android-restore [new symlink]

index 882abc2dce223334d7145bcb617a8f032e76c573..ce36f6359b45170d865f1d7c7480567c9211a034 100755 (executable)
 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 (symlink)
index 0000000..6ccc9f5
--- /dev/null
@@ -0,0 +1 @@
+android-backup
\ No newline at end of file