How to create only n number of copies for database backup SQL Server

brgchamk  于 2023-06-21  发布在  SQL Server
关注(0)|答案(1)|浏览(146)

We're currently working with 5-6 projects simultaneous and we're setting schedule for backup database twice a week and we want to keep only 2-3 backup for each database.

The problem is the database(s) is increase exponential because we also have both dev and production databases, therefore we have to manually delete old backup file(s), this take lots of resource because only a few can access to the database.

Is there any way we can scheduled it for deleting old backup file?

We've research in Microsoft docs and still, nothing was found for this problem.

qkf9rpyu

qkf9rpyu1#

The xp_delete_file system stored procedure is dedicated to this job. Despite its name, this procedure only delete backup files generated by SQL Server BACKUP command.

Parameters are :

  1. FileTypeSelected (0 = backup, 1 = backup report)
  2. path to the backup directory (needs to terminate by "" on windows)
  3. suffix of file to delete (BAK, TRN, TXT)
  4. date before which the files are actually deleted
  5. deletion in the sub-directories (0 => the current directory, 1 => the current directory + the level 1 sub-directories)

As an example :

EXEC master.sys.xp_delete_file 0, N'C:\DataSave\SQL\Complete\', N'bak', N'20101231'

相关问题