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
Here is the linux version, doesn't do any cleanup; i.e. deleting of old files.
________________________
#!/bin/bash
USERNAME="kbftp"
PASSWORD="----"
SERVER="kbox"
#create local dir to store backup
echo "`mkdir -p /tmp/backup`"
echo "creating directory"
echo "FTP to get the backup files, this might take a few minutes"
# login to remote server and grab all kbox* files
ftp -n -i $SERVER <<EOF
user $USERNAME $PASSWORD
mget kbox*
quit
EOF
echo "relocating the files for transport to PSC"
#move the files we downloaded to tmp
theDate="`date +%m-%d-%Y`"
mkdir /somePath/$theDate
echo "`mv kbox* /somePath/$theDate`"
_______________________ - Jbr32 10 years ago
I have made some changes to just using the built in FTP on Windows as I never could get the alias files to download with WINSCP.
CODE:
@ECHO OFF
REM Created by Marc Johnson April 5th 2014
REM Last edit Oct 29, 2014
REM Set log file location and name
SET log=C:\K1000_Backup\KACE_BACKUP.log
REM Set config file location and name
SET config=C:\K1000_Backup\KACE_BACKUP.tmp
REM Set KBOX FTP Password
SET password=
REM Set KBOX hostname or IP
SET kbox=kbox
REM Set backup file location
SET backuplocation=C:\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
Call :DayOfWeek
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
SET backup=%backuplocation%%day%
forfiles /p "%backup%" /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
SET backup=%backuplocation%%day%
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.
ECHO user kbftp> %config%
ECHO %password%>> %config%
ECHO binary>> %config%
ECHO lcd "%backup%">> %config%
ECHO get kbox_dbdata.gz>> %config%
ECHO get kbox_file.tgz>> %config%
ECHO quit >> %config%
GOTO :EOF
:Download
ftp -n -s:%config% %kbox%
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 ftp -n -s:%config% %kbox%
ECHO "=====COMMAND RUN STOP=====" >> %log%
GOTO :EOF - smalls 10 years ago