Batch Script for Transferring Files from Windows Machines To Linux File Server
January 13, 2014 10:09 pm Leave your thoughtsLately I had to come to come up with a way to transfer daily backup files from windows machines to a linux file server automatically. The idea is to keep a backup of the backup files on an external off site backup server. This is pretty much a standard practice, however the Windows machines where the backups came from were of different versions, therefore I had to find a universal (at least for windows) file transfer script. Therefore I settled with Windows batch file and a couple freeware applications.
It took me a bit of research and time to write the batch script, so I thought I’d share this with you all. Especially, some businesses with smaller IT implementation might find this useful.
Basic Concept
To put it simply, the batch script would transfer the nightly files via SCP automatically. The authentication method would be ssh key authentication. Also because the generated backup archives in these Windows machines can be very large, the batch script would also need to compress these files prior to transferring them. This is important in order to speed up the transfer and save cost on bandwidth.
Once the batch script is working properly, the automated task run will be done via windows scheduler.
Since there is no native support for rsa key generation and scp transfer in Windows as far as I know, there are a couple handy applications that needs to be installed. Do leave your comments if you know a good way to generate rsa key and perform scp natively, I normally use Linux therefore not extremely familiar with Windows.
The Applications
As mentioned previously, here are the applications that I needed in order to write the batch script as planned. These applications of course, would need to be able to run from the command line as we are writing a batch script after all.
- Putty
- This is used to generate the public and private keys on the Windows machine.
- 7zip
- Used to compress the backup filed.
- WinSCP
- Used to transfer file using SCP protocol via ssh. I have chosen this method as this is one of those most secure way for file transfer.
Once all the applications above are installed, make sure that you add their executables into the Windows machine’s environment variables so that it can be run from the command line without providing the full directory paths.
Setup Authentications
EDIT: Here’s a detailed guide on how to setup ssh authentications
I won’t go into too much details as this is pretty much a standard practice to setting up ssh access via ssh keys.
- Generate the public and private key using puttygen. Make sure to save both public key and private key in a directory accessible by the batch script.
- Log into the remote backup server using putty and put the Windows machine’s public key inside this authorized_keys file in the .ssh directory of the backup server’s account.
- Log out and log back in to the remote server using putty (with private key access setup), at this stage you wouldn’t be asked any password as your private key should be matching the public key in the backup server.
Batch Script Walkthrough
So from here on we start on the batch script codes walkthrough so that it make sense if you decide to use it.
Setup the Variables
1 2 3 4 |
set accessKey=c:\myprivatekey.ppk set backupDrive=e set backupDirectory=\DATABASE\Backup\ set uploadToRemoteServer="yes |
In this portion of the code we setup the path to the private key to use, make sure it’s the key that corresponds with the public key that we added earlier to the remote linux server.
We also setup the backup drive and directory in the Windows machine so we can use it later for the upload. In addition I have also added the config to turn off upload in case we just want to test compression and folder creation.
Move to the Current Backup Directory
1 2 3 4 |
REM Moving directory to the backup directory so we can easily upload later %backupDrive%: cd %backupDirectory% |
We just simply move to the current backup directory so that it’s easier to process the backup files without providing full paths.
Getting Today’s Date and Setting Up Archive Directory
1 2 3 4 5 6 7 8 9 10 11 12 |
REM Get today's date first for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined rawDate set rawDate=%%x set todayDate=%rawDate:~0,4%-%rawDate:~4,2%-%rawDate:~6,2% REM Uncomment this section if you want to archive as one huge file REM now that we have today's date we can archive our backup files REM Make directory for the backup mkdir %todayDate% for %%f in (*.bak) do ( 7z a -tzip "%todayDate%\%%~nf" "%%f" ) |
The code snippet above retrieved the date from the server’s time (thanks to this post in stackoverflow) using wmic. We then proceed to create a directory based on today’s date, which will serve as the directory where our archived backups are stored.
Lastly, we loop through all the files in the current backup directories and compress them using 7zip command. Note the %%~nf inside the loop, that is to keep the original filename of the backup file yet excluding the file extension.
Transferring the File to the Backup Server
1 2 3 4 5 6 7 8 9 10 |
REM This is actual all one command, however separated to separate lines for readability if %uploadToRemoteServer%=="yes" ( winscp.com /command ^ "option batch abort" ^ "option reconnecttime off" ^ "open scp://[email protected]:22 -privatekey=%accessKey% -rawsettings SendBuf=0 Compression=1 -timeout=999" ^ "cd /media/remote_backup"^ "put %todayDate% ./"^ "exit" ) |
This is the most important part, is of course transferring the actual files and this can be achieved using WinSCP that we installed previously. The above are the commands for the scp transfer using batch mode, which means the script will not stop for any prompts. Timeout has been set to really high too as the internet connection where I applied these scripts can sometimes stall for a brief moment.
Also note that in this command we also specify the path to the private key to use so that we are able to login to the remote server with no login prompts at all. This is the -privatekey= parameter.
Deleting the Original Files
1 2 3 4 |
REM lastly let's delete the bak files as we already transferred and archived them if %deleteBakFiles%=="yes" ( del *.bak ) |
The codes above will delete the original backup files inside the current directory. If you are not short on space, it’s possible to change this delete command into moving the current backup files into an archived directory.
The Full Codes
So here are the full codes for you to use. I hope that this can be useful to some of you needing a simple way to transfer between windows machine to linux backup server in a more secure manner.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
@echo off echo "Sync backup started" set deleteBakFiles="yes" set accessKey=c:\myprivatekey.ppk set backupDrive=e set backupDirectory=\DATABASE\Backup set uploadToRemoteServer="yes" REM Moving directory to the backup directory so we can easily upload later %backupDrive%: cd %backupDirectory% REM Using 7zip to archive our directory REM Get today's date first for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined rawDate set rawDate=%%x set todayDate=%rawDate:~0,4%-%rawDate:~4,2%-%rawDate:~6,2% REM Uncomment this section if you want to archive as one huge file REM now that we have today's date we can archive our backup files REM Make directory for the backup mkdir %todayDate% for %%f in (*.bak) do ( 7z a -tzip "%todayDate%\%%~nf" "%%f" ) REM This is actual all one command, however separated to separate lines for readability if %uploadToRemoteServer%=="yes" ( winscp.com /command ^ "option batch abort" ^ "option reconnecttime off" ^ "open scp://[email protected]:22 -privatekey=%accessKey% -rawsettings SendBuf=0 Compression=1 -timeout=999" ^ "cd /media/remote_backup"^ "put %todayDate% ./"^ "exit" ) REM lastly let's delete the bak files as we already transferred and archived them if %deleteBakFiles%=="yes" ( del *.bak ) REM now back to C again c: REM exit |
Tags: batch script, windows scripting
Categorised in: Business Process, Console commands