Backup.java revision 240c7d2d1fb2944ee6a6f1dee41c7bbd766f8f0d
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        boolean allIncludesSystem = true;
70
71        String arg;
72        while ((arg = nextArg()) != null) {
73            if (arg.startsWith("-")) {
74                if ("-apk".equals(arg)) {
75                    saveApks = true;
76                } else if ("-noapk".equals(arg)) {
77                    saveApks = false;
78                } else if ("-shared".equals(arg)) {
79                    saveShared = true;
80                } else if ("-noshared".equals(arg)) {
81                    saveShared = false;
82                } else if ("-system".equals(arg)) {
83                    allIncludesSystem = true;
84                } else if ("-nosystem".equals(arg)) {
85                    allIncludesSystem = false;
86                } else if ("-all".equals(arg)) {
87                    doEverything = true;
88                } else {
89                    Log.w(TAG, "Unknown backup flag " + arg);
90                    continue;
91                }
92            } else {
93                // Not a flag; treat as a package name
94                packages.add(arg);
95            }
96        }
97
98        if (doEverything && packages.size() > 0) {
99            Log.w(TAG, "-all passed for backup along with specific package names");
100        }
101
102        if (!doEverything && !saveShared && packages.size() == 0) {
103            Log.e(TAG, "no backup packages supplied and neither -shared nor -all given");
104            return;
105        }
106
107        try {
108            ParcelFileDescriptor fd = ParcelFileDescriptor.adoptFd(socketFd);
109            String[] packArray = new String[packages.size()];
110            mBackupManager.fullBackup(fd, saveApks, saveShared, doEverything, allIncludesSystem,
111                    packages.toArray(packArray));
112        } catch (RemoteException e) {
113            Log.e(TAG, "Unable to invoke backup manager for backup");
114        }
115    }
116
117    private void doFullRestore(int socketFd) {
118        // No arguments to restore
119        try {
120            ParcelFileDescriptor fd = ParcelFileDescriptor.adoptFd(socketFd);
121            mBackupManager.fullRestore(fd);
122        } catch (RemoteException e) {
123            Log.e(TAG, "Unable to invoke backup manager for restore");
124        }
125    }
126
127    private String nextArg() {
128        if (mNextArg >= mArgs.length) {
129            return null;
130        }
131        String arg = mArgs[mNextArg];
132        mNextArg++;
133        return arg;
134    }
135}