Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Alternative to xp_cmdshell Expand / Collapse
Author
Message
Posted Friday, November 30, 2012 11:20 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, December 12, 2012 8:04 AM
Points: 297, Visits: 354
i need to delete some backup files as per daily operation through SQL ,currently i am using xp_cmdshell ,is there any alternative for xp_cmdshell so i dont need to enable the xp_cmdshell on all servers?


Post #1391629
Posted Saturday, December 01, 2012 2:07 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, May 18, 2013 1:28 AM
Points: 62, Visits: 1,140
You could write a powershell script to delete the files and schedule this with the SQL agent or windows task scheduler.
Post #1391632
Posted Saturday, December 01, 2012 5:39 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, March 30, 2013 9:39 AM
Points: 261, Visits: 966
do you have to do it through SQL server? why not use a VBS script and MS task scheduler

*************************************************************

The first is always the hardest
Post #1391642
Posted Saturday, December 01, 2012 6:08 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, May 20, 2013 6:18 AM
Points: 242, Visits: 882
Here's an alternative. Sneaky, but it works.

http://www.sql-server-pro.com/xp_cmdshell.html



FREE DOWNLOAD
www.sqlcopilot.com
Post #1391644
Posted Saturday, December 01, 2012 9:23 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, December 12, 2012 8:04 AM
Points: 297, Visits: 354
Thanks all, any idea what maintenance cleanup schedule uses in SQL server to clean up back up files?


Post #1391650
Posted Saturday, December 01, 2012 9:34 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Friday, May 17, 2013 11:21 AM
Points: 4,317, Visits: 9,216
You can use the 'undocumented' xp_deletefile which is called from the maintenance plan cleanup task. Since it is undocumentated - it may not work in future versions, but so far it has worked well for me from 2005 through 2008 R2.

Jeffrey Williams
Problems are opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #1391652
Posted Saturday, December 01, 2012 12:00 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, May 20, 2013 6:18 AM
Points: 242, Visits: 882
Or, instead of writing your own backup and maintenance scripts, you could use the ones by Ola Hallengren.

http://ola.hallengren.com

Highly recommended.



FREE DOWNLOAD
www.sqlcopilot.com
Post #1391657
Posted Sunday, December 02, 2012 8:04 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:35 PM
Points: 32,930, Visits: 26,817
Richard Fryar (12/1/2012)
Or, instead of writing your own backup and maintenance scripts, you could use the ones by Ola Hallengren.

http://ola.hallengren.com

Highly recommended.


Do you use it, Richard?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1391725
Posted Sunday, December 02, 2012 11:40 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, May 20, 2013 6:18 AM
Points: 242, Visits: 882
Hi Jeff,

Yes I do use it. It's good to find such well written and supported code provided free to the community.

I should have mentioned that "other scripts are available" as the BBC would say :)



FREE DOWNLOAD
www.sqlcopilot.com
Post #1391741
Posted Monday, December 03, 2012 6:40 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Wednesday, May 15, 2013 7:43 AM
Points: 656, Visits: 2,921
I use this VBS script (it works, please don't tell me that's old school) I wrote to do exactly that.

As explained in the comments, one benefit of this over a simple delete script is that it makes sure that the archive bit on each file has been cleared, which means that the network backup system has backed up that SQL backup file to tape. We occasionally experience times when our network backups hang, and I don't want to delete any SQL backups from disk until they've been copied off to tape.

Without xp_cmdshell, I don't think there's any way within SQL server natively to check the archive bit of a file; you'd need PowerShell, CLR, or some such.

BTW, I have a separate Agent job that runs after this job does. If it finds any backup files created before the nightly system backup last ran, with an uncleared archive bit -- in other words, a file that should have gone off to tape last night but did not -- it sends me an email alert that the network backup may have had problems.

'   Find all backup files in the SQL backup folder we can safely delete.
' "Safely delete" means:
' - file creation date older than DaysToRetain days AND
' - the archive bit has been cleared (meaning they've gone to tape)

' Created 1/7/2011 as an alternative to the SQL Maintenance Plan task so that
' we can not only check that the backups are more than x days old, but also check
' that the archive bit has been cleared before deleting them!
' Too many tape backup failures...

' ======================================================================================
' MODIFICATIONS
' ======================================================================================
' 2012-09-18: Changed path to new HD installed on server


' ======================================================================================
' How many days should we keep?
DaysToRetain = 5

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder("E:\SQLBackup")
' ======================================================================================

' Note the bitwise comparison: is Archive Bit not set (i.e., cleared).
' Don't use parentheses after "Not" or the script fails....

For Each f In oFolder.Files
If (LCase(Right(f.Name, 3)) = "bak" Or LCase(Right(f.Name,3)) = "trn") And _
f.DateCreated < DateAdd("d", (-1 * DaysToRetain), Date) And _
Not f.Attributes AND 32 Then
f.Delete
End If
Next


HTH,
Rich
Post #1391908
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse