From 1d5630d6c4d45d8ab2954e6c3b11886a4f268317 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Fri, 13 Sep 2024 14:01:33 -0300 Subject: Renames android-{backup,restore} to android-{backup,restore}-adb; adds android-backup-mtp --- android-backup | 159 ---------------------------------------------------- android-backup-adb | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++ android-backup-mtp | 54 ++++++++++++++++++ android-restore | 1 - android-restore-adb | 1 + 5 files changed, 214 insertions(+), 160 deletions(-) delete mode 100755 android-backup create mode 100755 android-backup-adb create mode 100755 android-backup-mtp delete mode 120000 android-restore create mode 120000 android-restore-adb diff --git a/android-backup b/android-backup deleted file mode 100755 index 1985f6b..0000000 --- a/android-backup +++ /dev/null @@ -1,159 +0,0 @@ -#!/bin/bash -# -# Android backup -# - -# 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" ]; then - android_backup_backup -else - android_backup_restore -fi diff --git a/android-backup-adb b/android-backup-adb new file mode 100755 index 0000000..f601742 --- /dev/null +++ b/android-backup-adb @@ -0,0 +1,159 @@ +#!/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 diff --git a/android-backup-mtp b/android-backup-mtp new file mode 100755 index 0000000..9eda7b6 --- /dev/null +++ b/android-backup-mtp @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# +# Android backups using MTP +# +# Overall procedure +# +# PROFILE="profile-name" +# PHONE="phone-name" +# go-mtpfs ~/temp/shared/$PHONE/$PROFILE & +# time rsync -av --delete ~/temp/shared/$PHONE/$PROFILE/ ~/sync/$PHONE/$PROFILE/ +# fusermount -u ~/temp/shared/$PHONE/$PROFILE +# + +# Parameters +BASENAME="`basename $0`" +PHONE="$1" +PROFILE="$2" +SHARED="$HOME/temp/shared/$PHONE/$PROFILE" +SYNCED="$HOME/sync/$PHONE/$PROFILE" +COMMANDS="go-mtpfs rsync fusermount" + +# Syntax check +if [ -z "$PROFILE" ]; then + echo "usage: $BASENAME " + + echo "" + echo "Overall procedure:" + echo "" + echo "For each user profile:" + echo "" + echo "1. Log into the profile on the phone." + echo "2. Export the contacts somewhere like Backups/Contacts/contacts.vcf." + echo "3. Run a local backup to the internal storage (to the .SeedVaultAndroidBackup folder)." + echo "4. Run Signal and Molly backups (to the Backups folder)." + echo "4. Mount the profile folder in the computer." + echo "5. Sync the profile in the respective folder." + echo "" + echo "The last two steps are done through $BASENAME commend" + + exit 1 +fi + +# Commands check +for tool in $COMMANDS; do + if ! which $tool &> /dev/null; then + echo "error: please install $tool" + exit 1 + fi +done + +# Backup +go-mtpfs $SHARED & +time rsync -av --delete $SHARED/ $SYNCED/ +fusermount -u $SHARED diff --git a/android-restore b/android-restore deleted file mode 120000 index 6ccc9f5..0000000 --- a/android-restore +++ /dev/null @@ -1 +0,0 @@ -android-backup \ No newline at end of file diff --git a/android-restore-adb b/android-restore-adb new file mode 120000 index 0000000..41faa6c --- /dev/null +++ b/android-restore-adb @@ -0,0 +1 @@ +android-backup-adb \ No newline at end of file -- cgit v1.2.3