Skip to content

Commit 6e38f4c

Browse files
authored
Re-organize and update article a little
1 parent 9f0a70a commit 6e38f4c

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

support/sql/database-engine/backup-restore/schedule-automate-backup-database.md

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Schedule and automate backups of databases
33
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
55
ms.custom: sap:Database Backup and Restore
66
ms.topic: how-to
77
---
@@ -31,21 +31,21 @@ This article describes how to use a Transact-SQL script together with Task Sched
3131
> [!NOTE]
3232
> This applies to only SQL Server express editions and not to SQL Server Express LocalDB.
3333
34-
## More information
34+
## How to create a scheduled backup in SQL Express
3535

3636
You have to follow these four steps to back up your SQL Server databases by using Windows Task Scheduler:
3737

38-
**Step A**: Create stored procedure to Back up your databases.
38+
### Step 1: Create stored procedure to Back up your databases
3939

4040
Connect to your SQL express instance and create sp_BackupDatabases stored procedure in your master database using the script at the following location:
4141

4242
[SQL_Express_Backups](https://raw.githubusercontent.com/microsoft/mssql-support/master/sample-scripts/backup_restore/SQL_Express_Backups.sql)
4343

44-
**Step B**: Download SQLCMD tool (if applicable).
44+
### Step 2: Download the SQLCMD client utility
4545

4646
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).
4747

48-
**Step C**: Create batch file using text editor.
48+
### Step 3: Create a batch file using a text editor
4949

5050
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:
5151

@@ -56,80 +56,75 @@ In a text editor, create a batch file that is named *Sqlbackup.bat*, and then co
5656
> [!NOTE]
5757
> 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.
5858
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.
6060

61-
```sql
61+
```console
6262
// Sqlbackup.bat
6363
sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\', @backupType='F'"
6464
```
6565

66-
**Example 2:** Differential backups of all databases in the local named instance of SQLEXPRESS by using a SQLLogin and its password.
66+
#### Example 2: Differential backups of all databases in the local named instance of SQLEXPRESS by using a SQLLogin and its password.
6767

68-
```sql
68+
```console
6969
// Sqlbackup.bat
7070
sqlcmd -U <YourSQLLogin> -P <StrongPassword> -S .\SQLEXPRESS -Q "EXEC sp_BackupDatabases  @backupLocation ='D:\SQLBackups', @BackupType='D'"
7171
```
7272

7373
> [!NOTE]
7474
> The SQLLogin should have at least the Backup Operator role in SQL Server.
7575
76-
**Example 3:** Log backups of all databases in local named instance of SQLEXPRESS by using Windows Authentication
76+
#### Example 3: Log backups of all databases in local named instance of SQLEXPRESS by using Windows Authentication
7777

78-
```sql
78+
```console
7979
// Sqlbackup.bat
8080
sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\',@backupType='L'"
8181
```
8282

83-
**Example 4:** Full backups of the database USERDB in the local named instance of SQLEXPRESS by using Windows Authentication
83+
#### Example 4: Full backups of the database USERDB in the local named instance of SQLEXPRESS by using Windows Authentication
8484

85-
```sql
85+
```console
8686
// Sqlbackup.bat
8787
sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\', @databaseName='USERDB', @backupType='F'"
8888
```
8989

9090
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.
9191

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:
9395

9496
1. On the computer that is running SQL Server Express, click **Start**, then in the text box type *task Scheduler*.
9597

9698
:::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":::
9799
1. Under **Best match**, click **Task Scheduler** to launch it.
98-
99100
1. In Task Scheduler, right-click on **Task Schedule Library** and click on **Create Basic task…**.
100-
101101
1. Enter the name for the new task (for example: SQLBackup) and click **Next**.
102-
103102
1. Select **Daily** for the Task Trigger and click **Next**.
104-
105103
1. Set the recurrence to one day and click **Next**.
106-
107104
1. Select **Start a program** as the action and click **Next**.
108-
109105
1. Click **Browse**, click the batch file that you created in Step C, and then click **Open**.
110-
111106
1. Check the box Open the Properties dialog for this task when I click **Finish**.
112-
113107
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:)
116109

117110
The account should have at least Read and Execute permissions to launch sqlcmd utility. Additionally,
118111

119112
- If using Windows authentication in the batch file, ensure the owner of the task permissions to do SQL Backups.
120113

121114
- If using SQL authentication in the batch file, the SQL user should have the necessary permissions to do SQL Backups.
122115

123-
1. Adjust other settings according to your requirements.
116+
- Adjust other settings according to your requirements.
124117

125118
> [!TIP]
126119
> 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.
127120
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:
129124

130125
- 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.
131126

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.
133128

134129
## Additional references
135130

0 commit comments

Comments
 (0)