A regularly scheduled backup of databases used by SyAM server programs (System Area Manager, Management Utilities, and Site Manager can be implemented by creating a Windows batch script and running it with Task Scheduler. In this document we ll look at a batch script created for this purpose. Copy the batch file (in our example it s named syamserverbackup.bat to the server where SyAM programs are installed. Edit the file using Notepad or your preferred text editor. User configuration of the backup script By default, backup is enabled for all three supported SyAM server programs. For any program that s not installed on the server, or to skip the backup for a program for any reason, set the corresponding variable to 0. SET backup_area_manager=1 SET backup_management_utilities=1 SET backup_site_manager=1 Specify the backup directory. This must be on a local drive, as the SQL backup does not support mapped drives or network shares. The script will create the directory if it does not already exist. You ll need to make sure the available free space is sufficient to store your backup files. The script creates a log file syamserverbackup.log in the same directory. C:\syamserverbackup\ is the default backup directory. SET budir=c:\syamserverbackup\ Set the number of days for backup retention; the default is 10. When the backup script runs, any backup files older than this number of days will be removed. When you choose the number of days for retention, you ll want to take into account how often the backup runs (daily, weekly, etc. and your available disk space. If retention is set to 0, the script never removes old backups. SET retention=10 Specify your SQL database instance;.\sqlexpress is the default. If SQL has been installed according to the specifications in SyAM documentation, you should be able to open a command prompt on the server and run (for example: 1
sqlcmd -S.\SQLEXPRESS -Q "select name from sys.databases" substituting the correct database instance if it is not.\sqlexpress. The command should return a list of SQL databases including syamutility (if Management Utilities is installed and syamdb (if Site Manager is installed. SET dbinstance=.\sqlexpress Finally, if System Area Manager is installed to a directory other than the default C:SyAM\ you ll need to specify the correct directory. SET amdir=c:\syam\ Save the backup script file when you re finished editing. Scheduling the backup When the script backs up the System Area Manager database, Area Manager services are stopped and then restarted after the database copy. Backing up the SQL databases for Management Utilities and Site Manager locks database tables while the copy runs. The backup should be scheduled to run at a time of day that minimizes any inconvenience to users. On the server, go to Administrative Tools Task Scheduler. Click Create Basic Task. (Our illustrations are for Windows Server 2008 R2; other operating systems may have some differences. 2
The Create a Basic Task wizard will start. Enter a task name and description. Click Next. Select the backup task frequency. Click Next. 3
Set the time of day for the backup. Click Next. Leave Start a program selected. Click Next. 4
Browse to the script file. Click Next. Check the box to open the Properties dialog. Click Finish. 5
Select the option to run whether the user is logged on or not. Click OK. If prompted, enter the password for the user account that will be running the backup. Backup script log To verify that the backup is running correctly, examine syamserverbackup.log in the backup directory. 6
The log shows the sizes of backup files, giving warnings if a file of size zero has been created. Output of SQL backup commands is included for reference. Review the log periodically to insure that backups are being performed satisfactorily. Cleanup of old backup files If the retention variable is set to a non-zero value, the script performs these cleanup actions: All backup files of size zero will be removed. For backup files older than the number of days specified by the retention variable, old- will be prepended to the file name. For each database, old backups are removed, but only if a more current backup of that database exists. This is to prevent the last good (non-zero-size backup of a database from being removed, in case the backup is failing for any reason. If the retention variable is set to zero, the cleanup routine is skipped. You will need to maintain the directory manually or by some other automated process. 7
Backup script Long lines in the batch script may be displayed in this document on more than one line. Copy the batch script from this document and paste into Notepad to eliminate these line breaks. @ECHO OFF REM syamserverbackup.bat REM Database backup for: REM SyAM System Area Manager REM SyAM Management Utilities REM SyAM Site Manager REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++ REM Enable backup for a program by setting to 1. REM If the program is not installed, set to 0. SET backup_area_manager=1 SET backup_management_utilities=1 SET backup_site_manager=1 REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++ REM Specify backup directory. This must be on a local REM drive, not a network share. The directory will be REM created if it does not already exist. SET budir=c:\syamserverbackup\ REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++ REM Backups older than this number of days will be removed. REM If retention is set to 0, no backups will be removed. REM In any case, backups of a database are not removed if REM there is no current backup file for that database REM with size greater than zero. SET retention=10 REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++ REM Specify your SQL database instance. SET dbinstance=.\sqlexpress REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++ REM Specify installation directory for System Area Manager. SET amdir=c:\syam\ REM Append trailing slashes as required IF DEFINED amdir IF NOT "%amdir:~-1%"=="\" SET amdir=%amdir%\ IF DEFINED budir IF NOT "%budir:~-1%"=="\" SET budir=%budir%\ FOR /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time: =0%" DO SET DateTime=%%c-%%a- %%b_%%d-%%e-%%f IF NOT EXIST %budir% ( mkdir %budir% SET logname="syamserverbackup.log" SET logfile=%budir%%logname% IF EXIST %logfile% ( ECHO. >> %logfile% ECHO Start backup %DateTime% >> %logfile% IF %backup_area_manager% NEQ 1 ( ECHO No backup configured for System Area Manager >> %logfile% GOTO mubackup 8
IF NOT EXIST %amdir%javadb\syamdb.h2.db ( ECHO System Area Manager database not found >> %logfile% GOTO mubackup ECHO Backing up System Area Manager database >> %logfile% net stop smcentralmgr > nul 2>&1 net stop smwebsrv > nul 2>&1 net stop smdbsrv > nul 2>&1 copy %amdir%javadb\syamdb.h2.db %budir%syamdb-%datetime%.h2.db > nul 2>&1 net start smdbsrv > nul 2>&1 net start smwebsrv > nul 2>&1 net start smcentralmgr > nul 2>&1 IF NOT EXIST %budir%syamdb-%datetime%.h2.db ( ECHO System Area Manager backup failed, file does not exist >> %logfile% GOTO mubackup FOR %%A IN (%budir%syamdb-%datetime%.h2.db DO SET amsize=%%~za ECHO Size of System Area Manager backup is %amsize% >> %logfile% IF %amsize% EQU 0 ( ECHO System Area Manager backup failed, file is empty >> %logfile% GOTO mubackup :mubackup IF %backup_management_utilities% NEQ 1 ( ECHO No backup configured for Management Utilities >> %logfile% GOTO smbackup sqlcmd -S %dbinstance% -Q "select name from sys.databases" find "syamutility" > nul IF %ERRORLEVEL% EQU 1 ( ECHO Management Utilities database not found >> %logfile% GOTO smbackup sqlcmd -S %dbinstance% -Q "BACKUP DATABASE syamutility TO DISK='%budir%syamutility- %DateTime%.bak'" >> %logfile% IF NOT EXIST %budir%syamutility-%datetime%.bak ( ECHO Management Utilities backup failed, file does not exist >> %logfile% GOTO smbackup FOR %%A IN (%budir%syamutility-%datetime%.bak DO SET amsize=%%~za ECHO Size of Management Utilities backup is %amsize% >> %logfile% IF %amsize% EQU 0 ( ECHO Management Utilities backup failed, file is empty >> %logfile% GOTO smbackup :smbackup IF %backup_site_manager% NEQ 1 ( ECHO No backup configured for Site Manager >> %logfile% GOTO cleanup sqlcmd -S %dbinstance% -Q "select name from sys.databases" find "syamdb" > nul IF %ERRORLEVEL% EQU 1 ( ECHO Site Manager database not found >> %logfile% GOTO cleanup sqlcmd -S %dbinstance% -Q "BACKUP DATABASE syamdb TO DISK='%budir%syamdb- %DateTime%.bak'" >> %logfile% 9
IF NOT EXIST %budir%syamdb-%datetime%.bak ( ECHO Site Manager backup failed, file does not exist >> %logfile% GOTO cleanup FOR %%A IN (%budir%syamdb-%datetime%.bak DO SET amsize=%%~za ECHO Size of Site Manager backup is %amsize% >> %logfile% IF %amsize% EQU 0 ( ECHO Site Manager backup failed, file is empty >> %logfile% GOTO cleanup :cleanup IF %retention% LEQ 0 ( ECHO Cleanup of old backups is not configured >> %logfile% GOTO end REM delete backups of size zero forfiles /p %budir% /m syamdb*.db /c "cmd /c if @fsize==0 del @file" > nul forfiles /p %budir% /m syamutility*.bak /c "cmd /c if @fsize==0 del @file" > nul forfiles /p %budir% /m syamdb*.bak /c "cmd /c if @fsize==0 del @file" > nul REM rename old backups forfiles /d -%retention% /p %budir% /m syamdb*.db /c "cmd /c rename @file old-@file" > nul forfiles /d -%retention% /p %budir% /m syamutility*.bak /c "cmd /c rename @file old- @file" > nul forfiles /d -%retention% /p %budir% /m syamdb*.bak /c "cmd /c rename @file old-@file" > nul REM delete old backups unless no current backup exists IF EXIST %budir%syamdb*.db ( DEL %budir%old-syamdb*.db > nul IF EXIST %budir%syamutility*.bak ( DEL %budir%old-syamutility*.bak > nul IF EXIST %budir%syamdb*.bak ( DEL %budir%old-syamdb*.bak > nul :end ECHO Backup script completed >> %logfile% EXIT 10