引自:http://xiongseason.blog.163.com/blog/static/176995025201011249912835/
BackupManager的使用与解析
看了两天的备份管理今天终于有点眉目了,先来看看如何来操作才能够backup
android default backup function is disabled
before you do the actions as follow. you should confirm
Settings.Secure.BACKUP_ENABLED and Settings.Secure.BACKUP_PROVISIONED was set 1 in database
bash-4.0#
bash-4.0# bmgr enable true --enable backup
Backup Manager now enabled
bash-4.0# bmgr transport android/com.android.internal.backup.LocalTransport <!--指定一个transport来做backup的动作 具体的功能后面会详讲 -->
Selected transport android/com.android.internal.backup.LocalTransport (formerly android/com.android.internal.backup.LocalTransport)
bash-4.0# bmgr backup com.android.browser <!--制定要备份的packagename-->
bash-4.0# bmgr run <!--开始备份-->
bash-4.0#
备份已经做完了 现在来看看结果
<!-- -->
bash-4.0# cd /data/backup/
bash-4.0# ls
com.android.internal.backup.LocalTransport
processed <!--存储已经备份过的packagename -->
pending <!--存储还需要备份的packagename-->
bash-4.0# cat processed
$com.android.providers.userdictionarycom.android.inputmethod.latinandroidcom.android.providers.settingscom.android.browsercom.android.settingsbash-4.0#
bash-4.0#
bash-4.0# cd pending/
bash-4.0# ls
journal46314.tmp
bash-4.0# cat journal46314.tmp
com.android.providers.settingsbash-4.0
bash-4.0#
bash-4.0# cd ..
bash-4.0# ls
com.android.internal.backup.LocalTransport
processed
pending
bash-4.0# cd com.android.internal.backup.LocalTransport/ <!--该目录下存储 backup state -->
bash-4.0# ls
com.android.providers.settings
android
@pm@
com.android.browser
com.android.providers.userdictionary
com.android.inputmethod.latin
com.android.settings
bash-4.0# cat com.android.browser
bash-4.0#
bash-4.0# cd /cache/backup <!--该目录下存储的才是真正的data -->
bash-4.0# ls
com.android.providers.settings
android
@pm@
com.android.browser
com.android.settings
bash-4.0# cd com.android.browser/
bash-4.0# ls
X2Jvb2ttYXJrc18=
bash-4.0# cat X2Jvb2ttYXJrc18\=
h++-://+++.g--g+e.c-+/G--g+e9h++-://-ica_a+eb.g--g+e.c-+/+/+ie+e_?_-+_ce=a+d_-idc+ie++Pica_ah++-://+++.yah--.c-+/Yah--!h++-://+++.+_+.c-+/MSNh++-://+++.+y_-ace.c-+/MyS-aceh++-://+++.faceb--+.c-+Faceb--+h++-://+++.+i+i-edia.-_g/ Wi+i-ediah++-://+++.ebay.c-+/eBayh++-://+++.c++.c-+/i+de|.h+++CNNh++-://+++.+y+i+e_.c-+NY Ti+e_h++-://e_-+.c-+/ESPNh++-://+++.a+az-+.c-+/A+az-+h++-://+++.+ea+he_.c-+/Weather Channelhttp://www.bbc.co.uk/BBCbash-4.0#
bash-4.0#
代码解析
默认Settings.Secure.BACKUP_ENABLED and Settings.Secure.BACKUP_PROVISIONED在数据库中没有数据 你可以通过sqlite3 将其插入到provider.settings数据库中
currentTransport = null
可以通过bmgr transport android/com.android.internal.backup.LocalTransport
来指定 代码调用BackupManagerService.selectBackupTransport(String port)
bmgr run 调用BackupManagerService.backupNow();才真正执行backup
通过
dataChanged(String packagename) 方法会执行两个动作
mPendingBackups.put(app, req)
writeToJournalLocked(packageName);
然后调用backupNow() 会执行
(new PerformBackupThread(transport, queue, oldJournal)).start(); <!--queue = mPendingBackups -->
先来看看常用变量
mBaseStateDir = /data/backup
mStateDir = /data/backup/com.android.internal.backup.LocalTransport
mDataDir = /cache
mJournalDir = /data/backup/pending
mJournal = /data/backup/pending/journal46752.tmp
以com.android.browser为例
mEverStored = /data/backup/processed
savedStateName = /data/backup/com.android.internal.backup.LocalTransport/com.android.browser backupDataName = /cache/com.android.browser.data
newStateName = /data/backup/com.android.internal.backup.LocalTransport/com.android.browser.new
然后继续
在thread的run()方法里调用doQueuedBackups(mTransport);
IBackupAgent agent = bindToAgentSynchronous(request.appInfo, mode); <!--在这里系统启动在app的agent ap的agent在AndroidManifest.xml里通过android.backup=""指定返回正在运行的backupAgent实例-->
再调用 processOneBackup(request, agent, transport);
该方法中调用 agent.doBackup(savedState, backupData, newState);即直接调用application里的agent的doBackup方法 savedState = 上面常用变量的savedStateName backupData=上面常用变量的backupDataName
newState = 上面常用变量的newStateName
这个时候备份的data在/cache/com.android.browser.data
transport.performBackup(packInfo, backupData); 这个方法做的动作就是将/cache/com.android.browser.data文件复制到/data/backup/com.android.browser/xxxxx;这个
newStateName.renameTo(savedStateName);
备份的动作做完了 整理一下
只要在AndroidManifest.xml里注册了android:backupAgent=".类名" 系统就会执行该packagename的备份
先使用backupAgent onBackup 来备份指定的文件然后transport来将备份的data移动到/data/backup/下
系统提供SharedPreferencesBackupHelper和AbsoluteFileBackupHelper两个agent;
SharedPreferencesBackupHelper用来备份SharedPreferences
AbsoluteFileBackupHelper用来备份指定的文件
使用如下
addHelper(SHARED_KEY, new SharedPreferencesBackupHelper(this, SHARED_PREFS_NAME));
files = new String[] { /data/data/com.androd.browser/test1.txt,
/data/data/com.android.browser/test2.txt}; <!--定是绝对路径 --》 addHelper("testkey", new AbsoluteFileBackupHelper(this, files));
写的很乱 不想整理了