Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- EXEC xp_cmdshell 'FORFILES /p c:BACKUP /s /m *.sql /d -30 /c "CMD /C del /Q /F @FILE"'
- ## Delete files older than 90 days
- $a = Get-ChildItem C:Scripts
- foreach($x in $a)
- {
- $y = ((Get-Date) - $x.CreationTime).Days
- if ($y -gt 90 -and $x.PsISContainer -ne $True)
- {$x.Delete()}
- }
- EXECUTE master.dbo.xp_delete_file
- 0,
- N'**FILEPATH OF OLD BACKUPS LOCATION**',
- N'bak',
- N'**DATE BEFORE WHICH YOU DONT WANT TO KEEP BACKUPS** T **TIME**',
- 1 ;
- -- Here is an example of creating a script table to run Forfiles against multiple paths
- -- Deletes after 7 days
- use master;
- set nocount on
- declare @forfiles_scripts table ([statements] varchar(255))
- insert into @forfiles_scripts select '''FORFILES /p "K:BACKUPS" /s /m *.bak /c "cmd /c Del @path" /d -7'''
- insert into @forfiles_scripts select '''FORFILES /p "R:BACKUPS" /s /m *.bak /c "cmd /c Del @path" /d -7'''
- insert into @forfiles_scripts select '''FORFILES /p "T:BACKUPS" /s /m *.bak /c "cmd /c Del @path" /d -7'''
- insert into @forfiles_scripts select '''FORFILES /p "V:BACKUPS" /s /m *.bak /c "cmd /c Del @path" /d -7'''
- declare @command varchar(max)
- set @command = ''
- select @command = @command + 'exec master..xp_cmdshell ' + [statements] + char(10)
- from @forfiles_scripts
- select (@command) for xml path (''), type
- --exec (@command) -- Uncomment this line to run the process
- go
- -- Script tables are really handy to use. You can use the same setup for copying files through Xcopy, Robocopy etc.
- use master;
- set nocount on
- declare @xcopy_scripts table ([statement] varchar(255))
- insert into @xcopy_scripts select '''XCOPY "Y:BACKUPS*.bak" "K:BACKUPS"'''
- insert into @xcopy_scripts select '''XCOPY "Y:BACKUPS*.bak" "R:BACKUPS"'''
- insert into @xcopy_scripts select '''XCOPY "Y:BACKUPS*.bak" "T:BACKUPS"'''
- insert into @xcopy_scripts select '''XCOPY "Y:BACKUPS*.bak" "V:BACKUPS"'''
- declare @command varchar(max)
- set @command = ''
- select @command = @command + 'exec master..xp_cmdshell ' + [statement] + char(10)
- from @xcopy_scripts
- select (@command) for xml path (''), type
- --exec (@command) -- Uncomment this line to run the process
- go
- BACKUP DATABASE AdventureWorks2012
- TO DISK = 'Z:SQLServerBackupsAdventureWorks2012.Bak'
- WITH EXPIREDATE = DATEADD(hour, 3, GETDATE()) ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement