Backup.java revision e26e96bcc19b1cdac690d21b3986f09a502739e6
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.util.ArrayList; 26 27public final class Backup { 28 static final String TAG = "bu"; 29 30 static String[] mArgs; 31 int mNextArg; 32 IBackupManager mBackupManager; 33 34 public static void main(String[] args) { 35 Log.d(TAG, "Beginning: " + args[0]); 36 mArgs = args; 37 try { 38 new Backup().run(); 39 } catch (Exception e) { 40 Log.e(TAG, "Error running backup/restore", e); 41 } 42 Log.d(TAG, "Finished."); 43 } 44 45 public void run() { 46 mBackupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); 47 if (mBackupManager == null) { 48 Log.e(TAG, "Can't obtain Backup Manager binder"); 49 return; 50 } 51 52 int socketFd = Integer.parseInt(nextArg()); 53 54 String arg = nextArg(); 55 if (arg.equals("backup")) { 56 doFullBackup(socketFd); 57 } else if (arg.equals("restore")) { 58 doFullRestore(socketFd); 59 } else { 60 Log.e(TAG, "Invalid operation '" + arg + "'"); 61 } 62 } 63 64 private void doFullBackup(int socketFd) { 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.adoptFd(socketFd); 104 String[] packArray = new String[packages.size()]; 105 mBackupManager.fullBackup(fd, saveApks, saveShared, doEverything, 106 packages.toArray(packArray)); 107 } catch (RemoteException e) { 108 Log.e(TAG, "Unable to invoke backup manager for backup"); 109 } 110 } 111 112 private void doFullRestore(int socketFd) { 113 // No arguments to restore 114 try { 115 ParcelFileDescriptor fd = ParcelFileDescriptor.adoptFd(socketFd); 116 mBackupManager.fullRestore(fd); 117 } catch (RemoteException e) { 118 Log.e(TAG, "Unable to invoke backup manager for restore"); 119 } 120 } 121 122 private String nextArg() { 123 if (mNextArg >= mArgs.length) { 124 return null; 125 } 126 String arg = mArgs[mNextArg]; 127 mNextArg++; 128 return arg; 129 } 130}