Backup.java revision 75a99709accef8cf221fd436d646727e7c8dd1f1
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 = "bu"; 31 32 static String[] mArgs; 33 int mNextArg; 34 IBackupManager mBackupManager; 35 36 public static void main(String[] args) { 37 Log.d(TAG, "Beginning: " + args[0]); 38 mArgs = args; 39 try { 40 new Backup().run(); 41 } catch (Exception e) { 42 Log.e(TAG, "Error running backup/restore", e); 43 } 44 Log.d(TAG, "Finished."); 45 } 46 47 public void run() { 48 mBackupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); 49 if (mBackupManager == null) { 50 Log.e(TAG, "Can't obtain Backup Manager binder"); 51 return; 52 } 53 54 String arg = nextArg(); 55 if (arg.equals("backup")) { 56 doFullBackup(); 57 } else if (arg.equals("restore")) { 58 doFullRestore(); 59 } else { 60 Log.e(TAG, "Invalid operation '" + arg + "'"); 61 } 62 } 63 64 private void doFullBackup() { 65 ArrayList<String> packages = new ArrayList<String>(); 66 boolean saveApks = false; 67 boolean saveShared = false; 68 boolean doEverything = false; 69 70 String arg; 71 while ((arg = nextArg()) != null) { 72 if (arg.startsWith("-")) { 73 if ("-apk".equals(arg)) { 74 saveApks = true; 75 } else if ("-noapk".equals(arg)) { 76 saveApks = false; 77 } else if ("-shared".equals(arg)) { 78 saveShared = true; 79 } else if ("-noshared".equals(arg)) { 80 saveShared = false; 81 } else if ("-all".equals(arg)) { 82 doEverything = true; 83 } else { 84 Log.w(TAG, "Unknown backup flag " + arg); 85 continue; 86 } 87 } else { 88 // Not a flag; treat as a package name 89 packages.add(arg); 90 } 91 } 92 93 if (doEverything && packages.size() > 0) { 94 Log.w(TAG, "-all passed for backup along with specific package names"); 95 } 96 97 if (!doEverything && !saveShared && packages.size() == 0) { 98 Log.e(TAG, "no backup packages supplied and neither -shared nor -all given"); 99 return; 100 } 101 102 try { 103 ParcelFileDescriptor fd = ParcelFileDescriptor.dup(FileDescriptor.out); 104 String[] packArray = new String[packages.size()]; 105 mBackupManager.fullBackup(fd, saveApks, saveShared, doEverything, 106 packages.toArray(packArray)); 107 } catch (IOException e) { 108 Log.e(TAG, "Can't dup out"); 109 } catch (RemoteException e) { 110 Log.e(TAG, "Unable to invoke backup manager for backup"); 111 } 112 } 113 114 private void doFullRestore() { 115 // No arguments to restore 116 try { 117 ParcelFileDescriptor fd = ParcelFileDescriptor.dup(FileDescriptor.in); 118 mBackupManager.fullRestore(fd); 119 } catch (IOException e) { 120 Log.e(TAG, "Can't dup System.in"); 121 } catch (RemoteException e) { 122 Log.e(TAG, "Unable to invoke backup manager for restore"); 123 } 124 } 125 126 private String nextArg() { 127 if (mNextArg >= mArgs.length) { 128 return null; 129 } 130 String arg = mArgs[mNextArg]; 131 mNextArg++; 132 return arg; 133 } 134}