How to set up daily database auto-backup in SQL Server
To set up automatic daily SQL Server backups, generate a backup script in dbForge Studio and run it on a schedule with Windows Task Scheduler or use a script to generate a native SQL Server backup. Both methods are below, step by step.
With dbForge Studio for SQL Server, you can streamline backup preparation using its visual script generation wizard or a ready-made script for native SQL Server backups. The first method uses dbForge's SQL backup feature and involves creating a backup project file that generates a .sql file with both CREATE database object scripts and INSERT data scripts. You can launch and schedule this project file execution using a .bat file. The second method involves using a SQL script to create a native SQL Server backup that can also be scheduled, simplifying and automating the backup process.
Both approaches help reduce manual effort and establish a repeatable backup routine intended to protect databases against accidental data deletion, failed updates, database corruption, and other incidents that can lead to data loss and costly downtime. Automating the process ensures that backups run consistently, reduces routine administrative work, and makes recovery more predictable when something goes wrong.
Best tools to automate SQL Server backups (compared)
There are quite a number of SQL Server backup automation tools on the market today. The table below lists the most popular of them.
| Tool | Type | Scheduling | Best for |
|---|---|---|---|
| dbForge Studio for SQL Server | GUI IDE | Yes (a .bat file + Windows Task Scheduler) | GUI-based backups |
| SQL Server Agent / Maintenance Plans | Built-in | Yes (jobs and schedules) | Native scheduled backups |
| Ola Hallengren Maintenance Solution | Free scripts | Yes (via SQL Server Agent) | Robust, scripted native backups |
| Redgate SQL Backup | Third-party | Yes | Compressed, managed backups |
| Veeam / Commvault | Enterprise backup | Yes | Enterprise-wide backup and recovery |
| Azure Backup | Cloud | Yes | Cloud and hybrid backup |
SQL Server backup types
There are several types of database backups in SQL Server, each with its own advantages and disadvantages:
- Full backup: This type of backup creates a complete copy of the entire database. It is the most comprehensive type of backup and can be used to restore the database to its exact state at the time the backup was taken. Full backups are essential for disaster recovery scenarios.
- Differential backup: This type of backup captures data extents that have changed since the most recent full backup, which serves as the differential base. It is faster than a full backup and requires less storage space, making it an efficient way to capture incremental changes.
- Transaction log backup: This type of backup captures transaction log records since the last transaction log backup, including records of insertions, deletions, and modifications. Transaction log backups are useful for point-in-time recovery, where you need to restore the database to a specific point in time.
When it comes to SQL Server databases, backups are usually performed directly on SQL Server, either by the SQL Server Agent or by third-party backup software. Regularly backing up data and storing it on a separate server or disk can provide an extra layer of protection against data loss. This type of backup, usually called native SQL Server backup, is typically reliable because it is less prone to human error.
You can also perform a backup on the client side, rather than by SQL Server itself. This approach is beneficial when you require a custom backup of specific tables or data that cannot be carried out using native backups or when you need to store the backup file physically on your computer. However, this type of backup is generally less reliable than native backups because it is more prone to human error. In this article, we will provide step-by-step instructions for setting up and scheduling both native backups and those made with the help of third-party tools.
Organizing your backup strategy: Methods for daily SQL Server backups on Windows
To automate SQL Server backups, you can either create a backup project in dbForge Studio for SQL Server or use a .sql file that contains your backup details. Both can be executed on schedule with a .bat file.
Method 1: Scheduling daily backups with a .sql file created in dbForge Studio that includes scripts for database object creation and data insertion
The overall idea behind this method is to use dbForge Studio for SQL Server to create a backup project file and a .bat file with command-line keys to execute the generated backup project and then use Windows Task Scheduler to schedule daily backups at the desired time.
Step 1. Create a backup project file with customized options.
The execution of the project file generates a .sql file that includes DDL statements to CREATE SQL Server database objects and DML statements to INSERT data. This file can be used to restore a database by simply executing it, using tools such as the sqlcmd utility, the Execute Large Script wizard of dbForge Studio, or the command-line functionality of the Studio with the /execute key.
To generate a backup project file, follow the steps below.
1. In Database Explorer, right-click the database for which you want to set up daily backups and select Tasks > Generate Scripts.
2. On the General page of the Generate Scripts Wizard that opens, set up the general script generation options: check the connection and database names, provide a path to the directory that the generated script file will be saved to, specify the output file name, select whether to append a timestamp to the file name, etc. Once done, click Next to proceed.
3. On the Script content page, you can select the database objects you want to include in the script and choose whether to include the database structure, data, or both. Click Next to continue.
4. On the Options page, you can customize the script options. Click the option to view its description. Once you have completed customizing the options, click Next to continue.
5. On the Errors handling page, you can configure how errors or issues encountered during the script generation should be handled; you can also set up the logging options.
6. Once you have completed the configuration of the database script generation, your next step is to save the options as a project file. To do so, click the Save dropdown and select Save Project.
7. Specify the destination folder where you would like to save the project and click Save.
Step 2. Create a .bat file to invoke the generated backup project.
Now that you have a backup project file ready, you need to generate a .bat file with the command-line keys that will launch the backup project.
1. Click the Save dropdown and select Save Command Line. In the dialog that opens, specify the path to the backup project file generated during Step 1, check the connection and database settings, and then provide a path to the result file. Optionally, click Validate to check the generated script. Click Save and specify a file name to save the .bat file.
2. The execution of the .bat file you created invokes dbForge Studio for SQL Server, launches the backup project created during Step 1, and generates a database backup according to the project settings.
Step 3. Create a scheduled backup task.
1. Type Task Scheduler in the Windows search bar and open Task Scheduler.
2. Click Create Basic Task in the Actions pane on the right side of the Task Scheduler window and give the task a name and a description. Click Next to continue.
3. On the Trigger page of the wizard, set the frequency of the backup task to Daily. Then specify the start time and click Next.
4. On the Actions page, select Start a Program and specify the location of the .bat file created during Step 2. Then click Next to proceed.
5. On the Finish tab, check the settings and click Finish to create the scheduled backup task.
Method 2: Scheduling daily backups using a script to create a native SQL Server backup
This method involves using a SQL script to create a native SQL Server backup, a .bat file to run the script, and Windows Task Scheduler to set up the .bat file execution.
Step 1. Create a .sql file with a script to generate a native SQL Server backup.
In a text editor, modify the following script by replacing the variables with your specific values: the database name and the backup destination. Verify that the backup folder exists and that the specified path is correct. Once the variables have been updated, save the file with the .sql extension.
SET CONCAT_NULL_YIELDS_NULL, ANSI_NULLS, ANSI_PADDING, QUOTED_IDENTIFIER, ANSI_WARNINGS, ARITHABORT, XACT_ABORT ON
SET NUMERIC_ROUNDABORT, IMPLICIT_TRANSACTIONS OFF
GO
--
-- Create backup folder
--
IF OBJECT_ID('xp_create_subdir') IS NOT NULL
EXEC xp_create_subdir N'D:\Backups'
GO
--
-- Backup database to the file with the name: <database_name>_<yyyy>_<mm>_<dd>_<hh>_<mi>.bak
--
DECLARE @db_name SYSNAME
SET @db_name = N'AdventureWorks2025'
DECLARE @filepath NVARCHAR(4000)
SET @filepath =
/*define base part*/ N'D:\Backups\' + @db_name + '_' +
/*append date*/ REPLACE(CONVERT(NVARCHAR(10), GETDATE(), 102), '.', '_') + '_' +
/*append time*/ REPLACE(CONVERT(NVARCHAR(5), GETDATE(), 108), ':', '_') + '.bak'
DECLARE @SQL NVARCHAR(MAX)
SET @SQL =
N'BACKUP DATABASE ' + QUOTENAME(@db_name) + ' TO DISK = @filepath WITH INIT' +
CASE WHEN EXISTS(
SELECT value
FROM sys.configurations WITH (NOLOCK)
WHERE name = 'backup compression default'
)
THEN ', COMPRESSION'
ELSE ''
END
EXEC sys.sp_executesql @SQL, N'@filepath NVARCHAR(4000)', @filepath = @filepath
GO
This script generates a .bak file, which can be used to restore the database via the Restore Database feature in SSMS or the Restore wizard of dbForge Studio for SQL Server.
Step 2. Create a .bat file to execute the .sql file created in the previous step.
To create a batch file (also known as a .bat file), open Notepad or any other plain text editor. Enter the following command:
"C:\Program Files\Devart\dbForge Studio for SQL Server\dbforgesql.com" /execute /connection:"Data Source=<server_name>;Integrated Security=False;User ID=<user_name>" /inputfile:"<path_to_file>" /password:<password> /quiet
Make sure to replace the placeholders with actual values. Once done, save the file with the .bat extension.
The execution of this file launches dbForge Studio for SQL Server to run the script created in Step 1.
Step 3. Create a scheduled backup task.
To schedule the execution of the created .bat file, you can use Windows Task Scheduler. For this, just follow the actions described in Step 3 of Method 1.
Conclusion
By scheduling daily backups, you can establish a consistent database backup routine that fits your environment and reduces the risk of data loss. In this article, we showed how dbForge Studio for SQL Server can help you prepare backup projects that generate scripts for database objects and data, while custom SQL scripts provide another way to automate native SQL Server backups. Whichever approach you choose, remember to test your backups and restore procedures regularly to ensure that your data can be recovered when needed.
At the same time, dbForge Studio has much more to offer than a reliable database backup and restoration workflow. Its rich set of features supports you at each stage of the database lifecycle: design, development, management, and administration. Download dbForge Studio for SQL Server for free and see it in action today!
FAQ
For native scheduling, the best option is SQL Server Agent and SSMS. If you need a reliable GUI-based backup flow, consider dbForge Studio for SQL Server with its intuitive wizards that help you set up automated database backups. Among free automation frameworks, Ola Hallengren's SQL Server Maintenance Solution stands out as a tool for native backups.
To schedule a daily automated backup of your SQL Server database, you can use dbForge Studio and Windows Task Scheduler. Follow the steps below.
- Create a backup project using the Generate Scripts Wizard in dbForge Studio for SQL Server.
- Create a .bat file to execute the backup project.
- Set up a daily task in Windows Task Scheduler that executes the .bat file.
To set up automated backups with SQL Server Agent, you need to configure them in SSMS as follows.
- Open SSMS and connect to your SQL Server instance.
- Start SQL Server Agent from the Object Explorer.
- Create a job that generates a backup file at regular intervals.
- Set the job's schedule as needed.
- Save the job.
Full backups create a complete copy of the entire database. This backup type is essential for disaster recovery scenarios.
Differential backups capture changes made to the database since the last full backup. They store a reduced amount of data compared to full backups and can speed up recovery.
Transaction log backups store transaction logs generated since the previous log backup. Such backups enable point-in-time database recovery.