Tạo Backup tự động SQL Express

Hướng dẫn tạo backup tự động theo thời gian cho SQL Server Express dựa vào Task Scheduler

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

// Sqlbackup.bat
 sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\', @backupType='F'"
  1. Backup những thay đổi khác tất cả các database với xác thực SQL Login

// Sqlbackup.bat
sqlcmd -U <YourSQLLogin> -P <StrongPassword> -S .\SQLEXPRESS -Q "EXEC sp_BackupDatabases  @backupLocation ='D:\SQLBackups', @BackupType='D'"
  1. Backup log tất cả các databases với xác thực windows

// Sqlbackup.bat
 sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupLocation='D:\SQLBackups\',@backupType='L'"
  1. Backup full 01 database tên là UserDB với xác thực windows

// 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

  1. Mở ứng dụng lên tạo 1 Basic task mới làm theo hướng dẫn

  2. Chọn click chuột phải vào Task Schedule Library và nhấn vào Create Basic task….

  3. Nhập vào tên của Task mới (ví dụ: SQLBackup) và nhấn Next.

  4. Chọn Daily (Hàng ngày) tại Task Trigger và nhấn Next.

  5. Set the recurrence to one day and click Next.

  6. Select Start a program as the action and click Next.

  7. Click Browse, click the batch file that you created in Step C, and then click Open.

  8. Check the box Open the Properties dialog for this task when I click Finish.

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

Last updated