aboutsummaryrefslogtreecommitdiff
path: root/android-backup
blob: 1985f6b37d9f81feb076995d3a837d41d0c8eba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/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