Hello All,

I have been working on getting something put together so that we can back up the KACE system to our backup server.

I know there is a How-to for using the windows CLI FTP client but I was not having much luck with it.

I have put together a BAT file that can be called from the Windows task scheduler to pull backups every day to a location of you pick.  You can choose how long to keep the daily backups and how long to keep the monthly backups.

Remember to fill out all the variables at the top of the script for your environment.

Just thought I would share.  Any feedback would be great!

I just use the portable edition of WinSCP located here: http://winscp.net/eng/download.php

Code:

@ECHO OFF
REM Created by Marc Johnson April 5th 2014
REM Last edit April 6th 2014
REM Some code taken from http://community.spiceworks.com/scripts/show/2050-download-files-from-remote-server-via-winscp-shell

REM Set log file location and name
SET log=C:\Data_Bin\K1000_Backup\KACE_BACKUP.log

REM Set config file location and name
SET config=C:\Data_Bin\K1000_Backup\KACE_BACKUP.tmp

REM Set WinSCP.com file location and name
SET winscp=C:\Data_Bin\K1000_Backup\WINSCP.COM

REM Set KBOX FTP Password
SET password=

REM Set KBOX hostname or IP
SET kbox=

REM Set backup file location
SET backuplocation=C:\Data_Bin\K1000_Backup\Backups\

REM Better Logging ON (1) or OFF (0)
SET logging=0

REM How many days to keep daily backups
SET daily=5

REM How many days to keep monthly backups
SET monthly=60

REM=============================
REM No editing needed after here
REM=============================

REM Is it the first of the month if so run a monthly backup
for /F "skip=2 tokens=2-4 delims=," %%C in ('WMIC Path Win32_LocalTime Get Day /Format:csv') do set date=%%C
IF %date%==1 CALL :Month

REM Update Backup Path
SET backup=%backuplocation%%day%

Call :Config

Call :Download

REM See if config completed
IF errorlevel 1 CALL error
    ELSE
        REM Remove old Backups
        forfiles /p "%backup%" /s /d -%daily% /c "cmd /c del @FILE"
        cls
        ECHO SUCCESSFULLY COPIED KACE BACKUPS

DEL %config%
REM pause
exit

:DayOfWeek
REM Find day of the week
SET daysofweek=Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday  
for /F "skip=2 tokens=2-4 delims=," %%A in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set daynumber=%%A  
for /F "tokens=%daynumber% delims=," %%B in ("%daysofweek%") do set day=%%B

GoTo :EOF

:Month
REM Update Backup Path
SET backup=%backuplocation%Monthly

Call :Config

Call :Download

REM See if config completed
IF errorlevel 1 CALL error
    ELSE
        REM Remove old Backups
        forfiles /p "%backup%" /s /d -%monthly% /c "cmd /c del @FILE"
        cls
        ECHO SUCCESSFULLY COPIED KACE BACKUPS

GOTO :EOF

:Config
REM Generate a config file to use for WinSCP, this avoids a second text file from needing to exist.
ECHO option batch abort > %config%
ECHO option confirm off >> %config%
ECHO open ftp://kbftp:%password%@%kbox% >> %config%
ECHO cd / >> %config%
ECHO option transfer binary >> %config%
REM I know there are sim links but WinSCP for some reason will not follow them. Maybe I have an error in config?
ECHO get *_kbox_file.tgz *_k1_dbdata.gz "%backup%\" >> %config%
ECHO close >> %config%
ECHO exit >> %config%

GOTO :EOF

:Download
REM Run WinSCP, use the temp config created above, and set log file location.
IF %logging%==1
    ECHO "=====MONTHLY BACKUP START=====" >> %log%
    "%winscp%" /console /script="%config%" /log="%log%"
    ECHO "=====MONTHLY BACKUP END=====" >> %log%
ELSE
        "%winscp%" /console /script="%config%"

DEL %config%

GoTO :EOF

:Error
cls
ECHO "ERROR COPYING KACE BACKUPS."
ECHO "=====CONFIG FILE START=====" >> %log%
ECHO %config% >> %log%
ECHO "=====CONFIG FILE END=====" >> %log%
ECHO "=====COMMAND RUN START=====" >> %log%
ECHO "%winscp%" /console /script="%config%" /log="%log%"
ECHO "=====COMMAND RUN STOP=====" >> %log%

GOTO :EOF