Backup.java revision a312c03fd830c489ffc81fad0812826b093b73ee
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.system.OsConstants; 24import android.util.Log; 25 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(OsConstants.STDOUT_FILENO); 57 } else if (arg.equals("restore")) { 58 doFullRestore(OsConstants.STDIN_FILENO); 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 saveObbs = false; 68 boolean saveShared = false; 69 boolean doEverything = false; 70 boolean doWidgets = false; 71 boolean allIncludesSystem = true; 72 73 String arg; 74 while ((arg = nextArg()) != null) { 75 if (arg.startsWith("-")) { 76 if ("-apk".equals(arg)) { 77 saveApks = true; 78 } else if ("-noapk".equals(arg)) { 79 saveApks = false; 80 } else if ("-obb".equals(arg)) { 81 saveObbs = true; 82 } else if ("-noobb".equals(arg)) { 83 saveObbs = false; 84 } else if ("-shared".equals(arg)) { 85 saveShared = true; 86 } else if ("-noshared".equals(arg)) { 87 saveShared = false; 88 } else if ("-system".equals(arg)) { 89 allIncludesSystem = true; 90 } else if ("-nosystem".equals(arg)) { 91 allIncludesSystem = false; 92 } else if ("-widgets".equals(arg)) { 93 doWidgets = true; 94 } else if ("-nowidgets".equals(arg)) { 95 doWidgets = false; 96 } else if ("-all".equals(arg)) { 97 doEverything = true; 98 } else { 99 Log.w(TAG, "Unknown backup flag " + arg); 100 continue; 101 } 102 } else { 103 // Not a flag; treat as a package name 104 packages.add(arg); 105 } 106 } 107 108 if (doEverything && packages.size() > 0) { 109 Log.w(TAG, "-all passed for backup along with specific package names"); 110 } 111 112 if (!doEverything && !saveShared && packages.size() == 0) { 113 Log.e(TAG, "no backup packages supplied and neither -shared nor -all given"); 114 return; 115 } 116 117 ParcelFileDescriptor fd = null; 118 try { 119 fd = ParcelFileDescriptor.adoptFd(socketFd); 120 String[] packArray = new String[packages.size()]; 121 mBackupManager.fullBackup(fd, saveApks, saveObbs, saveShared, doWidgets, 122 doEverything, allIncludesSystem, packages.toArray(packArray)); 123 } catch (RemoteException e) { 124 Log.e(TAG, "Unable to invoke backup manager for backup"); 125 } finally { 126 if (fd != null) { 127 try { 128 fd.close(); 129 } catch (IOException e) {} 130 } 131 } 132 } 133 134 private void doFullRestore(int socketFd) { 135 // No arguments to restore 136 ParcelFileDescriptor fd = null; 137 try { 138 fd = ParcelFileDescriptor.adoptFd(socketFd); 139 mBackupManager.fullRestore(fd); 140 } catch (RemoteException e) { 141 Log.e(TAG, "Unable to invoke backup manager for restore"); 142 } finally { 143 if (fd != null) { 144 try { 145 fd.close(); 146 } catch (IOException e) {} 147 } 148 } 149 } 150 151 private String nextArg() { 152 if (mNextArg >= mArgs.length) { 153 return null; 154 } 155 String arg = mArgs[mNextArg]; 156 mNextArg++; 157 return arg; 158 } 159}