# Tạo Backup tự động SQL Express

## HƯỚNG DẪN CMD

Các câu lệnh cmd cơ bản

1. Backup Full tất cả các database với xác thực windows

```powershell
// Sqlbackup.bat
 sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\', @backupType='F'"
```

2. Backup những thay đổi khác tất cả các database với xác thực SQL Login

```powershell
// Sqlbackup.bat
sqlcmd -U <YourSQLLogin> -P <StrongPassword> -S .\SQLEXPRESS -Q "EXEC sp_BackupDatabases  @backupLocation ='D:\SQLBackups', @BackupType='D'"
```

3. Backup log tất cả các databases với xác thực windows

```powershell
// Sqlbackup.bat
 sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\',@backupType='L'"
```

4. Backup full 01 database tên là UserDB với xác thực windows

```powershell
// Sqlbackup.bat
 sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\', @databaseName='USERDB', @backupType='F'" 
```

## Tạo task trên Task Scheduler

1. Vào menu search windows tìm kiếm ứng dụng Task Sche

<figure><img src="/files/9jqsNoVmfMSzN1aF4F6t" alt=""><figcaption></figcaption></figure>

2. Mở ứng dụng lên tạo 1 Basic task mới làm theo hướng dẫn
3. Chọn click chuột phải vào **Task Schedule Library** và nhấn vào **Create Basic task…**.
4. Nhập vào tên của Task mới  (ví dụ: SQLBackup) và nhấn **Next**.
5. Chọn **Daily**  (Hàng ngày) tại Task Trigger và nhấn **Next**.
6. Set the recurrence to one day and click **Next**.
7. Select **Start a program** as the action and click **Next**.
8. Click **Browse**, click the batch file that you created in Step C, and then click **Open**.
9. Check the box Open the Properties dialog for this task when I click **Finish**.
10. In the General tab,
    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:)

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

       * If using Windows authentication in the batch file, ensure the owner of the task permissions to do SQL Backups.
       * If using SQL authentication in the batch file, the SQL user should have the necessary permissions to do SQL Backups.
    2. Adjust other settings according to your requirements.

## TOOL TẠO TỰ ĐỘNG


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kythuat.dtechvn.com/sql-server/tao-backup-tu-dong-sql-express.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
