Backup.java revision e26e96bcc19b1cdac690d21b3986f09a502739e6
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;
694a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
704a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        String arg;
714a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        while ((arg = nextArg()) != null) {
724a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            if (arg.startsWith("-")) {
734a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                if ("-apk".equals(arg)) {
744a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                    saveApks = true;
754a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                } else if ("-noapk".equals(arg)) {
764a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                    saveApks = false;
774a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                } else if ("-shared".equals(arg)) {
784a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                    saveShared = true;
794a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                } else if ("-noshared".equals(arg)) {
804a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                    saveShared = false;
814a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                } else if ("-all".equals(arg)) {
824a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                    doEverything = true;
834a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                } else {
8414a2935809e73a9d824888dc837f2f017100fd26Christopher Tate                    Log.w(TAG, "Unknown backup flag " + arg);
8514a2935809e73a9d824888dc837f2f017100fd26Christopher Tate                    continue;
864a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                }
874a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            } else {
884a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                // Not a flag; treat as a package name
894a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                packages.add(arg);
904a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            }
914a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
924a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
934a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        if (doEverything && packages.size() > 0) {
9414a2935809e73a9d824888dc837f2f017100fd26Christopher Tate            Log.w(TAG, "-all passed for backup along with specific package names");
954a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
964a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
9714a2935809e73a9d824888dc837f2f017100fd26Christopher Tate        if (!doEverything && !saveShared && packages.size() == 0) {
9814a2935809e73a9d824888dc837f2f017100fd26Christopher Tate            Log.e(TAG, "no backup packages supplied and neither -shared nor -all given");
994a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            return;
1004a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
1014a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1024a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        try {
103e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate            ParcelFileDescriptor fd = ParcelFileDescriptor.adoptFd(socketFd);
1044a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            String[] packArray = new String[packages.size()];
10514a2935809e73a9d824888dc837f2f017100fd26Christopher Tate            mBackupManager.fullBackup(fd, saveApks, saveShared, doEverything,
10614a2935809e73a9d824888dc837f2f017100fd26Christopher Tate                    packages.toArray(packArray));
1074a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        } catch (RemoteException e) {
10875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            Log.e(TAG, "Unable to invoke backup manager for backup");
1094a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
1104a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    }
1114a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
112e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate    private void doFullRestore(int socketFd) {
11375a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        // No arguments to restore
11475a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        try {
115e26e96bcc19b1cdac690d21b3986f09a502739e6Christopher Tate            ParcelFileDescriptor fd = ParcelFileDescriptor.adoptFd(socketFd);
11675a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            mBackupManager.fullRestore(fd);
11775a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        } catch (RemoteException e) {
11875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            Log.e(TAG, "Unable to invoke backup manager for restore");
11975a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        }
12014a2935809e73a9d824888dc837f2f017100fd26Christopher Tate    }
12114a2935809e73a9d824888dc837f2f017100fd26Christopher Tate
1224a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    private String nextArg() {
1234a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        if (mNextArg >= mArgs.length) {
1244a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            return null;
1254a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
1264a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        String arg = mArgs[mNextArg];
1274a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        mNextArg++;
1284a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        return arg;
1294a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    }
1304a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate}