Backup.java revision 4a627c71ff53a4fca1f961f4b1dcc0461df18a06
1/* 2 * Copyright (C) 2011 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.bu; 18 19import android.app.backup.IBackupManager; 20import android.os.ParcelFileDescriptor; 21import android.os.RemoteException; 22import android.os.ServiceManager; 23import android.util.Log; 24 25import java.io.FileDescriptor; 26import java.io.IOException; 27import java.util.ArrayList; 28 29public final class Backup { 30 static final String TAG = "Backup"; 31 32 private static String[] mArgs; 33 private int mNextArg; 34 35 public static void main(String[] args) { 36 mArgs = args; 37 try { 38 new Backup().run(); 39 } catch (Exception e) { 40 Log.e(TAG, "Error running backup", e); 41 } 42 Log.d(TAG, "Finished."); 43 } 44 45 public void run() { 46 IBackupManager bmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); 47 if (bmgr == null) { 48 System.err.println("ERROR: could not contact backup manager"); 49 return; 50 } 51 52 ArrayList<String> packages = new ArrayList<String>(); 53 boolean saveApks = false; 54 boolean saveShared = false; 55 boolean doEverything = false; 56 57 String arg; 58 while ((arg = nextArg()) != null) { 59 60 if (arg.startsWith("-")) { 61 if ("-apk".equals(arg)) { 62 saveApks = true; 63 } else if ("-noapk".equals(arg)) { 64 saveApks = false; 65 } else if ("-shared".equals(arg)) { 66 saveShared = true; 67 } else if ("-noshared".equals(arg)) { 68 saveShared = false; 69 } else if ("-all".equals(arg)) { 70 doEverything = true; 71 } else { 72 System.err.println("ERROR: unknown flag " + arg); 73 return; 74 } 75 } else { 76 // Not a flag; treat as a package name 77 packages.add(arg); 78 } 79 } 80 81 if (doEverything && packages.size() > 0) { 82 System.err.println("ERROR: -all used with specific package set"); 83 return; 84 } 85 86 if (!doEverything && packages.size() == 0) { 87 System.err.println("ERROR: no packages supplied and -all not used"); 88 return; 89 } 90 91 try { 92 ParcelFileDescriptor fd = ParcelFileDescriptor.dup(FileDescriptor.out); 93 String[] packArray = new String[packages.size()]; 94 bmgr.fullBackup(fd, saveApks, saveShared, doEverything, packages.toArray(packArray)); 95 } catch (IOException e) { 96 System.err.println("ERROR: cannot dup System.out"); 97 } catch (RemoteException e) { 98 System.err.println("ERROR: unable to invoke backup manager service"); 99 } 100 } 101 102 private String nextArg() { 103 if (mNextArg >= mArgs.length) { 104 return null; 105 } 106 String arg = mArgs[mNextArg]; 107 mNextArg++; 108 return arg; 109 } 110}