Bmgr.java revision abce4e8714bed26a2b37b20ad3f02cf619d71c9a
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.IRestoreSession;
21import android.backup.RestoreSet;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24
25public final class Bmgr {
26    IBackupManager mBmgr;
27    IRestoreSession mRestore;
28
29    static final String BMGR_NOT_RUNNING_ERR =
30            "Error: Could not access the Backup Manager.  Is the system running?";
31    static final String TRANSPORT_NOT_RUNNING_ERR =
32        "Error: Could not access the backup transport.  Is the system running?";
33
34    private String[] mArgs;
35    private int mNextArg;
36    private String mCurArgData;
37
38    public static void main(String[] args) {
39        try {
40            new Bmgr().run(args);
41        } catch (Exception e) {
42            System.err.println("Exception caught:");
43            e.printStackTrace();
44        }
45    }
46
47    public void run(String[] args) {
48        boolean validCommand = false;
49        if (args.length < 1) {
50            showUsage();
51            return;
52        }
53
54        mBmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
55        if (mBmgr == null) {
56            System.err.println(BMGR_NOT_RUNNING_ERR);
57            return;
58        }
59
60        mArgs = args;
61        String op = args[0];
62        mNextArg = 1;
63
64        if ("run".equals(op)) {
65            doRun();
66            return;
67        }
68
69        if ("backup".equals(op)) {
70            doBackup();
71            return;
72        }
73
74        if ("list".equals(op)) {
75            doList();
76            return;
77        }
78
79        if ("restore".equals(op)) {
80            doRestore();
81            return;
82        }
83
84        if ("transport".equals(op)) {
85            doTransport();
86            return;
87        }
88
89        System.err.println("Unknown command");
90        showUsage();
91    }
92
93    private void doRun() {
94        try {
95            mBmgr.backupNow();
96        } catch (RemoteException e) {
97            System.err.println(e.toString());
98            System.err.println(BMGR_NOT_RUNNING_ERR);
99        }
100    }
101
102    private void doBackup() {
103        boolean isFull = false;
104        String pkg = nextArg();
105        if ("-f".equals(pkg)) {
106            isFull = true;
107            pkg = nextArg();
108        }
109
110        if (pkg == null || pkg.startsWith("-")) {
111            showUsage();
112            return;
113        }
114
115        try {
116            // !!! TODO: handle full backup
117            mBmgr.dataChanged(pkg);
118        } catch (RemoteException e) {
119            System.err.println(e.toString());
120            System.err.println(BMGR_NOT_RUNNING_ERR);
121        }
122    }
123
124    private void doTransport() {
125        try {
126            int which = Integer.parseInt(nextArg());
127            int old = mBmgr.selectBackupTransport(which);
128            System.out.println("Selected transport " + which + " (formerly " + old + ")");
129        } catch (NumberFormatException e) {
130            showUsage();
131        } catch (RemoteException e) {
132            System.err.println(e.toString());
133            System.err.println(BMGR_NOT_RUNNING_ERR);
134        }
135    }
136
137    private void doList() {
138        String arg = nextArg();     // sets, transports, packages set#
139        if ("transports".equals(arg)) {
140            doListTransports();
141            return;
142        }
143
144        // The rest of the 'list' options work with a restore session on the current transport
145        try {
146            int curTransport = mBmgr.getCurrentTransport();
147            mRestore = mBmgr.beginRestoreSession(curTransport);
148            if (mRestore == null) {
149                System.err.println(BMGR_NOT_RUNNING_ERR);
150                return;
151            }
152
153            if ("sets".equals(arg)) {
154                doListRestoreSets();
155            }
156
157            mRestore.endRestoreSession();
158        } catch (RemoteException e) {
159            System.err.println(e.toString());
160            System.err.println(BMGR_NOT_RUNNING_ERR);
161        }
162    }
163
164    private void doListTransports() {
165    }
166
167    private void doListRestoreSets() {
168        try {
169            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
170            if (sets == null || sets.length == 0) {
171                System.out.println("No restore sets available");
172            } else {
173                for (RestoreSet s : sets) {
174                    System.out.println("  " + s.token + " : " + s.name);
175                }
176            }
177        } catch (RemoteException e) {
178            System.err.println(e.toString());
179            System.err.println(TRANSPORT_NOT_RUNNING_ERR);
180        }
181    }
182
183    private void doRestore() {
184        int token;
185        try {
186            token = Integer.parseInt(nextArg());
187        } catch (NumberFormatException e) {
188            showUsage();
189            return;
190        }
191
192        try {
193            int curTransport = mBmgr.getCurrentTransport();
194            mRestore = mBmgr.beginRestoreSession(curTransport);
195            if (mRestore == null) {
196                System.err.println(BMGR_NOT_RUNNING_ERR);
197                return;
198            }
199            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
200            for (RestoreSet s : sets) {
201                if (s.token == token) {
202                    System.out.println("Scheduling restore: " + s.name);
203                    mRestore.performRestore(token);
204                    break;
205                }
206            }
207            mRestore.endRestoreSession();
208        } catch (RemoteException e) {
209            System.err.println(e.toString());
210            System.err.println(BMGR_NOT_RUNNING_ERR);
211        }
212    }
213
214    private String nextArg() {
215        if (mNextArg >= mArgs.length) {
216            return null;
217        }
218        String arg = mArgs[mNextArg];
219        mNextArg++;
220        return arg;
221    }
222
223    private static void showUsage() {
224        System.err.println("usage: bmgr [backup|restore|list|transport|run]");
225        System.err.println("       bmgr backup [-f] package");
226        System.err.println("       bmgr list sets");
227        System.err.println("       #bmgr list transports");
228        System.err.println("       #bmgr transport which#");
229        System.err.println("       bmgr restore token#");
230        System.err.println("       bmgr run");
231    }
232}