Skip to content

Instantly share code, notes, and snippets.

@DMzda
Created August 15, 2010 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DMzda/456e41738a880678b766 to your computer and use it in GitHub Desktop.
Save DMzda/456e41738a880678b766 to your computer and use it in GitHub Desktop.
Backup Tool to backup and restore apps and data
@ECHO off
ECHO --------------------------------------
ECHO -- --
ECHO -- Backup/Restore your apps --
ECHO -- by DMzda --
ECHO -- --
ECHO --------------------------------------
ECHO.
ECHO.
:: Thanks AColWill, daentech and Tjh for helping and testing
::Sets path to 7-zip. Replace with your own path.
SET zippath=%PROGRAMFILES%\7-zip
:phonepluggedin
::Restarts adb, to prevent errors
adb kill-server
adb start-server
::Detect if phone is plugged in.
@adb devices|find "device"|find /v "List"
ECHO.
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO Phone not plugged in.
ECHO Please plug in your phone
GOTO end
)
::Validate the argument
IF "%1"=="" GOTO Usage
If /I %1==backup GOTO Backup
If /I %1==restore GOTO Restore
ECHO.
ECHO Invalid argument
GOTO Usage
:Usage
ECHO Usage:
ECHO To backup:
ECHO %~n0 backup
ECHO Then enter a valid path to place your backup. Do not rename these backups, as this will probably break the script.
ECHO.
ECHO To restore:
ECHO %~n0 restore
ECHO Then enter a valid path to a backup.zip taken using "%~n0 backup".
ECHO.
GOTO end
::-----------------------------BACKUP------------------------------------
:Backup
SET /P folder=Type a path for the backup to go:
::Removes quotes from the folder variable if present
IF %folder:~0,1%%folder:~-1%=="" (
SET folder=%folder:~0,-1%
SET folder=%folder:~1%
)
::Removes the trailing \ if present
IF %folder:~-1%==\ (
SET folder=%folder:~0,-1%
)
::Prevents the root of the drive being used, as that breaks the restore part
IF %folder:~-1%==: (
ECHO.
ECHO Root of drives not supported. Please specify a different folder.
ECHO.
GOTO end
)
:backuppathcheck
::Check if the folder is present
IF NOT EXIST "%folder%" (
ECHO.
ECHO Invalid path
ECHO.
GOTO end
)
SET /P bupOptions=Do you want to backup [a]pps, [d]ata or [b]oth:
IF /I %bupOptions%==a MKDIR "%folder%\data\app\"
IF /I %bupOptions%==d MKDIR "%folder%\data\data\"
IF /I %bupOptions%==b MKDIR "%folder%\data\app\"
IF /I %bupOptions%==b MKDIR "%folder%\data\data\"
IF /I %bupOptions%==d GOTO databup
:appbup
ECHO.
ECHO Backing up Apps...
ECHO.
adb pull /data/app/ "%folder%\data\app"
IF /I %bupOptions%==a GOTO zipbup
:databup
ECHO.
ECHO Backing up app data...
ECHO.
adb pull /data/data/ "%folder%\data\data"
::Does not work properly, deleted from .zip below
::FOR %%D IN ('DIR "%folder%\data\data\com.android.*" /B ') DO (RD /S /Q "%folder%\data\data\%%D")
::FOR %%E IN ('DIR "%folder%\data\data\com.google.android.*" /B ') DO (RD /S /Q "%folder%\data\data\%%E")
::RD /S /Q %folder%\data\data\com.android*
::RD /S /Q %folder%\data\data\com.google.android*
:zipbup
ECHO.
ECHO Zipping up backup...
ECHO.
::Zip up the backup
::The %time% variable adds a space before the hour if it is before 10am. The FOR gets around that.
SET Goodtime=%time%
IF "%Goodtime:~0,1%"==" " (
SET Goodtime=0%Goodtime:~1,10%
)
SET Backupzip=Backup-%date:~-4,4%%date:~-7,2%%date:~-10,2%-%Goodtime:~0,2%%Goodtime:~3,2%.zip
"%zippath%\7z" a -r "%folder%\%Backupzip%" "%folder%\data\*"
ECHO.
ECHO Working...
ECHO.
::Actually, it is deleting system data. Muahahahaha
"%zippath%\7z" d "%folder%\%Backupzip%" /data/com.android.* -r
"%zippath%\7z" d "%folder%\%Backupzip%" /data/com.google.android.* -r
"%zippath%\7z" d "%folder%\%Backupzip%" /data/com.fede.launcher -r
ECHO.
ECHO Cleaning up...
ECHO.
::Deletes the pulled files
RD /S /Q "%folder%\data"
ECHO.
ECHO Backup Complete!
ECHO.
GOTO end
::-----------------------------RESTORE------------------------------------
:Restore
SET /P backup=Type the path to the Backup.zip:
::Removes quotes from the backup variable if present
IF %backup:~0,1%%backup:~-1%=="" (
SET backup=%backup:~0,-1%
SET backup=%backup:~1%
)
::Removes the trailing \ if present
IF %backup:~-1%==\ (
SET backup=%backup:~0,-1%
)
::Prevents the root of the drive being used, as that breaks the restore
IF %backup:~-1%==: (
ECHO.
ECHO Root of drives not supported. Please specify the Backup.zip
ECHO.
GOTO end
)
:restorePathcheck
::Checks if the path exists
IF NOT EXIST "%backup%" (
ECHO.
ECHO Invalid path
ECHO.
GOTO end
)
::Get the name of the backup.zip, if selected.
FOR %%A in ("%backup%") do (
IF "%%~nxA"=="" (
SET backupname=notbackup.zip
ECHO.
ECHO Backup.zip not specified
ECHO.
DIR "%backup%\backup*.zip" /B
ECHO.
ECHO Use one of the above files
ECHO.
GOTO end
)
ECHO.
ECHO You selected: %%~nxA
ECHO.
SET backupname=%%~nxA
)
::If a different file is specified, it knows. IT KNOWS EVERYTHING
IF /I %backupname:~0,6%%backupname:~-4% NEQ Backup.zip (
ECHO.
ECHO Backup.zip not specified
ECHO.
DIR "%backup%\backup*.zip" /B
ECHO.
ECHO Use one of the above files
ECHO.
GOTO end
)
::Makes a folder with the backup.zip's name, without the .zip part. See, its very clever.
SET folder=%backup:~0,-4%
MKDIR "%folder%"
:restoreZip
ECHO.
ECHO Unzipping backup...
ECHO.
"%zippath%\7z" x -y "%backup%" -o"%folder%"
ECHO.
ECHO Remounting...
ECHO.
adb remount
::If no apps were backed up, it knows. And it skips restoring them
IF NOT EXIST "%folder%\app" (
ECHO.
ECHO No apps found
ECHO.
GOTO restoreData
)
ECHO.
ECHO Restoring Apps...
ECHO.
::Oh look, another very clever part.
::adb is very dumb, can't do "adb install %folder%\app\*.apk"
::Hence this cool workaround. Record current dir, cd to the app folder, install apps, cd back to the previous dir.
::The user won't even know
SET directory=%cd%
cd "%folder%\app\"
FOR /F "tokens=*" %%B IN ('DIR *.* /B ') DO (
adb install %%B
)
cd "%directory%"
:restoreData
::Checks if data is there
IF NOT EXIST "%folder%\data" (
ECHO.
ECHO No App data found
ECHO.
GOTO restoreFix
)
ECHO.
ECHO Restoring app data...
ECHO.
::A simple push
adb push "%folder%\data" /data/data/
:restoreFix
ECHO.
ECHO Fixing permissions...
ECHO.
::Fix permissions of the data that was just restored. Make sure that this script is present in your ROM
adb shell fix_permissions
ECHO.
ECHO Rebooting phone...
ECHO.
adb reboot
ECHO.
ECHO Cleaning up...
ECHO.
::Delete the mess left behind
RD /S /Q "%folder%"
ECHO.
ECHO Restore Complete!
ECHO.
GOTO end
:end
ECHO.
ECHO Thanks for using this script
ECHO.
@DMzda
Copy link
Author

DMzda commented Aug 16, 2010

Replace the "SET zippath="C:\Program Files\7-zip"" line with the path to your 7zip installation.

Usage:
To backup:
backuptool backup
Then enter a valid path to place your backup. Do not rename these backups, as this will probably break the script.

To restore:
backuptool restore
Then enter a valid path to a backup.zip taken using "backuptool backup".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment