SHARE
TWEET

Untitled

a guest Jun 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. EXEC xp_cmdshell 'FORFILES /p c:BACKUP /s /m *.sql /d -30 /c "CMD /C del /Q /F @FILE"'
  2.      
  3. ## Delete files older than 90 days
  4.  
  5. $a = Get-ChildItem C:Scripts
  6. foreach($x in $a)
  7.     {
  8.         $y = ((Get-Date) - $x.CreationTime).Days
  9.         if ($y -gt 90 -and $x.PsISContainer -ne $True)
  10.             {$x.Delete()}
  11.     }
  12.      
  13. EXECUTE master.dbo.xp_delete_file    
  14.     0,
  15.     N'**FILEPATH OF OLD BACKUPS LOCATION**',
  16.     N'bak',
  17.     N'**DATE BEFORE WHICH YOU DONT WANT TO KEEP BACKUPS** T **TIME**',
  18.     1 ;
  19.      
  20. -- Here is an example of creating a script table to run Forfiles against multiple paths
  21. -- Deletes after 7 days
  22. use master;
  23. set nocount on
  24. declare @forfiles_scripts table ([statements] varchar(255))
  25. insert into @forfiles_scripts select '''FORFILES /p "K:BACKUPS" /s /m *.bak /c "cmd /c Del @path" /d -7'''
  26. insert into @forfiles_scripts select '''FORFILES /p "R:BACKUPS" /s /m *.bak /c "cmd /c Del @path" /d -7'''
  27. insert into @forfiles_scripts select '''FORFILES /p "T:BACKUPS" /s /m *.bak /c "cmd /c Del @path" /d -7'''
  28. insert into @forfiles_scripts select '''FORFILES /p "V:BACKUPS" /s /m *.bak /c "cmd /c Del @path" /d -7'''
  29.  
  30. declare @command varchar(max)
  31. set     @command = ''
  32. select  @command = @command + 'exec master..xp_cmdshell ' + [statements] + char(10)
  33. from    @forfiles_scripts
  34. select (@command) for xml path (''), type
  35. --exec  (@command) -- Uncomment this line to run the process
  36. go
  37.  
  38.  
  39. -- Script tables are really handy to use.  You can use the same setup for copying files through Xcopy, Robocopy etc.
  40. use master;
  41. set nocount on
  42.  
  43. declare @xcopy_scripts table ([statement] varchar(255))
  44. insert into @xcopy_scripts select '''XCOPY "Y:BACKUPS*.bak" "K:BACKUPS"'''
  45. insert into @xcopy_scripts select '''XCOPY "Y:BACKUPS*.bak" "R:BACKUPS"'''
  46. insert into @xcopy_scripts select '''XCOPY "Y:BACKUPS*.bak" "T:BACKUPS"'''
  47. insert into @xcopy_scripts select '''XCOPY "Y:BACKUPS*.bak" "V:BACKUPS"'''
  48.  
  49. declare @command varchar(max)
  50. set     @command = ''
  51. select  @command = @command + 'exec master..xp_cmdshell ' + [statement] + char(10)
  52. from    @xcopy_scripts
  53. select (@command) for xml path (''), type
  54. --exec  (@command) -- Uncomment this line to run the process
  55. go
  56.      
  57. BACKUP DATABASE AdventureWorks2012
  58. TO DISK = 'Z:SQLServerBackupsAdventureWorks2012.Bak'
  59. WITH EXPIREDATE = DATEADD(hour, 3, GETDATE()) ;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top