Friday, 25 January 2013

Compress Sage backups by date to save disk space

What:

Windows Server 2003 with Sage

Problem:

Sage creates massive number of backup files, they are uncompressed and take a lot of space.

Solution:

Create a script, first to compress files, then put all files into another archive with a given date and add as a scheduled task.

Example file:
SAGE_backup_201301212130.bak - size 116MB
SAGE_backup_201301212130.bak.zip - size 14MB

 :: created by Plazmus

D:
cd "SAGE\SQLBackups\Daily"

:: First compress all raw files one by one, if compression is successful delete raw file

for /f "delims=" %%G in ('dir /a-d /B /OD *.bak') do "C:\Program Files\7-Zip\7z.exe" a -tzip "%%G.zip" "%%G" && del "%%G"

:: Now compress all zip's into one file
:: loop to compress all .bak.zip files in a directory by date

:start
:: find and extract date from file name and set it as variable
FOR /f "tokens=1 delims=." %%a in ('dir *bak.zip /aa-d /b /O-D') do set dd=%%a
set data=%dd:~-12%

if %data%==~-12 exit

:: make list of files to be zipped
dir *backup_%data%*.bak.zip /aa-d /b /OD > compressme.txt

:: if there is nothing in compressme.txt exit
for %%i in (compressme.txt) do (
 if %%~zi GTR 0 goto compress else
 exit
)

:compress

"C:\Program Files\7-Zip\7z.exe" a -tzip Backup_%data%.zip @compressme.txt && for /f "delims=" %%i in (compressme.txt) do del "%%i"

goto start

No comments:

Post a Comment