Skip to content

Commit 1ed2418

Browse files
authored
Merge pull request #7991 from MicrosoftDocs/ScheduledBackupExpressEdition_pijocoder_010725
AB#3356: SQL: Re-organize and update SQL express scheduled backup article
2 parents 420958f + f3659a4 commit 1ed2418

File tree

1 file changed

+43
-45
lines changed

1 file changed

+43
-45
lines changed

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

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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/09/2025
55
ms.custom: sap:Database Backup and Restore
66
ms.topic: how-to
7+
ms.reviewer: jopilov
78
---
89
# Schedule and automate backups of SQL Server databases in SQL Server Express
910

@@ -14,7 +15,7 @@ _Original KB number:_   2019698
1415

1516
## Summary
1617

17-
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.
1819

1920
Currently SQL Server Express users can back up their databases by using one of the following methods:
2021

@@ -31,21 +32,21 @@ This article describes how to use a Transact-SQL script together with Task Sched
3132
> [!NOTE]
3233
> This applies to only SQL Server express editions and not to SQL Server Express LocalDB.
3334
34-
## More information
35+
## How to create a scheduled backup in SQL Express
3536

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

38-
**Step A**: Create stored procedure to Back up your databases.
39+
### Step 1: Create a stored procedure to back up your databases
3940

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

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

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

46-
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).
4748

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

5051
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:
5152

@@ -54,82 +55,79 @@ In a text editor, create a batch file that is named *Sqlbackup.bat*, and then co
5455
- 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.
5556

5657
> [!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.
5859
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
6061

61-
```sql
62+
```cmd
6263
// Sqlbackup.bat
6364
sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\', @backupType='F'"
6465
```
6566

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

68-
```sql
69+
```cmd
6970
// Sqlbackup.bat
70-
sqlcmd -U <YourSQLLogin> -P <StrongPassword> -S .\SQLEXPRESS -Q "EXEC sp_BackupDatabases  @backupLocation ='D:\SQLBackups', @BackupType='D'"
71+
sqlcmd -U <YourSQLLogin> -P <StrongPassword> -S .\SQLEXPRESS -Q "EXEC sp_BackupDatabases @backupLocation ='D:\SQLBackups', @BackupType='D'"
7172
```
7273

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

78-
```sql
79+
```cmd
7980
// Sqlbackup.bat
8081
sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\',@backupType='L'"
8182
```
8283

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

85-
```sql
86+
```cmd
8687
// Sqlbackup.bat
8788
sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\', @databaseName='USERDB', @backupType='F'"
8889
```
8990

9091
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.
9192

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
9394

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

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

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":::
100100

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

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:**)
104112

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,
106114

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

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

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

125121
> [!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
127125

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

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

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** folder regularly to make sure that you don't run out of disk space. The script doesn't contain the logic to clean up old files.
133131

134132
## Additional references
135133

0 commit comments

Comments
 (0)