Backup.java revision 4a627c71ff53a4fca1f961f4b1dcc0461df18a06
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.io.FileDescriptor; 264a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport java.io.IOException; 274a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport java.util.ArrayList; 284a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 294a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tatepublic final class Backup { 304a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate static final String TAG = "Backup"; 314a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 324a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate private static String[] mArgs; 334a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate private int mNextArg; 344a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 354a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate public static void main(String[] args) { 364a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate mArgs = args; 374a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate try { 384a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate new Backup().run(); 394a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } catch (Exception e) { 404a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate Log.e(TAG, "Error running backup", e); 414a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 424a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate Log.d(TAG, "Finished."); 434a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 444a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 454a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate public void run() { 464a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate IBackupManager bmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); 474a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if (bmgr == null) { 484a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate System.err.println("ERROR: could not contact backup manager"); 494a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return; 504a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 514a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 524a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate ArrayList<String> packages = new ArrayList<String>(); 534a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate boolean saveApks = false; 544a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate boolean saveShared = false; 554a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate boolean doEverything = false; 564a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 574a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate String arg; 584a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate while ((arg = nextArg()) != null) { 594a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 604a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if (arg.startsWith("-")) { 614a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if ("-apk".equals(arg)) { 624a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate saveApks = true; 634a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else if ("-noapk".equals(arg)) { 644a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate saveApks = false; 654a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else if ("-shared".equals(arg)) { 664a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate saveShared = true; 674a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else if ("-noshared".equals(arg)) { 684a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate saveShared = false; 694a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else if ("-all".equals(arg)) { 704a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate doEverything = true; 714a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else { 724a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate System.err.println("ERROR: unknown flag " + arg); 734a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return; 744a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 754a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } else { 764a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate // Not a flag; treat as a package name 774a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate packages.add(arg); 784a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 794a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 804a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 814a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if (doEverything && packages.size() > 0) { 824a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate System.err.println("ERROR: -all used with specific package set"); 834a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return; 844a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 854a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 864a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if (!doEverything && packages.size() == 0) { 874a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate System.err.println("ERROR: no packages supplied and -all not used"); 884a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return; 894a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 904a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 914a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate try { 924a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate ParcelFileDescriptor fd = ParcelFileDescriptor.dup(FileDescriptor.out); 934a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate String[] packArray = new String[packages.size()]; 944a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate bmgr.fullBackup(fd, saveApks, saveShared, doEverything, packages.toArray(packArray)); 954a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } catch (IOException e) { 964a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate System.err.println("ERROR: cannot dup System.out"); 974a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } catch (RemoteException e) { 984a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate System.err.println("ERROR: unable to invoke backup manager service"); 994a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1004a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1014a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate 1024a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate private String nextArg() { 1034a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate if (mNextArg >= mArgs.length) { 1044a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return null; 1054a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1064a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate String arg = mArgs[mNextArg]; 1074a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate mNextArg++; 1084a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate return arg; 1094a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate } 1104a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate}