:: Script Name: sp_backup.cmd :: Version: 1.0 :: Description: SharePoint Portal Server 2003 backup script :: Created By: Steve Clarke :: Created On: September 4, 2005 :: Last Updated: September 9, 2005 :: :: Dependencies: :: - blat.exe for sending e-mail :: http://www.blat.net :: :: Copyright (c) 2005 Infotech Canada Inc. (http://www.infotechcanada.com) :: :: Permission is hereby granted, free of charge, to any person obtaining :: a copy of this software and associated documentation files (the :: "Software"), to deal in the Software without restriction, including :: without limitation the rights to use, copy, modify, merge, publish, :: distribute, sublicense, and/or sell copies of the Software, and to :: permit persons to whom the Software is furnished to do so, subject to :: the following conditions: :: :: The above copyright notice and this permission notice shall be :: included in all copies or substantial portions of the Software. :: :: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, :: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF :: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. :: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY :: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, :: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE :: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. :: :: :: About Infotech Canada :: --------------------- :: Infotech Canada delivers portal solutions built on the Microsoft :: Office System platform and .NET. We offer SharePoint hosting, :: turn-key portal solutions, pre-built web parts, and custom portal :: integration. :: :: Visit our website at http://www.infotechcanada.com for more info. :: :: We hope you find this script useful! :: @echo off & setlocal enableextensions :: ------------------------------------------------------------------- :: @@ USER CONFIGURABLE SETTINGS :: ------------------------------------------------------------------- :: For this script you must specify a share for the backup destination. :: In a server farm environment, you cannot point SPSBackup.exe to a :: drive letter. Some tools, like forfiles, however, will not work with :: a UNC name, so we'll map to the drive letter specified for that. set backup_share=\\backupserver\backupshare set mapped_drive=S: :: Number of backups to retain. set backup_retention=1 :: Backup set location. This is the location of the file containing the :: list of folders you want ntbackup.exe to backup. You can edit it :: manually, or load it into the ntbackup GUI and save. set ntbackup_fileset=C:\Utils\sharepoint_backup\sp_backup.bks :: Set this to determine whether or not you will run SPSBackup.exe from :: this server. In a server farm environment, you need only run :: SPSBackup.exe on one front-end. set do_spsbackup=true :: If you skip SPSBackup on this system and know what front-end the :: SPSBackup is being done on, specify it here. We'll put a readme.txt :: file in the SPSBackup folder saying where the SPSBackup files are. set spsbackup_is_on=otherserver :: E-mail settings :: Recipients should be comma separated, with no spaces set email_recipients=name@example.local,someoneelse@test.local :: You can simply specify an email address, or use the format below to :: include the name and email address set email_from="SharePoint Administrator " :: mail server with (optional) port number set smtp_server=exchange.example.local:25 :: ------------------------------------------------------------------- :: @@ YOU SHOULDN'T HAVE TO EDIT BEYOND THIS POINT :: ------------------------------------------------------------------- :: @@ Misc Paramaters :: ------------------------------------------------------------------- set retcode=0 set debug=echo set hostname=%COMPUTERNAME% :: set the backup destination to \ set backup_dest_drive=%mapped_drive%\%hostname% set backup_dest_share=%backup_share%\%hostname% set backup_dest=%backup_dest_drive% :: Set the various backup sub-folders. :: we need to configure sps_backup for both drive letter and share :: access -- backup rotation using forfiles needs a drive letter, :: while spsbackup.exe in server farm environment must backup to :: share name, not drive letter. set sps_backup_dest=%backup_dest%\sps_backup set sps_backup_dest_share=%backup_dest_share%\sps_backup set misc_backup_dest=%backup_dest%\misc_backup :: set timestamp for /f "tokens=1-4 delims=/ " %%F in ('date /t') do (set curr_date=%%F%%G%%H%%I) for /f "tokens=1-4 delims=: " %%F in ('time /t') do (set curr_time=%%F%%G%%H) set ts=%curr_date%_%curr_time% :: log file set logdir=%backup_dest% set logfile=%backup_dest%\backup_%ts%.log :: path to backup apps set spsbackup_path="%ProgramFiles%\SharePoint Portal Server\Bin\SPSBackup.exe" set ntbackup_path="%SystemRoot%\System32\ntbackup.exe" :: Set eventcreate default options set eventcreate=eventcreate /L APPLICATION /SO sp_backup.cmd :: ------------------------------------------------------------------- :: @@ Pre-Backup Operations @@ :: ------------------------------------------------------------------- :: map drive letter to backup share net use %mapped_drive% %backup_share% if %errorlevel% neq 0 ( set retcode=1 call :log "*** Backup aborted: Couldn't map drive letter to backup share." goto :end ) :: create backup folder structure if not exist %backup_dest% mkdir %backup_dest% if not exist %sps_backup_dest% mkdir %sps_backup_dest% if not exist %misc_backup_dest% mkdir %misc_backup_dest% :: create the log dir, remove old logs if it exists if not exist %logdir% mkdir %logdir% if exist %logdir%\*.log del %logdir%\*.log :: ------------------------------------------------------------------- :: @@ BEGIN BACKUP :: ------------------------------------------------------------------- call :log "Backup Started" %eventcreate% /T Information /ID 100 /D "SP_BACKUP.CMD: Backup process started." :: == SPSBackup ===================================================== call :log "Begin SPSBackup" if /I %do_spsbackup%==TRUE goto :spsbackup_run else goto :spsbackup_skip goto :spsbackup_skip :spsbackup_run :: delete old backups forfiles /P "%sps_backup_dest%" /M *_backup_* /D -%backup_retention% /C "cmd /c rmdir /s /q @PATH" 2> nul :: Check for a README.TXT file, which indicates that SPSBackup was previously skipped for this :: server. if exist %sps_backup_dest%\README.TXT ( del %sps_backup_dest%\README.TXT ) :: Set the name of the sps backup. This is the folder where spsbackup.exe :: will place all backup files. set spsbackup_name=%hostname%_backup_%ts% :: create the folder for this backup mkdir "%sps_backup_dest%\%spsbackup_name%" :: call the spsbackup.exe util %spsbackup_path% /ALL /FILE "%sps_backup_dest_share%\%spsbackup_name%\%spsbackup_name%" if %errorlevel% neq 0 ( set retcode=1 call :log "*** Backup aborted: SPSBackup.exe failed." goto :end ) else ( call :log "SPSBackup was successful." ) goto :spsbackup_end :spsbackup_skip call :log "SPSBackup skipped. SPSBackup files are in %backup_share%\%spsbackup_is_on%." echo SPSBackup files are in %backup_share%\%spsbackup_is_on%. > "%sps_backup_dest%\README.txt" goto :spsbackup_end :spsbackup_end call :log "End SPSBackup" :: ================================================================== :: == Windows Backup ================================================ call :log "Begin Windows Backup" :: delete old backups forfiles /P "%misc_backup_dest%" /M *.bkf /D -%backup_retention% /C "cmd /c del @FILE" 2> nul %ntbackup_path% BACKUP "@%ntbackup_fileset%" /J %hostname%_%ts% /F "%misc_backup_dest%\%hostname%_system_backup_%ts%.bkf" if %errorlevel% neq 0 ( set retcode=1 call :log "*** Backup aborted: ntbackup.exe failed." goto :end ) else ( call :log "Windows Backup was successful." ) call :log "End Windows Backup" :: ================================================================== :: == IIS Metabase ================================================== call :log "Begin IIS Metabase backup" set iis_backup_file=%hostname%_iis_metabase_backup_%ts% set iis_metabase_folder=%SystemRoot%\System32\inetsrv\metaback :: delete old backups forfiles /P "%iis_metabase_folder%" /M *iis_metabase_backup* /D -%backup_retention% /C "cmd /c del @FILE" 2> nul forfiles /P "%misc_backup_dest%" /M *iis_metabase_backup* /D -%backup_retention% /C "cmd /c del @FILE" 2> nul :: The iisback.vbs script works by backing up the metabase to a hard- :: coded backup destination under inetsrv\metaback. Therefore, we :: need to run the backup, then copy the file over to the backup share. cscript "%SystemRoot%\system32\iisback.vbs" /backup /b %iis_backup_file% if %errorlevel% neq 0 ( set retcode=1 call :log "*** Backup aborted: Error backing up IIS Metabase." goto :end ) else ( call :log "IIS Metabase successfully backed up on local machine." ) xcopy "%iis_metabase_folder%\%iis_backup_file%.*" "%misc_backup_dest%" if %errorlevel% neq 0 ( set retcode=1 call :log "*** Backup aborted: Error copying IIS Metabase to backup share." goto :end ) else ( call :log "IIS Metabase successfully copied to backup share." ) call :log "End IIS Metabase backup" :: ================================================================== call :log "Backup Finished" :: ---------------- :: @@ END BAcKUP @@ :: ---------------- goto :end :log echo [ %date% %time% ] %~1 echo [ %date% %time% ] %~1 >> %logfile% goto :eof :abort echo *** Aborting echo. endlocal & exit /b %retcode% :end :: e-mail log file if %retcode% equ 0 ( set email_subject="SUCCESS: sp_backup on %hostname% was successful %DATE% %TIME%" ) else ( set email_subject="FAIL: sp_backup on %hostname% failed %DATE% %TIME%" ) blat "%logfile%" -t %email_recipients% -f %email_from% -server %smtp_server% -s %email_subject% :: log to event log if %retcode% equ 0 ( eventcreate /L APPLICATION /T INFORMATION /SO sp_backup.cmd /ID 1 /D %email_subject% ) else ( eventcreate /L APPLICATION /T ERROR /SO sp_backup.cmd /ID 2 /D %email_subject% ) :: delete mapped drive net use %mapped_drive% /delete %eventcreate% /T Information /ID 200 /D "SP_BACKUP.CMD: Backup process finished." endlocal & exit /b %retcode%