Bmgr.java revision 8472581aa32eee1368de379c2c079ea0a66baa3c
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.commands.bmgr;
18
19import android.backup.IBackupManager;
20import android.backup.IRestoreObserver;
21import android.backup.IRestoreSession;
22import android.backup.RestoreSet;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25
26public final class Bmgr {
27    IBackupManager mBmgr;
28    IRestoreSession mRestore;
29
30    static final String BMGR_NOT_RUNNING_ERR =
31            "Error: Could not access the Backup Manager.  Is the system running?";
32    static final String TRANSPORT_NOT_RUNNING_ERR =
33        "Error: Could not access the backup transport.  Is the system running?";
34
35    private String[] mArgs;
36    private int mNextArg;
37    private String mCurArgData;
38
39    public static void main(String[] args) {
40        try {
41            new Bmgr().run(args);
42        } catch (Exception e) {
43            System.err.println("Exception caught:");
44            e.printStackTrace();
45        }
46    }
47
48    public void run(String[] args) {
49        boolean validCommand = false;
50        if (args.length < 1) {
51            showUsage();
52            return;
53        }
54
55        mBmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
56        if (mBmgr == null) {
57            System.err.println(BMGR_NOT_RUNNING_ERR);
58            return;
59        }
60
61        mArgs = args;
62        String op = args[0];
63        mNextArg = 1;
64
65        if ("enabled".equals(op)) {
66            doEnabled();
67            return;
68        }
69
70        if ("enable".equals(op)) {
71            doEnable();
72            return;
73        }
74
75        if ("run".equals(op)) {
76            doRun();
77            return;
78        }
79
80        if ("backup".equals(op)) {
81            doBackup();
82            return;
83        }
84
85        if ("list".equals(op)) {
86            doList();
87            return;
88        }
89
90        if ("restore".equals(op)) {
91            doRestore();
92            return;
93        }
94
95        if ("transport".equals(op)) {
96            doTransport();
97            return;
98        }
99
100        if ("wipe".equals(op)) {
101            doWipe();
102            return;
103        }
104
105        System.err.println("Unknown command");
106        showUsage();
107    }
108
109    private String enableToString(boolean enabled) {
110        return enabled ? "enabled" : "disabled";
111    }
112
113    private void doEnabled() {
114        try {
115            boolean isEnabled = mBmgr.isBackupEnabled();
116            System.out.println("Backup Manager currently "
117                    + enableToString(isEnabled));
118        } catch (RemoteException e) {
119            System.err.println(e.toString());
120            System.err.println(BMGR_NOT_RUNNING_ERR);
121        }
122    }
123
124    private void doEnable() {
125        String arg = nextArg();
126        if (arg == null) {
127            showUsage();
128            return;
129        }
130
131        try {
132            boolean enable = Boolean.parseBoolean(arg);
133            mBmgr.setBackupEnabled(enable);
134            System.out.println("Backup Manager now " + enableToString(enable));
135        } catch (NumberFormatException e) {
136            showUsage();
137            return;
138        } catch (RemoteException e) {
139            System.err.println(e.toString());
140            System.err.println(BMGR_NOT_RUNNING_ERR);
141        }
142    }
143
144    private void doRun() {
145        try {
146            mBmgr.backupNow();
147        } catch (RemoteException e) {
148            System.err.println(e.toString());
149            System.err.println(BMGR_NOT_RUNNING_ERR);
150        }
151    }
152
153    private void doBackup() {
154        boolean isFull = false;
155        String pkg = nextArg();
156        if ("-f".equals(pkg)) {
157            isFull = true;
158            pkg = nextArg();
159        }
160
161        if (pkg == null || pkg.startsWith("-")) {
162            showUsage();
163            return;
164        }
165
166        try {
167            // !!! TODO: handle full backup
168            mBmgr.dataChanged(pkg);
169        } catch (RemoteException e) {
170            System.err.println(e.toString());
171            System.err.println(BMGR_NOT_RUNNING_ERR);
172        }
173    }
174
175    private void doTransport() {
176        try {
177            String which = nextArg();
178            String old = mBmgr.selectBackupTransport(which);
179            if (old == null) {
180                System.out.println("Unknown transport '" + which
181                        + "' specified; no changes made.");
182            } else {
183                System.out.println("Selected transport " + which + " (formerly " + old + ")");
184            }
185        } catch (RemoteException e) {
186            System.err.println(e.toString());
187            System.err.println(BMGR_NOT_RUNNING_ERR);
188        }
189    }
190
191    private void doWipe() {
192        String pkg = nextArg();
193        if (pkg == null) {
194            showUsage();
195            return;
196        }
197
198        try {
199            mBmgr.clearBackupData(pkg);
200            System.out.println("Wiped backup data for " + pkg);
201        } catch (RemoteException e) {
202            System.err.println(e.toString());
203            System.err.println(BMGR_NOT_RUNNING_ERR);
204        }
205    }
206
207    private void doList() {
208        String arg = nextArg();     // sets, transports, packages set#
209        if ("transports".equals(arg)) {
210            doListTransports();
211            return;
212        }
213
214        // The rest of the 'list' options work with a restore session on the current transport
215        try {
216            String curTransport = mBmgr.getCurrentTransport();
217            mRestore = mBmgr.beginRestoreSession(curTransport);
218            if (mRestore == null) {
219                System.err.println(BMGR_NOT_RUNNING_ERR);
220                return;
221            }
222
223            if ("sets".equals(arg)) {
224                doListRestoreSets();
225            } else if ("transports".equals(arg)) {
226                doListTransports();
227            }
228
229            mRestore.endRestoreSession();
230        } catch (RemoteException e) {
231            System.err.println(e.toString());
232            System.err.println(BMGR_NOT_RUNNING_ERR);
233        }
234    }
235
236    private void doListTransports() {
237        try {
238            String current = mBmgr.getCurrentTransport();
239            String[] transports = mBmgr.listAllTransports();
240            if (transports == null || transports.length == 0) {
241                System.out.println("No transports available.");
242                return;
243            }
244
245            for (String t : transports) {
246                String pad = (t.equals(current)) ? "  * " : "    ";
247                System.out.println(pad + t);
248            }
249        } catch (RemoteException e) {
250            System.err.println(e.toString());
251            System.err.println(BMGR_NOT_RUNNING_ERR);
252        }
253    }
254
255    private void doListRestoreSets() {
256        try {
257            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
258            if (sets == null || sets.length == 0) {
259                System.out.println("No restore sets available");
260            } else {
261                printRestoreSets(sets);
262            }
263        } catch (RemoteException e) {
264            System.err.println(e.toString());
265            System.err.println(TRANSPORT_NOT_RUNNING_ERR);
266        }
267    }
268
269    private void printRestoreSets(RestoreSet[] sets) {
270        for (RestoreSet s : sets) {
271            System.out.println("  " + Long.toHexString(s.token) + " : " + s.name);
272        }
273    }
274
275    class RestoreObserver extends IRestoreObserver.Stub {
276        boolean done;
277        public void restoreStarting(int numPackages) {
278            System.out.println("restoreStarting: " + numPackages + " packages");
279        }
280
281        public void onUpdate(int nowBeingRestored) {
282            System.out.println("onUpdate: " + nowBeingRestored);
283        }
284
285        public void restoreFinished(int error) {
286            System.out.println("restoreFinished: " + error);
287            synchronized (this) {
288                done = true;
289                this.notify();
290            }
291        }
292    }
293
294    private void doRestore() {
295        long token;
296        try {
297            token = Long.parseLong(nextArg(), 16);
298        } catch (NumberFormatException e) {
299            showUsage();
300            return;
301        }
302
303        RestoreObserver observer = new RestoreObserver();
304
305        try {
306            boolean didRestore = false;
307            String curTransport = mBmgr.getCurrentTransport();
308            mRestore = mBmgr.beginRestoreSession(curTransport);
309            if (mRestore == null) {
310                System.err.println(BMGR_NOT_RUNNING_ERR);
311                return;
312            }
313            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
314            if (sets != null) {
315                for (RestoreSet s : sets) {
316                    if (s.token == token) {
317                        System.out.println("Scheduling restore: " + s.name);
318                        didRestore = (mRestore.restoreAll(token, observer) == 0);
319                        break;
320                    }
321                }
322            }
323            if (!didRestore) {
324                if (sets == null || sets.length == 0) {
325                    System.out.println("No available restore sets; no restore performed");
326                } else {
327                    System.out.println("No matching restore set token.  Available sets:");
328                    printRestoreSets(sets);
329                }
330            }
331
332            // if we kicked off a restore successfully, we have to wait for it
333            // to complete before we can shut down the restore session safely
334            if (didRestore) {
335                synchronized (observer) {
336                    while (!observer.done) {
337                        try {
338                            observer.wait();
339                        } catch (InterruptedException ex) {
340                        }
341                    }
342                }
343            }
344
345            // once the restore has finished, close down the session and we're done
346            mRestore.endRestoreSession();
347        } catch (RemoteException e) {
348            System.err.println(e.toString());
349            System.err.println(BMGR_NOT_RUNNING_ERR);
350        }
351
352        System.out.println("done");
353    }
354
355    private String nextArg() {
356        if (mNextArg >= mArgs.length) {
357            return null;
358        }
359        String arg = mArgs[mNextArg];
360        mNextArg++;
361        return arg;
362    }
363
364    private static void showUsage() {
365        System.err.println("usage: bmgr [backup|restore|list|transport|run]");
366        System.err.println("       bmgr backup PACKAGE");
367        System.err.println("       bmgr enable BOOL");
368        System.err.println("       bmgr enabled");
369        System.err.println("       bmgr list transports");
370        System.err.println("       bmgr list sets");
371        System.err.println("       bmgr transport WHICH");
372        System.err.println("       bmgr restore TOKEN");
373        System.err.println("       bmgr run");
374        System.err.println("       bmgr wipe PACKAGE");
375        System.err.println("");
376        System.err.println("The 'backup' command schedules a backup pass for the named package.");
377        System.err.println("Note that the backup pass will effectively be a no-op if the package");
378        System.err.println("does not actually have changed data to store.");
379        System.err.println("");
380        System.err.println("The 'enable' command enables or disables the entire backup mechanism.");
381        System.err.println("If the argument is 'true' it will be enabled, otherwise it will be");
382        System.err.println("disabled.  When disabled, neither backup or restore operations will");
383        System.err.println("be performed.");
384        System.err.println("");
385        System.err.println("The 'enabled' command reports the current enabled/disabled state of");
386        System.err.println("the backup mechanism.");
387        System.err.println("");
388        System.err.println("The 'list transports' command reports the names of the backup transports");
389        System.err.println("currently available on the device.  These names can be passed as arguments");
390        System.err.println("to the 'transport' command.  The currently selected transport is indicated");
391        System.err.println("with a '*' character.");
392        System.err.println("");
393        System.err.println("The 'list sets' command reports the token and name of each restore set");
394        System.err.println("available to the device via the current transport.");
395        System.err.println("");
396        System.err.println("The 'transport' command designates the named transport as the currently");
397        System.err.println("active one.  This setting is persistent across reboots.");
398        System.err.println("");
399        System.err.println("The 'restore' command initiates a restore operation, using the restore set");
400        System.err.println("from the current transport whose token matches the argument.");
401        System.err.println("");
402        System.err.println("The 'run' command causes any scheduled backup operation to be initiated");
403        System.err.println("immediately, without the usual waiting period for batching together");
404        System.err.println("data changes.");
405        System.err.println("");
406        System.err.println("The 'wipe' command causes all backed-up data for the given package to be");
407        System.err.println("erased from the current transport's storage.  The next backup operation");
408        System.err.println("that the given application performs will rewrite its entire data set.");
409    }
410}
411