MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

ESSENTIALS: Home | Tips | Search | Categories | Top 10 | Products | Authors | Blogs | Forums | Webcasts | Advertise | About
Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files -

in Search

Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

Last post 08-20-2008 10:14 PM by bass_player. 16 replies.
Page 1 of 2 (17 items) 1 2 Next >
Sort Posts: Previous Next
  • 04-28-2008 12:30 AM

    Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    This post is related to this tip: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    http://www.mssqltips.com/tip.asp?tip=1486

  • 04-30-2008 5:09 PM In reply to

    • MrSato
    • Top 500 Contributor
    • Joined on 05-01-2008
    • Posts 1

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    For those of us who don't know scripting languages, can you specify what needs to be edited to reflect each database we want to backup, and anything else that might need editing?  The only obvious one, which is notated in the script, is where to put the log file.

    Thanks a bunch for the how-to though!  Looks like a slam dunk once I know where to put the database name(s).

  • 05-09-2008 1:30 AM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    The line containing the script

    SELECT @IDENT=MIN(database_idFROM SYS.DATABASES WHERE [database_id] AND NAME NOT IN ('TEMPDB')

    tells us which database not to include.  In this case, all databases will be backed up except for TEMPDB. To include a list of databases, simply remove the NOT in the script and specify your databases inside the parenthesis like this

     SELECT @IDENT=MIN(database_idFROM SYS.DATABASES WHERE [database_id] AND NAME IN ('database1', 'database2', 'etc') AND NAME NOT IN ('TEMPDB')

    You would still need to exclude the TEMPDB database as you cannot run a backup command for that. Since I wanted every database to be backed up, it would be easier to just exclude the TEMPDB instead

    I would strongly recommend learning TSQL scripting as this is essential to manage SQL Server well

  • 05-14-2008 4:18 PM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

     I am getting an error when I run the sqlcmd file.  The error is "Msg102, Level 15, State 1 - Incorrect syntax near 'S' - When I remove the sqlcmd from databaseBackup.cmd and run it independently, it works.  Any ideas?

     

    Thank you very much! 

  • 05-16-2008 8:42 AM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    There should be a space between the instance name and the -E switch. Check out the documentation for sqlcmd on MSDN.  Here's a sample sqlcmd call for a server instance named TESTSERVER\TEST

    sqlcmd -STESTSERVER\TEST -E -i"E:\SQL_Backup\scripts\backupDB.sql"  

  • 05-20-2008 7:21 AM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    Thanks very much for these scripts. I'm using SQL Server 2005 Standard Edition with the Maintenance Plan, but have adapted the VB script to delete the old BAK and TRN files once they've been moved onto tape. Thanks again!

  • 05-20-2008 9:15 AM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    It's great to hear that the tip is of great help.  Your feedback is very much appreciated. It would also be great if you could highlight some issues you want solved and we would be be happy to provide tips to resolve them

  • 05-21-2008 6:53 AM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    Okay - here goes, the next step in implementing this great little VB script.

    I would like to embed the VB script as part of an SQL Server agent job, which I could then include as part of my maintenance plan.

    So, I tried to create a new job with one step: an Operating System (CmdExec) step.

    I clicked on OPEN on the Job Step Properties page, browsed to the location of the vbs file then selected it, and the code was automatically inserted into the main window of the Job Step Properties page, as such:

    VB_DelDBBackups_MSSQL_Job_Properties

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    I didn't change any other settings,
    and then tried to run the job.

    Here's the error I got:

    VB_DelDBBackups_MSSQL_Job_Error

     

     

     

     

     

     

     

     

    Any info or feedback you may have would be terrific - thank you very much.

     Best,

    Simon

  • 05-26-2008 4:03 AM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    Use an ActiveX Script type and select VBScript in the option instead of Operating System (CmdExec)

  • 05-26-2008 6:33 AM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    Great - that worked perfectly, thank you so much.

    In case anyone out there is playing along at home, I also modified one of the conditions to check for TRN files (transactionl log files) as well as BAK file to delete.

                'Check if the file extension is BAK or TRN
                If (uCase(b)="BAK") Or (uCase(b)="TRN") Then

     Thanks again, this tutorial and all the help have been terrific - much appreciated.

    - Simon

  • 05-28-2008 12:47 PM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    Hello, All: 

    Would there be any way to have the script look through nested directories to delete backup files in them as well?

    In other words, instead of just deleting files in the root of the sFolder variable, I'd like the script to search through sub-directories in sFolder as well.

     As always, thanks in advance for any input/suggestions.

    -Simon

  • 05-29-2008 5:59 PM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    Try out this script and customize according to your need.


    Dim strRootFolder
    Dim intFileCount
    Dim objFSO
    getCount

    Sub getCount()
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       intFileCount= 0
       strRootFolder = "D:\MSSQLTips\test\"
       CheckFolder strRootFolder
       msgBox "Total files in " & strRootFolder & ": " & intFileCount
    End Sub

    Sub CheckFolder(strPath)
       Dim objFolder
       Dim objFile
       Dim objSubdirs
       Dim objLoopFolder
      
       msgBox "Checking directory " & strPath
       Set objFolder = objFSO.GetFolder(strPath)
      
      
       ' Check files in this directory
       For Each objFile In objFolder.Files
     a=objFolder & objFile.Name
     b = objFSO.GetExtensionName(a)

     'check if the file extension is BAK
      If uCase(b)="BAK" Then
              msgBox objFile
              intFileCount= intFileCount+ 1
     End IF
       Next
      
      
       ' Loop through all subdirectories
       Set objSubdirs = objFolder.SubFolders
       For Each objLoopFolder In objSubdirs
          CheckFolder objLoopFolder.Path
       Next
      
       Set objSubdirs = Nothing
       Set objFolder = Nothing
      
    End Sub

     

  • 08-14-2008 8:18 AM In reply to

    • LauraV
    • Top 150 Contributor
    • Joined on 08-14-2008
    • Posts 2

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    Can you help one more person?

    These scripts work terrific.  I have only had an issue on one instance that has Sharepoint databases with lots of dashes in the database name.

    It backs up all the databases without dashes perfectly, but throws an error on the others:

    Msg 102, Level 15, State 1, Server MSOCSP02\OFFICESERVERS, Line 1
    Incorrect syntax near '-'.
    Msg 319, Level 15, State 1, Server MSOCSP02\OFFICESERVERS, Line 1
    Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

    I tried putting in a semicolon after your SELECT @SQL = 'BACKUP DATABASE '+ @DBNAME +' TO DISK = ''D:\SQLBack\'+@DBNAME+'_db_' + @dateString +'.BAK'' WITH INIT'; line.  This is not working though.  Thanks in advance for your help!

  • 08-14-2008 10:31 AM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 161

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    Laura, trying putting brackets around the @dbname variable 

  • 08-19-2008 3:01 AM In reply to

    Re: Automating SQL Server 2005 Express Backups and Deletion of Older Backup Files

    Something like this

    SELECT @SQL 'BACKUP DATABASE ['+@DBNAME+'] TO DISK = ''E:\SQL_Backup\'+@DBNAME+'_db_' @dateString +'.BAK'' WITH INIT'

     

Page 1 of 2 (17 items) 1 2 Next >