Home | History | Annotate | Download | only in backup
      1 #!/bin/bash
      2 
      3 # Copyright (C) 2009 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 # uncomment for debugging
     18 #export DRY_RUN="echo"
     19 source test_backup_common.sh
     20 
     21 [ -z "$BUGREPORT_DIR" ] && BUGREPORT_DIR="$HOME/backup/bugreports"
     22 
     23 function check_file
     24 {
     25     data=$(a shell cat /data/data/com.android.backuptest/$1)
     26     if [ "$data" = "$2" ] ; then
     27         echo "$1 has correct value [$2]"
     28         return 0
     29     else
     30         echo $1 is INCORRECT
     31         echo "   value:    [$data]"
     32         echo "   expected: [$2]"
     33         return 1
     34     fi
     35 }
     36 
     37 function check_exists
     38 {
     39     # return 0 if file exists, 1 otherwise
     40     data=$(a shell "ls $@ 2> /dev/null >/dev/null && echo -n exists")
     41     if [ "$data" = "exists" ]; then
     42         return 0
     43     else
     44         return 1
     45     fi
     46 }
     47 
     48 # Make sure adb is root so we can poke at com.android.backuptest's data
     49 adb_root
     50 
     51 # delete the old data
     52 echo --- Previous files
     53 a shell "ls -l /data/data/com.android.backuptest/files"
     54 a shell "rm /data/data/com.android.backuptest/files/*"
     55 echo --- Previous shared_prefs
     56 a shell "ls -l /data/data/com.android.backuptest/shared_prefs"
     57 a shell "rm /data/data/com.android.backuptest/shared_prefs/*"
     58 echo --- Erased files and shared_prefs
     59 a shell "ls -l /data/data/com.android.backuptest/files"
     60 a shell "ls -l /data/data/com.android.backuptest/shared_prefs"
     61 echo ---
     62 
     63 echo
     64 echo
     65 
     66 # FIXME: there's probably a smarter way to do this
     67 # FIXME: if we can get the android ID, that's probably the safest thing to do
     68 # pick the most recent set and restore from it
     69 restore_set=$(a shell bmgr list sets | head -n1 | awk '{print $1}')
     70 
     71 # run the restore
     72 echo "Restoring from set [$restore_set]"
     73 a shell bmgr restore "$restore_set"
     74 
     75 echo
     76 echo
     77 
     78 # check the results
     79 export need_bug=0
     80 
     81 # make sure files have the expected contents
     82 check_file files/file.txt "first file" || need_bug=1
     83 check_file files/another_file.txt "asdf" || need_bug=1
     84 #check_file files/3.txt "3" || need_bug=1
     85 check_file files/empty.txt "" || need_bug=1
     86 check_file shared_prefs/raw.xml '<map><int name="pref" value="1" /></map>' || need_bug=1
     87 
     88 # make sure that missing files weren't somehow created
     89 check_exists files/file_doesnt_exist.txt && need_bug=1
     90 check_exists files/no_files_here.txt && need_bug=1
     91 
     92 if [ \( "$need_bug" -ne 0 \) -a -d "$BUGREPORT_DIR" ]; then
     93     dev_id=$(a get-serialno)
     94     filename="${dev_id}_`date +%s`"
     95     echo "Grabbing bugreport; filename is $filename"
     96     a bugreport > "$BUGREPORT_DIR/$filename.txt"
     97 fi
     98 
     99 echo
    100 echo --- Restored files
    101 a shell "ls -l /data/data/com.android.backuptest/files"
    102 echo --- Restored shared_prefs
    103 a shell "ls -l /data/data/com.android.backuptest/shared_prefs"
    104 echo ---
    105 echo
    106 
    107 echo "Last 3 timestamps in 3.txt:"
    108 a shell cat /data/data/com.android.backuptest/files/3.txt | tail -n 3
    109 
    110 exit $need_bug
    111 
    112