How to create a batch file that stops the MS SQL Server
How to create a batch file that restarts the MS SQL Server
How to configure a backup for the MS SQL Server data using Backup4all
The database files are locked by the MS SQL Server. In order to backup
these files and avoid data corruption, it is necessary to perform these
actions:
1. Stop the MS SQL Server.
2. Perform the backup.
3. Restart the MS SQL Server.
To automate the process described above, a backup needs to be configured
in Backup4all that will automatically stop the MS SQL Server, perform the
backup and restart the MS SQL Server. The MS SQL Server will be
stopped/restarted using some commands from a batch file loaded by
Backup4all before/after the backup.
MS SQL Server can be stopped using the sc.exe command line program. It is recommended to stop the MS SQL Server during off-hours or in week-ends. Here is the syntax for calling sc.exe:
To automate the startup sequence of MS SQL Server a batch file that calls
sc.exe needs to be created and added as a “before action” in the backup job
scheduled in Backup4all. Below is an example of commands that can be used
to stop the MSSQLServer and SQLServerAgent services on a local computer
(code also contains a sequence that sets a delay between each commands,
to ensure the services were stopped):
@ECHO OFF
SET NUM=1
:LOOP
IF %NUM% == 6000 GOTO END1
SET /A NUM=NUM+1
GOTO LOOP
:END1
@ECHO ON
@ECHO OFF
SET NUM=1
:LOOP2
IF %NUM% == 6000 GOTO END2
SET /A NUM=NUM+1
GOTO LOOP2
:END2
@ECHO ON
These commands will be saved in a batch file (stop_sql.bat) that will be
executed by Backup4all before starting the backup.
To create the batch file:
2. Add the commands needed to stop the MS SQL service following the
example above (if is a local computer simply copy/paste the code
above).
When the scheduled backup will start, the commands from the batch file will
be executed, stopping the MS SQL Server. Only after that, the backup
process will start. When the backup process has finished, the MS SQL Server
must be restarted. You can set a second batch file to be automatically
executed after the backup in order to restart the MS SQL Server.
service is located. The name must use the Universal Naming
Convention (UNC) format ("\\myserver"). To run sc.exe locally,
ignore this parameter.
To automate the restart sequence of MS SQL Server, a batch file that calls
sc.exe needs to be created and added as an “after action” in the scheduled
backup job in Backup4all. Below is an example of commands that can be
used to restart the MSSQLServer and SQLServerAgent on a local computer:
@ECHO OFF
SET NUM=1
:LOOP
IF %NUM% == 6000 GOTO END
SET /A NUM=NUM+1
GOTO LOOP
:END
@ECHO ON
These commands will be saved in a batch file (start_sql.bat) that will be
executed by Backup4all after the backup process.
To create the batch file:
2. Add the commands needed to start the MS SQL service following the
example above (if is a local computer simply copy/paste the code
above).