14a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate/* 24a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * Copyright (C) 2011 The Android Open Source Project 34a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * 44a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * Licensed under the Apache License, Version 2.0 (the "License"); 54a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * you may not use this file except in compliance with the License. 64a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * You may obtain a copy of the License at 74a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * 84a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * http://www.apache.org/licenses/LICENSE-2.0 94a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * 104a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * Unless required by applicable law or agreed to in writing, software 114a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * distributed under the License is distributed on an "AS IS" BASIS, 124a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * See the License for the specific language governing permissions and 144a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * limitations under the License. 154a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate */ 164a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 174a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tatepackage com.android.commands.bu; 184a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 194a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.app.backup.IBackupManager; 204a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.os.ParcelFileDescriptor; 214a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.os.RemoteException; 224a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.os.ServiceManager; 234a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.util.Log; 244a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 254a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport java.util.ArrayList; 264a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 274a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tatepublic final class Backup { 2814a2935809e73a9d824888dc837f2f017100fd26Christopher Tate static final String TAG = "bu"; 294a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 3014a2935809e73a9d824888dc837f2f017100fd26Christopher Tate static String[] mArgs; 3114a2935809e73a9d824888dc837f2f017100fd26Christopher Tate int mNextArg; 3214a2935809e73a9d824888dc837f2f017100fd26Christopher Tate IBackupManager mBackupManager; 334a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 344a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate public static void main(String[] args) { 3575a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate Log.d(TAG, "Beginning: " + args[0]); 364a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate mArgs = args; 374a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate try { 384a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate new Backup().run(); 394a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } catch (Exception e) { 4075a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate Log.e(TAG, "Error running backup/restore", e); 414a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 424a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate Log.d(TAG, "Finished."); 434a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 444a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 454a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate public void run() { 4614a2935809e73a9d824888dc837f2f017100fd26Christopher Tate mBackupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); 4714a2935809e73a9d824888dc837f2f017100fd26Christopher Tate if (mBackupManager == null) { 4875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate Log.e(TAG, "Can't obtain Backup Manager binder"); 494a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return; 504a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 514a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 52e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate int socketFd = Integer.parseInt(nextArg()); 53e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate 5414a2935809e73a9d824888dc837f2f017100fd26Christopher Tate String arg = nextArg(); 5514a2935809e73a9d824888dc837f2f017100fd26Christopher Tate if (arg.equals("backup")) { 56e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate doFullBackup(socketFd); 5714a2935809e73a9d824888dc837f2f017100fd26Christopher Tate } else if (arg.equals("restore")) { 58e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate doFullRestore(socketFd); 5914a2935809e73a9d824888dc837f2f017100fd26Christopher Tate } else { 6075a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate Log.e(TAG, "Invalid operation '" + arg + "'"); 6114a2935809e73a9d824888dc837f2f017100fd26Christopher Tate } 6214a2935809e73a9d824888dc837f2f017100fd26Christopher Tate } 6314a2935809e73a9d824888dc837f2f017100fd26Christopher Tate 64e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate private void doFullBackup(int socketFd) { 654a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate ArrayList<String> packages = new ArrayList<String>(); 664a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate boolean saveApks = false; 674a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate boolean saveShared = false; 684a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate boolean doEverything = false; 69240c7d2d1fb2944ee6a6f1dee41c7bbd766f8f0dChristopher Tate boolean allIncludesSystem = true; 704a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 714a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate String arg; 724a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate while ((arg = nextArg()) != null) { 734a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if (arg.startsWith("-")) { 744a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if ("-apk".equals(arg)) { 754a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate saveApks = true; 764a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else if ("-noapk".equals(arg)) { 774a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate saveApks = false; 784a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else if ("-shared".equals(arg)) { 794a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate saveShared = true; 804a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else if ("-noshared".equals(arg)) { 814a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate saveShared = false; 82240c7d2d1fb2944ee6a6f1dee41c7bbd766f8f0dChristopher Tate } else if ("-system".equals(arg)) { 83240c7d2d1fb2944ee6a6f1dee41c7bbd766f8f0dChristopher Tate allIncludesSystem = true; 84240c7d2d1fb2944ee6a6f1dee41c7bbd766f8f0dChristopher Tate } else if ("-nosystem".equals(arg)) { 85240c7d2d1fb2944ee6a6f1dee41c7bbd766f8f0dChristopher Tate allIncludesSystem = false; 864a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else if ("-all".equals(arg)) { 874a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate doEverything = true; 884a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else { 8914a2935809e73a9d824888dc837f2f017100fd26Christopher Tate Log.w(TAG, "Unknown backup flag " + arg); 9014a2935809e73a9d824888dc837f2f017100fd26Christopher Tate continue; 914a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 924a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else { 934a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate // Not a flag; treat as a package name 944a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate packages.add(arg); 954a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 964a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 974a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 984a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if (doEverything && packages.size() > 0) { 9914a2935809e73a9d824888dc837f2f017100fd26Christopher Tate Log.w(TAG, "-all passed for backup along with specific package names"); 1004a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1014a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 10214a2935809e73a9d824888dc837f2f017100fd26Christopher Tate if (!doEverything && !saveShared && packages.size() == 0) { 10314a2935809e73a9d824888dc837f2f017100fd26Christopher Tate Log.e(TAG, "no backup packages supplied and neither -shared nor -all given"); 1044a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return; 1054a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1064a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 1074a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate try { 108e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate ParcelFileDescriptor fd = ParcelFileDescriptor.adoptFd(socketFd); 1094a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate String[] packArray = new String[packages.size()]; 110240c7d2d1fb2944ee6a6f1dee41c7bbd766f8f0dChristopher Tate mBackupManager.fullBackup(fd, saveApks, saveShared, doEverything, allIncludesSystem, 11114a2935809e73a9d824888dc837f2f017100fd26Christopher Tate packages.toArray(packArray)); 1124a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } catch (RemoteException e) { 11375a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate Log.e(TAG, "Unable to invoke backup manager for backup"); 1144a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1154a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1164a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 117e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate private void doFullRestore(int socketFd) { 11875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate // No arguments to restore 11975a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate try { 120e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate ParcelFileDescriptor fd = ParcelFileDescriptor.adoptFd(socketFd); 12175a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate mBackupManager.fullRestore(fd); 12275a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate } catch (RemoteException e) { 12375a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate Log.e(TAG, "Unable to invoke backup manager for restore"); 12475a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate } 12514a2935809e73a9d824888dc837f2f017100fd26Christopher Tate } 12614a2935809e73a9d824888dc837f2f017100fd26Christopher Tate 1274a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate private String nextArg() { 1284a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if (mNextArg >= mArgs.length) { 1294a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return null; 1304a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1314a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate String arg = mArgs[mNextArg]; 1324a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate mNextArg++; 1334a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return arg; 1344a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1354a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate}