20091124

Archive backup periodicly

I'm not into typing much about such simple subjects, but i just had to share this old batch code. Turns out there is no quick and neat solution available for moving your daily backups to separate folders. So in short and without further adieu;

Backups are important. All Staes IT web servers submit backups on a regular basis to secure offsite locations. These should run automated and reliable, without much human intervention.

Problem
These backup server receives backups via FTP each day. I don't want the next day's backup to overwrite the current ones, so have to archive them in a daily manner. It also looks nice when browsing the backups folder. Keeps everything nice and organized.

I should run a script every morning to archive those nightly backups to do the job for me.

Solution
This scripts puts all files a folder named "bu_YYYYMMDD_auto". Run this daily when the ftp backups are finished.

Since i've realized this script over 7 years ago people keep asking me how to do such a thing with batch files. So i figured i should just put it on the internet for all to see. Sorry for the delay, i only recently started this blog thing.

Back then i created two batch files to suit my needs at the time. One named "bu_shift.bat" checks if files exist with the correct extention, then creates a folder using the date, and moves all files in here. It outputs the results to the console. The other file named "bu_shift_keeplog.bat" just calls the above and puts the results in a daily logfile. Files are listed below.

It assumes the incomming files are at ./bu_ftp_up/*.gz and you could easily change that yourself.

Also keep in mind better solutions exist. This one will work out of the box and simply does its job - thats the only reason i present it here.

bu_shift.bat
@echo off

REM Run this file as a cronjob, some time after the FTP has delivered a backup.
REM All folders are appended with _auto to easily distinguish them from manual backups.

echo.
echo ****************************************************
echo * Start backup shift:
echo.
if not exist ".\bu_ftp_up\*.gz" GOTO NoFiles


for /F "tokens=2-4 delims=/- " %%A in ('date/T') do set var=%%C%%B%%A

echo * Creating folder bu_%var%_auto:
md "bu_%var%_auto"
echo.
echo * Moving files to folder:
move ".\bu_ftp_up\*.*" ".\bu_%var%_auto\"
echo.
GOTO End


:NoFiles
echo * Stopped: found no files in "bu_ftp_up".
echo.


:End
echo * End of batch
echo ****************************************************
echo.
Above neatly outputs results in the console.
When running this as a cronjob use below file to log this to aptly named files.

bu_shift_keeplog.bat
@echo off
for /F "tokens=2-4 delims=/- " %%A in ('date/T') do set logd=%%C%%B

set logfile=bu_shift_log_%logd%.txt

echo. >> %logfile%
echo bu_shift_keeplog.bat op %date%, om %time% >> %logfile%
.\bu_shift.bat >> %logfile%
echo. >> %logfile%
In Windows you can add this file as a cronjob via "Task Scheduler". This code has been in use for over five years on various Windows Server 2003 and 2008 environments, and continues to serve a purpose. Happy sharing!

PS - boy this Blogger editor is worth shit. Awfull code markup.

No comments:

Post a Comment