Bmgr.java revision 7d411a3b947ba82d1d57f73c0fa698c3b9c95892
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 public void waitForCompletion() { 294 // The restoreFinished() callback will throw the 'done' flag; we 295 // just sit and wait on that notification. 296 synchronized (this) { 297 while (!this.done) { 298 try { 299 this.wait(); 300 } catch (InterruptedException ex) { 301 } 302 } 303 } 304 } 305 } 306 307 private void doRestore() { 308 String arg = nextArg(); 309 if (arg.indexOf('.') >= 0) { 310 // it's a package name 311 doRestorePackage(arg); 312 } else { 313 try { 314 long token = Long.parseLong(nextArg(), 16); 315 doRestoreAll(token); 316 } catch (NumberFormatException e) { 317 showUsage(); 318 return; 319 } 320 } 321 322 System.out.println("done"); 323 } 324 325 private void doRestorePackage(String pkg) { 326 try { 327 String curTransport = mBmgr.getCurrentTransport(); 328 mRestore = mBmgr.beginRestoreSession(curTransport); 329 if (mRestore == null) { 330 System.err.println(BMGR_NOT_RUNNING_ERR); 331 return; 332 } 333 334 RestoreObserver observer = new RestoreObserver(); 335 int err = mRestore.restorePackage(pkg, observer); 336 if (err == 0) { 337 // Off and running -- wait for the restore to complete 338 observer.waitForCompletion(); 339 } else { 340 System.err.println("Unable to restore package " + pkg); 341 } 342 343 // And finally shut down the session 344 mRestore.endRestoreSession(); 345 } catch (RemoteException e) { 346 System.err.println(e.toString()); 347 System.err.println(BMGR_NOT_RUNNING_ERR); 348 } 349 } 350 351 private void doRestoreAll(long token) { 352 RestoreObserver observer = new RestoreObserver(); 353 354 try { 355 boolean didRestore = false; 356 String curTransport = mBmgr.getCurrentTransport(); 357 mRestore = mBmgr.beginRestoreSession(curTransport); 358 if (mRestore == null) { 359 System.err.println(BMGR_NOT_RUNNING_ERR); 360 return; 361 } 362 RestoreSet[] sets = mRestore.getAvailableRestoreSets(); 363 if (sets != null) { 364 for (RestoreSet s : sets) { 365 if (s.token == token) { 366 System.out.println("Scheduling restore: " + s.name); 367 didRestore = (mRestore.restoreAll(token, observer) == 0); 368 break; 369 } 370 } 371 } 372 if (!didRestore) { 373 if (sets == null || sets.length == 0) { 374 System.out.println("No available restore sets; no restore performed"); 375 } else { 376 System.out.println("No matching restore set token. Available sets:"); 377 printRestoreSets(sets); 378 } 379 } 380 381 // if we kicked off a restore successfully, we have to wait for it 382 // to complete before we can shut down the restore session safely 383 if (didRestore) { 384 observer.waitForCompletion(); 385 } 386 387 // once the restore has finished, close down the session and we're done 388 mRestore.endRestoreSession(); 389 } catch (RemoteException e) { 390 System.err.println(e.toString()); 391 System.err.println(BMGR_NOT_RUNNING_ERR); 392 } 393 } 394 395 private String nextArg() { 396 if (mNextArg >= mArgs.length) { 397 return null; 398 } 399 String arg = mArgs[mNextArg]; 400 mNextArg++; 401 return arg; 402 } 403 404 private static void showUsage() { 405 System.err.println("usage: bmgr [backup|restore|list|transport|run]"); 406 System.err.println(" bmgr backup PACKAGE"); 407 System.err.println(" bmgr enable BOOL"); 408 System.err.println(" bmgr enabled"); 409 System.err.println(" bmgr list transports"); 410 System.err.println(" bmgr list sets"); 411 System.err.println(" bmgr transport WHICH"); 412 System.err.println(" bmgr restore TOKEN"); 413 System.err.println(" bmgr restore PACKAGE"); 414 System.err.println(" bmgr run"); 415 System.err.println(" bmgr wipe PACKAGE"); 416 System.err.println(""); 417 System.err.println("The 'backup' command schedules a backup pass for the named package."); 418 System.err.println("Note that the backup pass will effectively be a no-op if the package"); 419 System.err.println("does not actually have changed data to store."); 420 System.err.println(""); 421 System.err.println("The 'enable' command enables or disables the entire backup mechanism."); 422 System.err.println("If the argument is 'true' it will be enabled, otherwise it will be"); 423 System.err.println("disabled. When disabled, neither backup or restore operations will"); 424 System.err.println("be performed."); 425 System.err.println(""); 426 System.err.println("The 'enabled' command reports the current enabled/disabled state of"); 427 System.err.println("the backup mechanism."); 428 System.err.println(""); 429 System.err.println("The 'list transports' command reports the names of the backup transports"); 430 System.err.println("currently available on the device. These names can be passed as arguments"); 431 System.err.println("to the 'transport' command. The currently selected transport is indicated"); 432 System.err.println("with a '*' character."); 433 System.err.println(""); 434 System.err.println("The 'list sets' command reports the token and name of each restore set"); 435 System.err.println("available to the device via the current transport."); 436 System.err.println(""); 437 System.err.println("The 'transport' command designates the named transport as the currently"); 438 System.err.println("active one. This setting is persistent across reboots."); 439 System.err.println(""); 440 System.err.println("The 'restore' command when given a restore token initiates a full-system"); 441 System.err.println("restore operation from the currently active transport. It will deliver"); 442 System.err.println("the restore set designated by the TOKEN argument to each application"); 443 System.err.println("that had contributed data to that restore set."); 444 System.err.println(""); 445 System.err.println("The 'restore' command when given a package name intiates a restore of"); 446 System.err.println("just that one package according to the restore set selection algorithm"); 447 System.err.println("used by the RestoreSession.restorePackage() method."); 448 System.err.println(""); 449 System.err.println("The 'run' command causes any scheduled backup operation to be initiated"); 450 System.err.println("immediately, without the usual waiting period for batching together"); 451 System.err.println("data changes."); 452 System.err.println(""); 453 System.err.println("The 'wipe' command causes all backed-up data for the given package to be"); 454 System.err.println("erased from the current transport's storage. The next backup operation"); 455 System.err.println("that the given application performs will rewrite its entire data set."); 456 } 457} 458