Daily Backup Script with Encryption and Compression for Windows

Daily backup is one of the most important task any IT person has to perform. It ensures business continuity in event of a disk crash as well as protect data in-case of cyber attack.

There are many many tools which you can use for backing up your data, however here we would use open source free tools and setup encrypted backup over ftp. This is what you need on your windows machine to start with

1) 7zip – http://www.7-zip.org

2) NcFTP – ftp://ftp.ncftp.com/ncftp/binaries/Setup%20NcFTP%203.2.5.msi

I am using windows 2008 and looking to backup my MSSQL database. First, I setup maintenance plan in MSSQL so that daily backup of database happens at a location on disk (for example D:\Backup).

MSSQL Database Backup

You can ignore above step if you just want to backup some files on the server and use that directory/files directly. Once you have got the backup, lets get the date first for our script. Following code will do just that

//With following code, we will get month as mm/date as dd/year as yy

FOR /f “tokens=2-4 skip=1 delims=(-)” %%G IN (‘echo.^|date’) DO (
FOR /f “tokens=2 delims= ” %%A IN (‘date /t’) DO (
SET v_first=%%G
SET v_second=%%H
SET v_third=%%I
SET v_all=%%A
)
)

SET %v_first%=%v_all:~0,2%
SET %v_second%=%v_all:~3,2%
SET %v_third%=%v_all:~6,4%

//delete the old compressed file

del /f /q D:\Backup.7z

// Compress the file with 7zip and encrypt with password PASSWORD

“C:\Program Files\7-Zip\7z.exe” a -t7z D:\Backup.7z D:\Backup -pPASSWORD -mhc=on -mhe=on

//Copy compressed file to new file name with “YYYY-MM-DD” field suffixed to it

copy D:\Backup.7z d:\Backup%yy%-%mm%-%dd%.7z /Y

//Use ncftp to upload dated file to FTPSERVER using FTPUSER and FTPPASSWORD

“C:\Windows\ncftpput.exe” -u FTPUSER -p FTPPASSWORD FTPSERVER / d:\Backup%yy%-%mm%-%dd%.7z

Whole script will look like this

 FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO (
 FOR /f "tokens=2 delims= " %%A IN ('date /t') DO (
 SET v_first=%%G
 SET v_second=%%H
 SET v_third=%%I
 SET v_all=%%A
 )
 )SET %v_first%=%v_all:~0,2%
 SET %v_second%=%v_all:~3,2%
 SET %v_third%=%v_all:~6,4%
del /f /q D:\Backup.7z
"C:\Program Files\7-Zip\7z.exe" a -t7z D:\Backup.7z D:\Backup -pPASSWORD -mhc=on -mhe=on
copy D:\Backup.7z d:\Backup%yy%-%mm%-%dd%.7z /Y
"C:\Windows\ncftpput.exe" -u FTPUSER -p FTPPASSWORD FTPSERVER / d:\Backup%yy%-%mm%-%dd%.7z

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
© 2014 Technology Blog. All rights reserved. Site Admin · Entries RSS · Comments RSS