Using windows shell script and adb to backup stuff from your Android phone

Here's some tips on how to write a shell script to backup stuff from your Android phone to a Windows machine. This should work on any version of Windows, but I've only tested it on Windows 7 and Windows 10.

First, you need the Android SDK installed. The command line tools are enough, you don't need the full Android Studio. You will be using the adb.exe found within to do the transfers and removing of files on the phone.

Second, you need an ADB (Android Debug Bridge) driver for your phone installed. Most phone manufacturers provide ADB drivers on their websites. Note: Seems that the ADB bridge is nowadays supported in Windows 10 and recent mobile phones without any extra drivers. I haven't had to install ADB drivers at all recently.

Third, your Android phone needs to be in developer access mode.

You should do a couple modifications to the script below. These include setting the ADB path variable and setting the destination folders for sound recordings and images.

You can also uncomment a couple lines to make the script delete files from your phone. But note that this is quite dangerous, since if the adb transfer fails for some reason, you might end up just deleting your files without transfering them. The script should pause after a transfer error (at the if %errorlevel% lines) and will continue if you press enter. So be careful if you decide to uncomment those lines!

Below is the example script. Simply paste it into notepad, edit the parts as described in the comments and save it as a .bat file, for example backupscript.bat. Have fun backing stuff up!

@ECHO off

:: SET THIS VARIABLE TO POINT TO THE ADB.EXE
set adbpath="C:\Development\Android SDK\platform-tools\adb"
echo ADB path is %adbpath%

:: SCRIPT FOR GETTING DATE STRING IN FORM YYYY-MM-DD
:: Source: stackoverflow.com
:: Works on any NT/2k machine independent of regional date settings
:: 20110103 - adapted by jeroen@pluimers.com for Dutch locale
set v_day=
set v_month=
set v_year=
SETLOCAL ENABLEEXTENSIONS
if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
  for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
    if "%%a" GEQ "A" (
      for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
        set '%%a'=%%i
        set '%%b'=%%j
        set 'yy'=%%k
      )
    )
  )
if %'yy'% LSS 100 set 'yy'=20%'yy'%
  set Today=%'yy'%-%'mm'%-%'dd'%
ENDLOCAL & SET v_year=%'yy'%& SET v_month=%'mm'%& SET v_day=%'dd'%

set datestring=%V_Year%-%V_Month%-%V_Day%
echo Date-time string is %datestring%

:: TRANSFERS BEGIN

:: RECORDINGS FROM SOUND RECORDER

:: CREATE THE DESTINATION FOLDER...
:: Change this to whatever folder you want the files transfered to
set dest=C:\MyRecordings\%datestring%
echo Creating folder %dest%
mkdir %dest%

set source=/storage/sdcard1/Recording/

echo Copying from sdcard path %source% to %dest%
%adbpath% pull "%source%" %dest%
if %errorlevel% neq 0 pause

:: Uncomment these lines to delete files from your phone after transfer.
::echo Deleting all from sdcard path %source%
::%adbpath% shell rm '%source%'*

:: CAMERA IMAGES

:: CREATE THE DESTINATION FOLDER...
:: Change this to whatever folder you want the files transfered to
set dest=c:\MyPictures\%datestring%
echo Creating folder %dest%
mkdir %dest%


set source=/storage/sdcard1/DCIM/Camera/

echo Copying from sdcard path %source% to %dest%
%adbpath% pull "%source%" %dest%
if %errorlevel% neq 0 pause

:: Uncomment these lines to delete files from your phone after transfer.
::echo Deleting all from sdcard path %source%
::%adbpath% shell rm '%source%'*

:: WHATSAPP SENT IMAGES

:: We use the same destination folder as in the previous step.
set source=/sdcard/Whatsapp/Media/WhatsApp Images/Sent/

echo Copying from sdcard path %source% to %dest%
%adbpath% pull "%source%" %dest%
if %errorlevel% neq 0 pause

:: Uncomment these lines to delete files from your phone after transfer.
::echo Deleting all from sdcard path %source%
::%adbpath% shell rm '%source%'*


:: WHATSAPP RECEIVED IMAGES

:: We use the same destination folder as in the previous steps.
set source=/sdcard/Whatsapp/Media/WhatsApp Images/

echo Copying from sdcard path %source% to %dest%
%adbpath% pull "%source%" %dest%
if %errorlevel% neq 0 pause

:: Uncomment these lines to delete files from your phone after transfer.
::echo Deleting all from sdcard path %source%
::%adbpath% shell rm '%source%'*

:: PRESS ANY KEY TO CONTINUE
pause

Comments

Popular posts from this blog

How to fix the weird audio bug in Kingdom Come: Deliverance

How to install and play Curse of Monkey Island on Android

How to get rid of coil whine just by tweaking BIOS

Minecraft recv failed error... fixed!

Where does uTorrent keep its .torrent files?

Stop the CxUiUSvc service from eating your RAM