You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: support/sql/database-engine/backup-restore/schedule-automate-backup-database.md
+22-27Lines changed: 22 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Schedule and automate backups of databases
3
3
description: This article describes how to use a Transact-SQL script and Windows Task Scheduler to automate backups of SQL Server Express databases on a scheduled basis.
4
-
ms.date: 09/25/2020
4
+
ms.date: 01/07/2025
5
5
ms.custom: sap:Database Backup and Restore
6
6
ms.topic: how-to
7
7
---
@@ -31,21 +31,21 @@ This article describes how to use a Transact-SQL script together with Task Sched
31
31
> [!NOTE]
32
32
> This applies to only SQL Server express editions and not to SQL Server Express LocalDB.
33
33
34
-
## More information
34
+
## How to create a scheduled backup in SQL Express
35
35
36
36
You have to follow these four steps to back up your SQL Server databases by using Windows Task Scheduler:
37
37
38
-
**Step A**: Create stored procedure to Back up your databases.
38
+
### Step 1: Create stored procedure to Back up your databases
39
39
40
40
Connect to your SQL express instance and create sp_BackupDatabases stored procedure in your master database using the script at the following location:
The `sqlcmd` utility lets you enter Transact-SQL statements, system procedures, and script files. In SQL Server 2014 and lower versions, the utility is shipped as part of the product. Starting with SQL Server 2016, `sqlcmd` utility is offered as a separate download. For more information, review [sqlcmd Utility](/sql/tools/sqlcmd-utility).
47
47
48
-
**Step C**: Create batch file using text editor.
48
+
### Step 3: Create a batch file using a text editor
49
49
50
50
In a text editor, create a batch file that is named *Sqlbackup.bat*, and then copy the text from one of the following examples into that file, depending on your scenario:
51
51
@@ -56,80 +56,75 @@ In a text editor, create a batch file that is named *Sqlbackup.bat*, and then co
56
56
> [!NOTE]
57
57
> The folder for the `SQLCMD` executable is generally in the Path variables for the server after SQL Server is installed or after you install it as stand-alone tool. But if the Path variable does not list this folder, you can either add its location to the Path variable or specify the complete path to the utility.
58
58
59
-
**Example 1:** Full backups of all databases in the local named instance of SQLEXPRESS by using Windows Authentication.
59
+
#### Example 1: Full backups of all databases in the local named instance of SQLEXPRESS by using Windows Authentication.
Similarly, you can make a differential Backup of USERDB by pasting in 'D' for the **@backupType** parameter and a log Backup of USERDB by pasting in 'L' for the **@backupType** parameter.
91
91
92
-
**Step D:** Schedule a job by using Windows Task Scheduler to execute the batch file that you created in step B. To do this, follow these steps:
92
+
### Step 4: Schedule a job by using Windows Task Scheduler to execute the batch file that you created in step 2
93
+
94
+
Follow these steps:
93
95
94
96
1. On the computer that is running SQL Server Express, click **Start**, then in the text box type *task Scheduler*.
95
97
96
98
:::image type="content" source="media/schedule-automate-backup-database/task-scheduler.png" alt-text="Screenshot of the Task Scheduler Desktop app option in the search bar of Start menu." border="false":::
97
99
1. Under **Best match**, click **Task Scheduler** to launch it.
98
-
99
100
1. In Task Scheduler, right-click on **Task Schedule Library** and click on **Create Basic task…**.
100
-
101
101
1. Enter the name for the new task (for example: SQLBackup) and click **Next**.
102
-
103
102
1. Select **Daily** for the Task Trigger and click **Next**.
104
-
105
103
1. Set the recurrence to one day and click **Next**.
106
-
107
104
1. Select **Start a program** as the action and click **Next**.
108
-
109
105
1. Click **Browse**, click the batch file that you created in Step C, and then click **Open**.
110
-
111
106
1. Check the box Open the Properties dialog for this task when I click **Finish**.
112
-
113
107
1. In the General tab,
114
-
115
-
1. Review the Security options and ensure the following for the user account running the task (listed under When running the task, user the following user account:)
108
+
- Review the Security options and ensure the following for the user account running the task (listed under When running the task, user the following user account:)
116
109
117
110
The account should have at least Read and Execute permissions to launch sqlcmd utility. Additionally,
118
111
119
112
- If using Windows authentication in the batch file, ensure the owner of the task permissions to do SQL Backups.
120
113
121
114
- If using SQL authentication in the batch file, the SQL user should have the necessary permissions to do SQL Backups.
122
115
123
-
1. Adjust other settings according to your requirements.
116
+
- Adjust other settings according to your requirements.
124
117
125
118
> [!TIP]
126
119
> As a test, run the batch file from Step C from a command prompt that is started with the same user account that owns the task.
127
120
128
-
Be aware of the following when you use the procedure that is documented in this article:
121
+
### Requirements
122
+
123
+
Be aware of the following requirements when you use the procedure that is documented in this article:
129
124
130
125
- The Task Scheduler service must be running at the time that the job is scheduled to run. We recommend that you set the startup type for this service as **Automatic**. This makes sure that the service will be running even on a restart.
131
126
132
-
-There should be lots of space on the drive to which the backups are being written. We recommend that you clean the old files in the Backup folder regularly to make sure that you do not run out of disk space. The script does not contain the logic to clean up old files.
127
+
-You must create sufficient amount of space on the drive where the backups are written. We recommend that you clean old files in the Backup folder regularly to make sure that you do not run out of disk space. The script doesn't contain the logic to clean up old files.
0 commit comments