Bmgr.java revision 7e76ff1c409bc22e89ed09ef90161164dae40838
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.app.backup.RestoreSet; 20import android.app.backup.IBackupManager; 21import android.app.backup.IRestoreObserver; 22import android.app.backup.IRestoreSession; 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 if (which == null) { 179 showUsage(); 180 return; 181 } 182 183 String old = mBmgr.selectBackupTransport(which); 184 if (old == null) { 185 System.out.println("Unknown transport '" + which 186 + "' specified; no changes made."); 187 } else { 188 System.out.println("Selected transport " + which + " (formerly " + old + ")"); 189 } 190 } catch (RemoteException e) { 191 System.err.println(e.toString()); 192 System.err.println(BMGR_NOT_RUNNING_ERR); 193 } 194 } 195 196 private void doWipe() { 197 String pkg = nextArg(); 198 if (pkg == null) { 199 showUsage(); 200 return; 201 } 202 203 try { 204 mBmgr.clearBackupData(pkg); 205 System.out.println("Wiped backup data for " + pkg); 206 } catch (RemoteException e) { 207 System.err.println(e.toString()); 208 System.err.println(BMGR_NOT_RUNNING_ERR); 209 } 210 } 211 212 private void doList() { 213 String arg = nextArg(); // sets, transports, packages set# 214 if ("transports".equals(arg)) { 215 doListTransports(); 216 return; 217 } 218 219 // The rest of the 'list' options work with a restore session on the current transport 220 try { 221 String curTransport = mBmgr.getCurrentTransport(); 222 mRestore = mBmgr.beginRestoreSession(curTransport); 223 if (mRestore == null) { 224 System.err.println(BMGR_NOT_RUNNING_ERR); 225 return; 226 } 227 228 if ("sets".equals(arg)) { 229 doListRestoreSets(); 230 } else if ("transports".equals(arg)) { 231 doListTransports(); 232 } 233 234 mRestore.endRestoreSession(); 235 } catch (RemoteException e) { 236 System.err.println(e.toString()); 237 System.err.println(BMGR_NOT_RUNNING_ERR); 238 } 239 } 240 241 private void doListTransports() { 242 try { 243 String current = mBmgr.getCurrentTransport(); 244 String[] transports = mBmgr.listAllTransports(); 245 if (transports == null || transports.length == 0) { 246 System.out.println("No transports available."); 247 return; 248 } 249 250 for (String t : transports) { 251 String pad = (t.equals(current)) ? " * " : " "; 252 System.out.println(pad + t); 253 } 254 } catch (RemoteException e) { 255 System.err.println(e.toString()); 256 System.err.println(BMGR_NOT_RUNNING_ERR); 257 } 258 } 259 260 private void doListRestoreSets() { 261 try { 262 RestoreObserver observer = new RestoreObserver(); 263 int err = mRestore.getAvailableRestoreSets(observer); 264 if (err != 0) { 265 System.out.println("Unable to request restore sets"); 266 } else { 267 observer.waitForCompletion(); 268 printRestoreSets(observer.sets); 269 } 270 } catch (RemoteException e) { 271 System.err.println(e.toString()); 272 System.err.println(TRANSPORT_NOT_RUNNING_ERR); 273 } 274 } 275 276 private void printRestoreSets(RestoreSet[] sets) { 277 for (RestoreSet s : sets) { 278 System.out.println(" " + Long.toHexString(s.token) + " : " + s.name); 279 } 280 } 281 282 class RestoreObserver extends IRestoreObserver.Stub { 283 boolean done; 284 RestoreSet[] sets = null; 285 286 public void restoreSetsAvailable(RestoreSet[] result) { 287 synchronized (this) { 288 sets = result; 289 done = true; 290 this.notify(); 291 } 292 } 293 294 public void restoreStarting(int numPackages) { 295 System.out.println("restoreStarting: " + numPackages + " packages"); 296 } 297 298 public void onUpdate(int nowBeingRestored, String currentPackage) { 299 System.out.println("onUpdate: " + nowBeingRestored + " = " + currentPackage); 300 } 301 302 public void restoreFinished(int error) { 303 System.out.println("restoreFinished: " + error); 304 synchronized (this) { 305 done = true; 306 this.notify(); 307 } 308 } 309 310 public void waitForCompletion() { 311 // The restoreFinished() callback will throw the 'done' flag; we 312 // just sit and wait on that notification. 313 synchronized (this) { 314 while (!this.done) { 315 try { 316 this.wait(); 317 } catch (InterruptedException ex) { 318 } 319 } 320 } 321 } 322 } 323 324 private void doRestore() { 325 String arg = nextArg(); 326 if (arg == null) { 327 showUsage(); 328 return; 329 } 330 331 if (arg.indexOf('.') >= 0) { 332 // it's a package name 333 doRestorePackage(arg); 334 } else { 335 try { 336 long token = Long.parseLong(arg, 16); 337 doRestoreAll(token); 338 } catch (NumberFormatException e) { 339 showUsage(); 340 return; 341 } 342 } 343 344 System.out.println("done"); 345 } 346 347 private void doRestorePackage(String pkg) { 348 try { 349 String curTransport = mBmgr.getCurrentTransport(); 350 mRestore = mBmgr.beginRestoreSession(curTransport); 351 if (mRestore == null) { 352 System.err.println(BMGR_NOT_RUNNING_ERR); 353 return; 354 } 355 356 RestoreObserver observer = new RestoreObserver(); 357 int err = mRestore.restorePackage(pkg, observer); 358 if (err == 0) { 359 // Off and running -- wait for the restore to complete 360 observer.waitForCompletion(); 361 } else { 362 System.err.println("Unable to restore package " + pkg); 363 } 364 365 // And finally shut down the session 366 mRestore.endRestoreSession(); 367 } catch (RemoteException e) { 368 System.err.println(e.toString()); 369 System.err.println(BMGR_NOT_RUNNING_ERR); 370 } 371 } 372 373 private void doRestoreAll(long token) { 374 RestoreObserver observer = new RestoreObserver(); 375 376 try { 377 boolean didRestore = false; 378 String curTransport = mBmgr.getCurrentTransport(); 379 mRestore = mBmgr.beginRestoreSession(curTransport); 380 if (mRestore == null) { 381 System.err.println(BMGR_NOT_RUNNING_ERR); 382 return; 383 } 384 RestoreSet[] sets = null; 385 int err = mRestore.getAvailableRestoreSets(observer); 386 if (err == 0) { 387 observer.waitForCompletion(); 388 sets = observer.sets; 389 for (RestoreSet s : sets) { 390 if (s.token == token) { 391 System.out.println("Scheduling restore: " + s.name); 392 didRestore = (mRestore.restoreAll(token, observer) == 0); 393 break; 394 } 395 } 396 } 397 if (!didRestore) { 398 if (sets == null || sets.length == 0) { 399 System.out.println("No available restore sets; no restore performed"); 400 } else { 401 System.out.println("No matching restore set token. Available sets:"); 402 printRestoreSets(sets); 403 } 404 } 405 406 // if we kicked off a restore successfully, we have to wait for it 407 // to complete before we can shut down the restore session safely 408 if (didRestore) { 409 observer.waitForCompletion(); 410 } 411 412 // once the restore has finished, close down the session and we're done 413 mRestore.endRestoreSession(); 414 } catch (RemoteException e) { 415 System.err.println(e.toString()); 416 System.err.println(BMGR_NOT_RUNNING_ERR); 417 } 418 } 419 420 private String nextArg() { 421 if (mNextArg >= mArgs.length) { 422 return null; 423 } 424 String arg = mArgs[mNextArg]; 425 mNextArg++; 426 return arg; 427 } 428 429 private static void showUsage() { 430 System.err.println("usage: bmgr [backup|restore|list|transport|run]"); 431 System.err.println(" bmgr backup PACKAGE"); 432 System.err.println(" bmgr enable BOOL"); 433 System.err.println(" bmgr enabled"); 434 System.err.println(" bmgr list transports"); 435 System.err.println(" bmgr list sets"); 436 System.err.println(" bmgr transport WHICH"); 437 System.err.println(" bmgr restore TOKEN"); 438 System.err.println(" bmgr restore PACKAGE"); 439 System.err.println(" bmgr run"); 440 System.err.println(" bmgr wipe PACKAGE"); 441 System.err.println(""); 442 System.err.println("The 'backup' command schedules a backup pass for the named package."); 443 System.err.println("Note that the backup pass will effectively be a no-op if the package"); 444 System.err.println("does not actually have changed data to store."); 445 System.err.println(""); 446 System.err.println("The 'enable' command enables or disables the entire backup mechanism."); 447 System.err.println("If the argument is 'true' it will be enabled, otherwise it will be"); 448 System.err.println("disabled. When disabled, neither backup or restore operations will"); 449 System.err.println("be performed."); 450 System.err.println(""); 451 System.err.println("The 'enabled' command reports the current enabled/disabled state of"); 452 System.err.println("the backup mechanism."); 453 System.err.println(""); 454 System.err.println("The 'list transports' command reports the names of the backup transports"); 455 System.err.println("currently available on the device. These names can be passed as arguments"); 456 System.err.println("to the 'transport' command. The currently selected transport is indicated"); 457 System.err.println("with a '*' character."); 458 System.err.println(""); 459 System.err.println("The 'list sets' command reports the token and name of each restore set"); 460 System.err.println("available to the device via the current transport."); 461 System.err.println(""); 462 System.err.println("The 'transport' command designates the named transport as the currently"); 463 System.err.println("active one. This setting is persistent across reboots."); 464 System.err.println(""); 465 System.err.println("The 'restore' command when given a restore token initiates a full-system"); 466 System.err.println("restore operation from the currently active transport. It will deliver"); 467 System.err.println("the restore set designated by the TOKEN argument to each application"); 468 System.err.println("that had contributed data to that restore set."); 469 System.err.println(""); 470 System.err.println("The 'restore' command when given a package name intiates a restore of"); 471 System.err.println("just that one package according to the restore set selection algorithm"); 472 System.err.println("used by the RestoreSession.restorePackage() method."); 473 System.err.println(""); 474 System.err.println("The 'run' command causes any scheduled backup operation to be initiated"); 475 System.err.println("immediately, without the usual waiting period for batching together"); 476 System.err.println("data changes."); 477 System.err.println(""); 478 System.err.println("The 'wipe' command causes all backed-up data for the given package to be"); 479 System.err.println("erased from the current transport's storage. The next backup operation"); 480 System.err.println("that the given application performs will rewrite its entire data set."); 481 } 482} 483