An example of a SQL Server database backup script

209 views 11:22 PM 0 Comments January 21, 2024

SQL Server database backup script:

sql
Copy code
— Specify the database name and backup file path
DECLARE @DatabaseName NVARCHAR(128) = ‘YourDatabaseName’
DECLARE @BackupPath NVARCHAR(256) = ‘C:\Backup\YourDatabase.bak’

— Set the backup options
DECLARE @BackupOptions NVARCHAR(256) = ‘WITH FORMAT, COMPRESSION, INIT, STATS = 10’

— Create the backup command
DECLARE @BackupCommand NVARCHAR(512) = ‘BACKUP DATABASE ‘ + @DatabaseName + ‘ TO DISK = ”’ + @BackupPath + ”’ ‘ + @BackupOptions

— Execute the backup command
EXEC sp_executesql @BackupCommand
In this script, you need to replace ‘YourDatabaseName’ with the name of the database you want to back up and ‘C:\Backup\YourDatabase.bak’ with the desired file path and name for the backup file. The @BackupOptions variable specifies the backup options, including FORMAT for overwriting existing backups, COMPRESSION for compressed backups, INIT for initializing the backup media, and STATS to display progress information during the backup.

You can execute this script in SQL Server Management Studio or any other SQL Server query tool to perform the backup. Make sure the SQL Server service account has proper permissions to write to the backup path specified.

Please note that this is a basic backup script, and you may need to modify it according to your specific requirements, such as including additional options or handling error conditions.

Tags: , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *