1. Database backup
- Database backups are performed using the program “SqlBackup.exe”, which is located in the “Backup” directory of each NET Genium installation. The program supports backups of both MSSQL and Firebird databases.
- Although databases can generally be backed up either fully or incrementally, the program “SqlBackup.exe” supports full database backups only.
- The program “SqlBackup.exe” can be executed only on the computer where the database server is running and where the database is physically stored.
Details
- The program backs up and restores Firebird databases with the services of the Firebird database server, to which it connects on this computer through the default port 3050, and it logs in with the “SYSDBA” account and the default password “masterkey” – if the “SYSDBA” account has a different password or the server listens on a different port, neither the backup nor the restore of a Firebird database works.
- A Firebird database backup that does not finish within 4 hours is terminated, and an incomplete backup file remains in the target.
Usage:
SqlBackup.exe [source] [target]
- The source parameter specifies the database name (for an MSSQL database) or the full path to the database file on disk (for a Firebird database).
Details
- A Firebird database specified in the source parameter only by a file name with the “.fdb” extension and without a path is looked up by the program in the “X:\Firebird\” directory on the drive from which the program is running, even when a file with the same name is located in the current working directory.
- The target parameter specifies the file name or full path of the file in which the backup will be stored. The file name may contain the variables #year#, #month#, #day#, #dayofweek#, #dayofyear#, and #week#.
Example:
SqlBackup.exe netgenium netgenium-#dayofweek#
- Using variables in the backup file name is useful not only for identifying the backed-up database, but also for controlling the number of retained backups. The program “SqlBackup.exe” overwrites files with the same name. For example, the #dayofweek# variable can be used to maintain a weekly rotation of backups (files netgenium-1 through netgenium-7). The older file with the same name is deleted before the new backup starts.
Details
- If the new backup ends with an error, not even the older file with the same name remains in the target.
- If the target parameter is not specified (only the database name or its full path is provided), the backup file name is generated in the format “yyyy-mm-dd-database_name”, and the backup file is stored in the directory from which “SqlBackup.exe” is executed.
- When backing up a single MSSQL database, the optional parameter “/backuplog” can be used to also back up the database transaction log. The transaction log backup is stored in a separate file named in the format “database_backup_name-log.bak” and in the same location as the database backup.
Details
- After a successful backup run with the source parameter (typically a scheduled backup), a service named “SqlBackup” is automatically logged into the database table “ng_sluzbawindows” of the backed-up database with an interval of 1 day. A backup made with the B, BT or BTHIS option after starting the program without parameters does not log the service.
- A transaction log backup with the “/backuplog” parameter requires the “FULL” or “BULK_LOGGED” recovery model of the database. In the “SIMPLE” model, the program ends with an error even though the database backup has been created, and does not log the service into the “ng_sluzbawindows” table.
- MSSQL backups are not made as “COPY_ONLY”: the database backup becomes the base for the differential backups of other backup tools of the same database, and the transaction log backup truncates the log and joins the log backup chain.
When the program “SqlBackup.exe” is started without parameters, it offers several options depending on the required operation:
- The B option performs a database backup. It is important to note that MSSQL and Firebird databases are handled differently. An MSSQL database is identified by its database name and the MSSQL server instance, while a Firebird database is identified by the physical path to the database file. MSSQL backups use the “.bak” extension, while Firebird backups use the “.fbk” extension.
- When the B option is selected, the program “SqlBackup.exe” searches for available databases:
Details
- MSSQL databases in a single MSSQL server instance – in the instance specified by the -S parameter, otherwise in the first instance to which a connection can be established, in the order: instances found in the system registry (the default instance as (local)), (local) and (local)\SQLEXPRESS, each only once. If there are more than three of these instances, the program first asks which instance to connect to,
- Firebird databases in the directories “E:\Firebird”, “D:\Firebird”, and “C:\Firebird”, and in the directory of the Firebird database specified in the “ConnectionString.txt” file in the “Config” directory of the NET Genium installation in which the program is located,
- and then prompts the user to select the database to be backed up.
- The optional parameter -S can be used to explicitly specify an MSSQL server instance. The instance name is written directly after the parameter, without a space. The parameter is needed whenever the required instance is not the one the program selects on its own:
Details
- the instance has no entry in the system registry under the key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL” and is neither (local) nor (local)\SQLEXPRESS,
- there are at most three instances in the order given above and the required instance is not the first of them to which a connection can be established within one second – in that case the program does not ask for the instance,
- the program is run with the source parameter containing the name of an MSSQL database (typically a scheduled backup) or as “SqlBackup.exe -r file” with an MSSQL database backup – it then never asks for the instance, not even when there are more than three, and uses the first one to which a connection can be established.
Example:
SqlBackup.exe -S(local)\SQLEXPRESS
- The BTHIS or BT options back up the database specified in the file “ConnectionString.txt”, which is located in the “Config” directory.
- The backupall option backs up all databases that the program finds (for MSSQL without the system databases “master”, “model”, “msdb” and “tempdb”) to the directory from which it is executed, and names the backups in the format “yyyy-mm-dd-database_name”. If it finds more than one database, it asks before starting whether each of them should be backed up in a separate thread.
Details
- If two Firebird databases in different directories have the same name, the backup of the next one gets the suffix “ (2)”, “ (3)”... with the backupall option.
- Of the MSSQL databases, the backupall option includes only those that are online and to which the account the program uses to connect to the MSSQL server has access, and it skips the others without a warning.
- When backing up a single MSSQL database, the optional parameter “-compression” can be used to create the backup with compression.
1.1. Scheduling a database backup
- The program “SqlBackup.exe” is a console application and can therefore be executed using a batch file. This batch file can then be scheduled to run automatically using the Windows Task Scheduler.
- The batch file must use absolute paths both to the “SqlBackup.exe” program and to the target backup file. If the target file is not specified with a full path, the backup is stored in the working directory of the task, by default in the “C:\Windows\System32” directory.
Details
- An MSSQL database backup is written to the file by the SQL Server service under its own account. If this account has no write permission to the working directory of the task, a backup without a full path ends with an error.
- The “BackupDatabase” scheduled task is also created by the “Setup.exe” program. The task runs the “SqlBackup.exe” program in the “Backup” directory with the source parameter (the database from the “ConnectionString.txt” file) and without the target parameter, so a new file “yyyy-mm-dd-database_name” is created in the “Backup” directory every day and older backups are not deleted. For backup rotation, the target parameter with a variable, for example #dayofweek#, must be added in the “Task Scheduler” to the “Add arguments” field.
Details
- For an MSSQL database, the task also passes the -S parameter with the server from the “ConnectionString.txt” file to the program if it is specified there with the “server” key.
1.2. Batch file
- To schedule a database backup, it is recommended to create a batch file, for example “BackupDatabase.bat”, and save it in the NET Genium installation directory.
- The program “FileBackup.exe” does not include the “*.bak” and “*.fbk” backups saved to the “Backup” directory of the NET Genium installation (as in the samples below) in the “0.zip” archive – if they are to reach the file backup target as well, they have to be saved directly to it or copied separately.
Weekly backup (7 backups retained) – MSSQL
C:\inetpub\wwwroot\NETGenium\Backup\SqlBackup.exe netgenium C:\inetpub\wwwroot\NETGenium\Backup\netgenium-#dayofweek#
Weekly backup (7 backups retained) – Firebird
C:\inetpub\wwwroot\NETGenium\Backup\SqlBackup.exe C:\Firebird\netgenium.fdb C:\inetpub\wwwroot\NETGenium\Backup\netgenium-#dayofweek#
Monthly backup (31 backups retained) – MSSQL
C:\inetpub\wwwroot\NETGenium\Backup\SqlBackup.exe netgenium C:\inetpub\wwwroot\NETGenium\Backup\netgenium-#day#
Monthly backup (31 backups retained) – Firebird
C:\inetpub\wwwroot\NETGenium\Backup\SqlBackup.exe C:\Firebird\netgenium.fdb C:\inetpub\wwwroot\NETGenium\Backup\netgenium-#day#
Yearly backup (365 backups retained) – MSSQL
C:\inetpub\wwwroot\NETGenium\Backup\SqlBackup.exe netgenium C:\inetpub\wwwroot\NETGenium\Backup\netgenium-#month#-#day#
Yearly backup (365 backups retained) – Firebird
C:\inetpub\wwwroot\NETGenium\Backup\SqlBackup.exe C:\Firebird\netgenium.fdb C:\inetpub\wwwroot\NETGenium\Backup\netgenium-#month#-#day#
1.3. Scheduled task
- In the Task Scheduler, “Create Task...” is selected.
- On the “General” tab, the task name is entered and the user account under which the task will run is configured. The option “Run with highest privileges” must be enabled to ensure the backup can be completed successfully.
- On the “Triggers” tab, the time and frequency of the backup are configured with the “New” button.
- On the “Actions” tab, the execution of the batch file “BackupDatabase.bat” is configured with the “New” button.
2. Database restore
- A database is restored by starting the program “SqlBackup.exe” and selecting the R option (restore).
- The program searches for all available backups in the directory from which it is executed and prompts the user to select the backup to restore. Restoring to an existing database will overwrite it, and all users connected to this database are disconnected before the restore.
- After a backup is selected, the program prompts for the name of the database into which the backup should be restored.
- For security reasons, the program then asks for confirmation of the entered database name.
- At the end of the restore process, the program asks whether the database should be restored to the default directory of the MSSQL or Firebird database server.
Details
- The default MSSQL directory is the directory of the data file of the “master” database. The default Firebird directory is the first existing one of the directories “E:\Firebird”, “D:\Firebird” and “C:\Firebird”.
- An MSSQL database is restored to the files “database_name.mdf” and “database_name_log.ldf”. The program moves only the first two files of the backup and is therefore intended for databases with one data file and one log file.
- A Firebird database is restored to the file “DATABASE_NAME.FDB” with the name converted to upper case, also with the restoreall option and when run with the “-r” parameter.
- Before restoring a Firebird database, the program shuts the database down, which disconnects all connected users. If the restore ends with an error, the database may remain shut down (the “gfix” program supplied with Firebird brings it back online with the “-online” parameter) or damaged.
- The restoreall option restores the “.bak” and “.fbk” backups from the directory from which the program is executed. It derives the name of the target database from the file name without the extension. If the name is longer than 11 characters and the 11th character is a hyphen, it cuts off the first 11 characters (the “yyyy-mm-dd-” date, but also any other text of this length). It overwrites an existing database with the same name. It asks about the default directory of the database server and about restoring each database in a separate thread once for all backups.
Details
- If several backups lead to the same target database (for example the daily “yyyy-mm-dd-database_name” backups of the “BackupDatabase” scheduled task), the restoreall option restores only the newest of them – by the date in the file name, and if one of them has no date in the name or the dates are the same, by the time of the last change – and lists the others as skipped.
- Running “SqlBackup.exe -r file [database_name]” restores the backup without asking for the database name and directory to the default directory of the database server. Without the database_name parameter, the database is named after the file name without the extension.