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
+43-45Lines changed: 43 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,10 @@
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/09/2025
5
5
ms.custom: sap:Database Backup and Restore
6
6
ms.topic: how-to
7
+
ms.reviewer: jopilov
7
8
---
8
9
# Schedule and automate backups of SQL Server databases in SQL Server Express
SQL Server Express editions do not offer a way to schedule either jobs or maintenance plans because the SQL Server Agent component is not included in these [editions](/sql/sql-server/editions-and-components-of-sql-server-version-15). Therefore, you have to take a different approach to back up your databases when you use these editions.
18
+
SQL Server Express editions don't offer a way to schedule either jobs or maintenance plans because the SQL Server Agent component isn't included in these [editions](/sql/sql-server/editions-and-components-of-sql-server-version-15). Therefore, you have to take a different approach to back up your databases when you use these editions.
18
19
19
20
Currently SQL Server Express users can back up their databases by using one of the following methods:
20
21
@@ -31,21 +32,21 @@ This article describes how to use a Transact-SQL script together with Task Sched
31
32
> [!NOTE]
32
33
> This applies to only SQL Server express editions and not to SQL Server Express LocalDB.
33
34
34
-
## More information
35
+
## How to create a scheduled backup in SQL Express
35
36
36
37
You have to follow these four steps to back up your SQL Server databases by using Windows Task Scheduler:
37
38
38
-
**Step A**: Create stored procedure to Back up your databases.
39
+
### Step 1: Create a stored procedure to back up your databases
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:
41
+
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
+
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
48
48
-
**Step C**: Create batch file using text editor.
49
+
### Step 3: Create a batch file using a text editor
49
50
50
51
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
52
@@ -54,82 +55,79 @@ In a text editor, create a batch file that is named *Sqlbackup.bat*, and then co
54
55
- If you are using SQL authentication, ensure that access to the folder is restricted to authorized users as the passwords are stored in clear text.
55
56
56
57
> [!NOTE]
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
+
> 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 doesn't list this folder, you can either add its location to the Path variable or specify the complete path to the utility.
58
59
59
-
**Example 1:** Full backups of all databases in the local named instance of SQLEXPRESS by using Windows Authentication.
60
+
#### 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
92
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:
93
+
### Step 4: Schedule a job by using Windows Task Scheduler to execute the batch file that you created in step 2
93
94
94
-
1. On the computer that is running SQL Server Express, click **Start**, then in the text box type *task Scheduler*.
95
+
Follow these steps:
95
96
96
-
:::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
-
1. Under **Best match**, click **Task Scheduler** to launch it.
97
+
1. On the computer that is running SQL Server Express, select **Start** and type **Task Scheduler** in the text box.
98
98
99
-
1. In Task Scheduler, right-click on **Task Schedule Library** and click on **Create Basic task…**.
99
+
:::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 the Start menu." border="false":::
100
100
101
-
1. Enter the name for the new task (for example: SQLBackup) and click **Next**.
101
+
1. Under **Best match**, select **Task Scheduler** to launch it.
102
+
1. In **Task Scheduler**, right-click **Task Scheduler (Local)** and select **Create Basic task**.
103
+
1. Enter the name for the new task (for example, **SQLBackup**) and select **Next**.
104
+
1. Select **Daily** for the Task Trigger and select **Next**.
105
+
1. Set the recurrence to one day and select **Next**.
106
+
1. Select **Start a program** as the action and select **Next**.
107
+
1. Select **Browse**, select the batch file that you created in [Step 3](#step-3-create-a-batch-file-using-a-text-editor), and then select **Open**.
108
+
1. Select the **Open the Properties dialog for this task when I click Finish** checkbox.
109
+
1. In the **General** tab:
102
110
103
-
1. Select **Daily** for the Task Trigger and click **Next**.
111
+
- 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:**)
104
112
105
-
1. Set the recurrence to one day and click **Next**.
113
+
The account should have at least Read and Execute permissions to launch the `sqlcmd` utility. Additionally,
106
114
107
-
1. Select **Start a program** as the action and click **Next**.
115
+
- If using Windows Authentication in the batch file, ensure the task owner has permission to do SQL backups.
108
116
109
-
1. Click **Browse**, click the batch file that you created in Step C, and then click **Open**.
117
+
- If using SQL Authentication in the batch file, the SQL user should have the necessary permissions to do SQL backups.
110
118
111
-
1. Check the box Open the Properties dialog for this task when I click **Finish**.
112
-
113
-
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:)
116
-
117
-
The account should have at least Read and Execute permissions to launch sqlcmd utility. Additionally,
118
-
119
-
- If using Windows authentication in the batch file, ensure the owner of the task permissions to do SQL Backups.
120
-
121
-
- If using SQL authentication in the batch file, the SQL user should have the necessary permissions to do SQL Backups.
122
-
123
-
1. Adjust other settings according to your requirements.
119
+
- Adjust other settings according to your requirements.
124
120
125
121
> [!TIP]
126
-
> 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.
122
+
> As a test, run the batch file from [Step 3](#step-3-create-a-batch-file-using-a-text-editor) from a command prompt that is started with the same user account that owns the task.
123
+
124
+
### Requirements
127
125
128
-
Be aware of the following when you use the procedure that is documented in this article:
126
+
Be aware of the following requirements when you use the procedure that is documented in this article:
129
127
130
-
- 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.
128
+
- 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
129
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 folderregularly to make sure that you do not run out of disk space. The script does not contain the logic to clean up old files.
130
+
-You must create sufficient space on the drive where the backups are written. We recommend that you clean old files in the **Backup** folderregularly to make sure that you don't run out of disk space. The script doesn't contain the logic to clean up old files.
0 commit comments