14a64bded06a0299785c295a975e2818308eb53e2Joe Onorato#!/bin/bash
24a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
31f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# Copyright (C) 2009 The Android Open Source Project
41f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens#
51f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# Licensed under the Apache License, Version 2.0 (the "License");
61f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# you may not use this file except in compliance with the License.
71f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# You may obtain a copy of the License at
81f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens#
91f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens#      http://www.apache.org/licenses/LICENSE-2.0
101f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens#
111f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# Unless required by applicable law or agreed to in writing, software
121f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# distributed under the License is distributed on an "AS IS" BASIS,
131f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
141f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# See the License for the specific language governing permissions and
151f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# limitations under the License.
161f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens
170cfeb25707c87af285cc993967be486d9c95a176Omari Stephens# uncomment for debugging
180cfeb25707c87af285cc993967be486d9c95a176Omari Stephens#export DRY_RUN="echo"
190cfeb25707c87af285cc993967be486d9c95a176Omari Stephenssource test_backup_common.sh
201f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens
21259447bc6490f4ec7e04eb5d8bfa1b041650ed53Omari Stephens[ -z "$BUGREPORT_DIR" ] && BUGREPORT_DIR="$HOME/backup/bugreports"
221f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens
234a64bded06a0299785c295a975e2818308eb53e2Joe Onoratofunction check_file
244a64bded06a0299785c295a975e2818308eb53e2Joe Onorato{
250cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    data=$(a shell cat /data/data/com.android.backuptest/$1)
264a64bded06a0299785c295a975e2818308eb53e2Joe Onorato    if [ "$data" = "$2" ] ; then
274a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        echo "$1 has correct value [$2]"
280cfeb25707c87af285cc993967be486d9c95a176Omari Stephens        return 0
294a64bded06a0299785c295a975e2818308eb53e2Joe Onorato    else
304a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        echo $1 is INCORRECT
314a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        echo "   value:    [$data]"
324a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        echo "   expected: [$2]"
330cfeb25707c87af285cc993967be486d9c95a176Omari Stephens        return 1
340cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    fi
350cfeb25707c87af285cc993967be486d9c95a176Omari Stephens}
360cfeb25707c87af285cc993967be486d9c95a176Omari Stephens
370cfeb25707c87af285cc993967be486d9c95a176Omari Stephensfunction check_exists
380cfeb25707c87af285cc993967be486d9c95a176Omari Stephens{
390cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    # return 0 if file exists, 1 otherwise
400cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    data=$(a shell "ls $@ 2> /dev/null >/dev/null && echo -n exists")
410cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    if [ "$data" = "exists" ]; then
420cfeb25707c87af285cc993967be486d9c95a176Omari Stephens        return 0
430cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    else
440cfeb25707c87af285cc993967be486d9c95a176Omari Stephens        return 1
454a64bded06a0299785c295a975e2818308eb53e2Joe Onorato    fi
464a64bded06a0299785c295a975e2818308eb53e2Joe Onorato}
474a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
48aa6e73a6c850694550b59a0911727dcb23c4837dOmari Stephens# Make sure adb is root so we can poke at com.android.backuptest's data
490cfeb25707c87af285cc993967be486d9c95a176Omari Stephensadb_root
501f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens
514a64bded06a0299785c295a975e2818308eb53e2Joe Onorato# delete the old data
524a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho --- Previous files
530cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell "ls -l /data/data/com.android.backuptest/files"
540cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell "rm /data/data/com.android.backuptest/files/*"
55dc355a90a3d9d34f66316928a53f61ac35ab4781Joe Onoratoecho --- Previous shared_prefs
560cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell "ls -l /data/data/com.android.backuptest/shared_prefs"
570cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell "rm /data/data/com.android.backuptest/shared_prefs/*"
58dc355a90a3d9d34f66316928a53f61ac35ab4781Joe Onoratoecho --- Erased files and shared_prefs
590cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell "ls -l /data/data/com.android.backuptest/files"
600cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell "ls -l /data/data/com.android.backuptest/shared_prefs"
614a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho ---
624a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
634a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho
644a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho
654a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
661f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# FIXME: there's probably a smarter way to do this
671f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# FIXME: if we can get the android ID, that's probably the safest thing to do
681f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens# pick the most recent set and restore from it
690cfeb25707c87af285cc993967be486d9c95a176Omari Stephensrestore_set=$(a shell bmgr list sets | head -n1 | awk '{print $1}')
701f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens
714a64bded06a0299785c295a975e2818308eb53e2Joe Onorato# run the restore
720cfeb25707c87af285cc993967be486d9c95a176Omari Stephensecho "Restoring from set [$restore_set]"
730cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell bmgr restore "$restore_set"
744a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
754a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho
764a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho
774a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
784a64bded06a0299785c295a975e2818308eb53e2Joe Onorato# check the results
790cfeb25707c87af285cc993967be486d9c95a176Omari Stephensexport need_bug=0
800cfeb25707c87af285cc993967be486d9c95a176Omari Stephens
810cfeb25707c87af285cc993967be486d9c95a176Omari Stephens# make sure files have the expected contents
820cfeb25707c87af285cc993967be486d9c95a176Omari Stephenscheck_file files/file.txt "first file" || need_bug=1
830cfeb25707c87af285cc993967be486d9c95a176Omari Stephenscheck_file files/another_file.txt "asdf" || need_bug=1
840cfeb25707c87af285cc993967be486d9c95a176Omari Stephens#check_file files/3.txt "3" || need_bug=1
850cfeb25707c87af285cc993967be486d9c95a176Omari Stephenscheck_file files/empty.txt "" || need_bug=1
860cfeb25707c87af285cc993967be486d9c95a176Omari Stephenscheck_file shared_prefs/raw.xml '<map><int name="pref" value="1" /></map>' || need_bug=1
870cfeb25707c87af285cc993967be486d9c95a176Omari Stephens
880cfeb25707c87af285cc993967be486d9c95a176Omari Stephens# make sure that missing files weren't somehow created
890cfeb25707c87af285cc993967be486d9c95a176Omari Stephenscheck_exists files/file_doesnt_exist.txt && need_bug=1
900cfeb25707c87af285cc993967be486d9c95a176Omari Stephenscheck_exists files/no_files_here.txt && need_bug=1
910cfeb25707c87af285cc993967be486d9c95a176Omari Stephens
920cfeb25707c87af285cc993967be486d9c95a176Omari Stephensif [ \( "$need_bug" -ne 0 \) -a -d "$BUGREPORT_DIR" ]; then
930cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    dev_id=$(a get-serialno)
940cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    filename="${dev_id}_`date +%s`"
950cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    echo "Grabbing bugreport; filename is $filename"
960cfeb25707c87af285cc993967be486d9c95a176Omari Stephens    a bugreport > "$BUGREPORT_DIR/$filename.txt"
970cfeb25707c87af285cc993967be486d9c95a176Omari Stephensfi
984a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
994a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho
1004a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho --- Restored files
1010cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell "ls -l /data/data/com.android.backuptest/files"
102dc355a90a3d9d34f66316928a53f61ac35ab4781Joe Onoratoecho --- Restored shared_prefs
1030cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell "ls -l /data/data/com.android.backuptest/shared_prefs"
1044a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho ---
1054a64bded06a0299785c295a975e2818308eb53e2Joe Onoratoecho
1061f808c6f6ed893bb50c94cf5be676712e4d68b9bOmari Stephens
107864ed1a9d7f7a18db7415322728f6208a0eaf9d5Omari Stephensecho "Last 3 timestamps in 3.txt:"
1080cfeb25707c87af285cc993967be486d9c95a176Omari Stephensa shell cat /data/data/com.android.backuptest/files/3.txt | tail -n 3
109864ed1a9d7f7a18db7415322728f6208a0eaf9d5Omari Stephens
110259447bc6490f4ec7e04eb5d8bfa1b041650ed53Omari Stephensexit $need_bug
111259447bc6490f4ec7e04eb5d8bfa1b041650ed53Omari Stephens
112