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.io.IOException;
26import java.util.ArrayList;
27
28public final class Backup {
29    static final String TAG = "bu";
30
31    static String[] mArgs;
32    int mNextArg;
33    IBackupManager mBackupManager;
34
35    public static void main(String[] args) {
36        Log.d(TAG, "Beginning: " + args[0]);
37        mArgs = args;
38        try {
39            new Backup().run();
40        } catch (Exception e) {
41            Log.e(TAG, "Error running backup/restore", e);
42        }
43        Log.d(TAG, "Finished.");
44    }
45
46    public void run() {
47        mBackupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
48        if (mBackupManager == null) {
49            Log.e(TAG, "Can't obtain Backup Manager binder");
50            return;
51        }
52
53        int socketFd = Integer.parseInt(nextArg());
54
55        String arg = nextArg();
56        if (arg.equals("backup")) {
57            doFullBackup(socketFd);
58        } else if (arg.equals("restore")) {
59            doFullRestore(socketFd);
60        } else {
61            Log.e(TAG, "Invalid operation '" + arg + "'");
62        }
63    }
64
65    private void doFullBackup(int socketFd) {
66        ArrayList<String> packages = new ArrayList<String>();
67        boolean saveApks = false;
68        boolean saveObbs = false;
69        boolean saveShared = false;
70        boolean doEverything = 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 ("-all".equals(arg)) {
93                    doEverything = true;
94                } else {
95                    Log.w(TAG, "Unknown backup flag " + arg);
96                    continue;
97                }
98            } else {
99                // Not a flag; treat as a package name
100                packages.add(arg);
101            }
102        }
103
104        if (doEverything && packages.size() > 0) {
105            Log.w(TAG, "-all passed for backup along with specific package names");
106        }
107
108        if (!doEverything && !saveShared && packages.size() == 0) {
109            Log.e(TAG, "no backup packages supplied and neither -shared nor -all given");
110            return;
111        }
112
113        ParcelFileDescriptor fd = null;
114        try {
115            fd = ParcelFileDescriptor.adoptFd(socketFd);
116            String[] packArray = new String[packages.size()];
117            mBackupManager.fullBackup(fd, saveApks, saveObbs, saveShared, doEverything,
118                    allIncludesSystem, packages.toArray(packArray));
119        } catch (RemoteException e) {
120            Log.e(TAG, "Unable to invoke backup manager for backup");
121        } finally {
122            if (fd != null) {
123                try {
124                    fd.close();
125                } catch (IOException e) {}
126            }
127        }
128    }
129
130    private void doFullRestore(int socketFd) {
131        // No arguments to restore
132        ParcelFileDescriptor fd = null;
133        try {
134            fd = ParcelFileDescriptor.adoptFd(socketFd);
135            mBackupManager.fullRestore(fd);
136        } catch (RemoteException e) {
137            Log.e(TAG, "Unable to invoke backup manager for restore");
138        } finally {
139            if (fd != null) {
140                try {
141                    fd.close();
142                } catch (IOException e) {}
143            }
144        }
145    }
146
147    private String nextArg() {
148        if (mNextArg >= mArgs.length) {
149            return null;
150        }
151        String arg = mArgs[mNextArg];
152        mNextArg++;
153        return arg;
154    }
155}