Batch Script to SFTP Download an Entire Directory and Timestamp it Locally

This same script I’ve originally written a few days ago is morphing again for another purpose and does the following.

  1. Create a Logs Directory Locally and a Timestamped Sub Directory
  2. SFTP an entire remote directory tree to this Local Directory
@echo off 

REM ###################################################### 
REM #### BEGIN - Do Not Touch - Misc Setup Variables: #### 
REM ###################################################### 

REM SCRIPT_DIRECTORY is defined as the directory that this script currently exists in 
SET "SCRIPT_DIRECTORY=%~dp0" 
SET "SCRIPT_DIRECTORY=%SCRIPT_DIRECTORY:~0,-1%" 

for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set VARIABLE_DATETIME=%%a 
set VARIABLE_YEAR=%VARIABLE_DATETIME:~0,4% 
set VARIABLE_MONTH=%VARIABLE_DATETIME:~4,2% 
set VARIABLE_DAY=%VARIABLE_DATETIME:~6,2% 
set VARIABLE_HOUR=%VARIABLE_DATETIME:~8,2% 
set VARIABLE_MINUTE=%VARIABLE_DATETIME:~10,2% 
set VARIABLE_SECOND=%VARIABLE_DATETIME:~12,2% 
set VARIABLE_TIMESTAMP=%VARIABLE_YEAR%-%VARIABLE_MONTH%-%VARIABLE_DAY% 

REM #################################################### 
REM #### END - Do Not Touch - Misc Setup Variables: #### 
REM #################################################### 

REM ####################################################### 
REM ### BEGIN - Customized Remote/Local Setup Variables ### 
REM ####################################################### 

REM PLINK and PSFTP Application Paths 
SET "PLINK=C:/Program Files/Putty/plink.exe" 
SET "PSFTP=C:/Program Files/Putty/psftp.exe" 

REM Setup - Remote Linux Host, Username, and Potential Command to Run 
SET "REMOTE_HOSTNAME=hostname.com" 
SET "REMOTE_USERNAME=username" 

REM Setup - Private Key for Authentication 
SET "LOCAL_PRIVATE_KEY=%SCRIPT_DIRECTORY%\private_key.ppk" 

REM Setup - Local File To Transfer - Local Filename Variables including Name, Path, and Timestamp 
SET "LOCAL_DIRECTORY=%SCRIPT_DIRECTORY%\Logs" 
SET "LOCAL_DIRECTORY_TIMESTAMPED=%LOCAL_DIRECTORY%\%VARIABLE_TIMESTAMP%" 

REM Setup - Final Transfer Path to Remote Server - Remote Directory and Final Filename to Transmit 
SET "REMOTE_DIRECTORY=/remote/path/to/logs" 
 
REM ##################################################### 
REM ### END - Customized Remote/Local Setup Variables ### 
REM ##################################################### 

REM ##################################################### 
REM ### BEGIN - Script Actions Using Setup Variables  ### 
REM ##################################################### 

mkdir "%LOCAL_DIRECTORY%" 
mkdir "%LOCAL_DIRECTORY_TIMESTAMPED%" 

REM Change to Timestamp Directory 
cd /d "%LOCAL_DIRECTORY_TIMESTAMPED%" 

REM SFTP - Stage SFTP Commands in Temp Script 
SET "TEMP_BATCH_FILE=%SCRIPT_DIRECTORY%\psftp.batch"    
echo cd %REMOTE_DIRECTORY% > "%TEMP_BATCH_FILE%" 
echo get -r %REMOTE_DIRECTORY% >> "%TEMP_BATCH_FILE%" 

REM SFTP - Execute Transfer 
"%PSFTP%" -i "%LOCAL_PRIVATE_KEY%" ^ 
         -b "%TEMP_BATCH_FILE%" ^ 
         "%REMOTE_USERNAME%@%REMOTE_HOSTNAME%" 

REM SFTP - Remove Temp Script 
del /f "%TEMP_BATCH_FILE%" 

REM ################################################### 
REM ### END - Script Actions Using Setup Variables  ### 
REM ################################################### 

pause 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s