24
Data Backup Kewang

Data backup

Embed Size (px)

Citation preview

Page 1: Data backup

Data Backup

Kewang

Page 2: Data backup

2

Backup data to the cloud

Restore data when reinstalled app

Requires API Level 8+

What is Backup Service ?

Page 3: Data backup

3

Getting Started

Page 4: Data backup

4

Getting Started

Register service & declare agent

Extend backup agent

BackupAgent

BackupAgentHelper

Page 5: Data backup

5

Register service & declare agent

Page 6: Data backup

6

Register service & declare agent<manifest>

<application android:backupAgent="ExampleAgent" > <meta-data android:name="com.google.android.backup.api_key" android:value="AEdPqrEAAAAIW4p30C1GTNjzBOqWrb0clI7_OCWxm3ddIgkKhw" />

</application>

</manifest> backupAgentbackup.api_key

Page 7: Data backup

7

Extend backup agent

BackupAgent

Version data format

Back up the portions of data

Back up data in a database

Page 8: Data backup

8

Extend BackupAgent

public class ExampleAgent extends BackupAgent { @Override public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) { // TODO }

@Override public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) { // TODO }}

Page 9: Data backup

9

Extend BackupAgent

onBackupParcelFileDescriptor oldState

read-only last backup state, may be nullcontains representation of the datafrom the last onBackup's newState

BackupDataOutput datause to deliver backup data

ParcelFileDescriptor newStaterecord the final backup state after writing data

Page 10: Data backup

10

Extend BackupAgent

onRestoreBackupDataInput data

can read backup data

int appVersionCodeManifest attribute when data was backed upcan cross-check app version

ParcelFileDescriptor newStatewrite the state of data

Page 11: Data backup

11

Implement onBackup (1/2)boolean doBackup = (oldState == null);

synchronized (BackupRestoreActivity.sDataLock) { RandomAccessFile f = new RandomAccessFile(mFile, "r");

mFilling = f.readInt(); mAddMayo = f.readBoolean(); mAddTomato = f.readBoolean();

f.close();}

if (!doBackup) { doBackup = compareStateFile(oldState);}

read local fileis newest

Page 12: Data backup

12

if (doBackup) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(buf);

out.writeInt(mFilling); out.writeBoolean(mAddMayo); out.writeBoolean(mAddTomato);

byte[] buffer = buf.toByteArray(); int len = buffer.length;

data.writeEntityHeader(APP_DATA_KEY, len); data.writeEntityData(buffer, len);}

writeStateFile(newState);

Implement onBackup (2/2)

back up

Page 13: Data backup

13

Implement onRestore (1/3)

String key = data.getKey();int size = data.getDataSize();

if (!APP_DATA_KEY.equals(key)) { data.skipEntityData();}

data is invalid, skip it

Page 14: Data backup

14

Implement onRestore (2/3)

if (APP_DATA_KEY.equals(key)) { byte[] buf = new byte[size];

data.readEntityData(buf, 0, size);

ByteArrayInputStream is = new ByteArrayInputStream(buf); DataInputStream in = new DataInputStream(is);

mFilling = in.readInt(); mAddMayo = in.readBoolean(); mAddTomato = in.readBoolean();

data is valid, read it

Page 15: Data backup

15

Implement onRestore (3/3)

synchronized (BackupRestoreActivity.sDataLock) { RandomAccessFile f = new RandomAccessFile(mFile, "rw");

f.setLength(0L); f.writeInt(mFilling); f.writeBoolean(mAddMayo); f.writeBoolean(mAddTomato);

f.close(); }}

writeStateFile(newState);

write data to local file

Page 16: Data backup

16

Another backup agent

BackupAgentHelper

Back up SharedPreferences

Back up a file

Page 17: Data backup

17

Extend BackupAgentHelper

public class ExampleAgent extends BackupAgentHelper { @Override public void onCreate() { FileBackupHelper helper = new FileBackupHelper(this, "file");

addHelper("key", helper); }}

can also use SharedPreferencesBackupHelper

Page 18: Data backup

18

How to using & testing it ?

Page 19: Data backup

19

How to using & testing it ?

mBackupManager.dataChanged();

Request backup

Page 20: Data backup

20

How to using & testing it ?

mBackupManager.requestRestore(new RestoreObserver() { @Override public void restoreStarting(int numPackages) { }

@Override public void onUpdate(int nowBeingRestored,String currentPackage) { }

@Override public void restoreFinished(int error) { }});

Request restore

Page 21: Data backup

21

How to using & testing it ?

adb shell bmgr run

adb shell bmgr backup <package>

adb shell bmgr restore <package>

adb shell bmgr wipe <package>

adb shell bmgr enable <boolean>

Page 22: Data backup

22

FAQ

Storage size ?

unknown

Backup data TTL ?

unknown

Can synchronizing ?

CANNOT

Page 24: Data backup

24