Results 1 to 17 of 17

Thread: StoredProc to Back-up SQL Server

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    StoredProc to Back-up SQL Server

    Using ado I use this:

    VB Code:
    1. strExecute = "BACKUP DATABASE ['database name"] "
    2.     strExecute = strExecute & "TO DISK = 'path" ' "
    3.     strExecute = strExecute & "WITH INIT" ', STATS "
    4.     Dim AdoCmd As New ADODB.Command
    5.     With AdoCmd
    6.         .ActiveConnection = 'active connection
    7.         .CommandType = adCmdText
    8.         .CommandTimeout = 0
    9.         .CommandText = "use master"
    10.         .Execute
    11.         .CommandText = strExecute
    12.         .Execute
    13.         Set .ActiveConnection = Nothing
    14.     End With
    15.     Set AdoCmd = Nothing

    Can it be turned to T-SQL so I could just execute it asynchronously and dont wait till it is finished backing-up the sql server database?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: StoredProc to Back-up SQL Server

    Regardless of any solution you may get - this is not the way databases such SQL Server are backed up. Use SQL Server's backup functionality if you want to prevent your business from having problems in the future..

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: StoredProc to Back-up SQL Server

    dee-u - RhinoBull is very correct in the statement he made.

    MS SQL Server has maintenance plan features that you should take advantage of - such as full backups nightly, transaction log backups every hour (or whatever you business requirements may be).

    These features, available in Enterprise manager, are reasonably robust - although I'm sure they are being enhanced in SQL 2005...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: StoredProc to Back-up SQL Server

    RhinoBull: What should I use then, the SQLDmo? I want to back-up the server through code so as not to burden the user of this task.....

    szlamany: Is SQL Server capable of knowing when no user is already connected to it? Then back-up the database if it determines that no user is already connected? I could time it but maybe at the specified time there are still users who are adding to the database so the back-up is not that updated.....

    For stored proc backing-up I found this....

    VB Code:
    1. -- Create a logical backup device for the full MyNwind backup.
    2. USE master
    3. EXEC sp_addumpdevice 'disk', 'MyNwind_1',
    4.    DISK ='c:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\MyNwind_1.dat'
    5. -- Back up the full MyNwind database.
    6. BACKUP DATABASE MyNwind TO MyNwind_1

    Do you think the above code is a better option to back-up sql server than my previous method? By the way, why is it that you are against my method of backing-up the sql server database? What are its weaknesses?
    Last edited by dee-u; May 23rd, 2005 at 04:12 AM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: StoredProc to Back-up SQL Server

    dee-u - SQL Server does not need a "user free" environment for a backup to be successful.

    The way that SQL uses transaction file "write-ahead" logic means that a backup can occur with hundreds of users connected and be a perfectly good backup.

    The transaction log design of the system allows for point-in-time recovery to any minute you want.

    I believe that when the backup starts the transactions are held in the log, until the backup of the "disk data" is complete. With this you get a perfect image of data on disk and then the log is also included in the backup so that all "open" transactions are also in the backup.

    I've got one customer that does two data backups a day - one at 2:00 AM and one at 2:00 PM. Every hour a transaction log backup is taken as well - all day long.

    To summarize, SQL Server does not need a user-free moment for backup.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: StoredProc to Back-up SQL Server

    You got me wrong, I want to perform the back-up procedure when I'm sure no user is already connected to the sql server so the back-up would be up-to-date...... Anyway to do this automatically in sql server?

    And what can you say about the my posted code in my previous post? Will you discourage me from using it or its better than my first code?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: StoredProc to Back-up SQL Server

    Quote Originally Posted by dee-u
    You got me wrong, I want to perform the back-up procedure when I'm sure no user is already connected to the sql server so the back-up would be up-to-date
    This is not needed in MS SQL Server - backups are not up-to-date because a user is not connected.

    SQL is much stronger than that - if you came from ACCESS you will have to forget what you did in that realm and realize how strong a DB SQL SERVER is...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: StoredProc to Back-up SQL Server

    I'm getting confused.....

    Here is the scenario:

    At the end of the day I want to perform a back-up of a certain database in sql server, I want to do it when no user is already using the app that accesses that database to be 'sure' that there wouldnt be new insertions in that database after the back-up procedure is done.....

    Maybe I'm missing something else, if yes pls. guide me to the correct path....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: StoredProc to Back-up SQL Server

    Quote Originally Posted by dee-u
    I'm getting confused.....

    Here is the scenario:

    At the end of the day I want to perform a back-up of a certain database in sql server, I want to do it when no user is already using the app that accesses that database to be 'sure' that there wouldnt be new insertions in that database after the back-up procedure is done.....

    Maybe I'm missing something else, if yes pls. guide me to the correct path....
    That sounds like you are saying you want to make sure no one inserts records after the backup is done.

    Isn't that based on when people leave the system for the day? Isn't that the same time - let's say 8:00 PM - every night - that you can safely say no one will be adding more rows?

    Rows inserted after a backup become part of the next days backup - right?

    Rows inserted during a backup are in the transaction log - get committed to the database after the backup and become part of the next days backup - this is good.

    Give more details of your "real-life" time schedule - please...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: StoredProc to Back-up SQL Server

    You got me right but the problem is the indefinite time of when the users closes the system so I can not schedule a specific time.... Only when they are closing the system that I am sure they are already leaving.....

    I'm thinking if there is a way that when the system is closing it would trigger a back-up schedule/job in the sql server and that's why I have thought of stored procedure but there might be a better way.....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: StoredProc to Back-up SQL Server

    Quote Originally Posted by dee-u
    You got me right but the problem is the indefinite time of when the users closes the system so I can not schedule a specific time.... Only when they are closing the system that I am sure they are already leaving.....

    I'm thinking if there is a way that when the system is closing it would trigger a back-up schedule/job in the sql server and that's why I have thought of stored procedure but there might be a better way.....
    Look into SQLDMO - I've not used it, but I believe it will allow you to talk to the SERVER agent and schedule a job to run immediately. That job being the backup task...

    But, if you are truly tied to this concept, you can use a SPROC to run a BACKUP - if a "timestamped-via-user-activity" backup is that important to you.

    We produce about 1000 health claim checks at one of our customer sites every day - at around 1:30 PM. They have a maintenance plan scheduled backup run at 2:30 everyday to "incorporate" that activity.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: StoredProc to Back-up SQL Server

    So will this sample code fair enough as compared to my previous code?

    VB Code:
    1. -- Create a logical backup device for the full MyNwind backup.
    2. USE master
    3. EXEC sp_addumpdevice 'disk', 'MyNwind_1',
    4.    DISK ='c:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\MyNwind_1.dat'
    5. -- Back up the full MyNwind database.
    6. BACKUP DATABASE MyNwind TO MyNwind_1
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: StoredProc to Back-up SQL Server

    Here's a backup post I put up a month or so ago with the syntax we use.

    We do not do this in a SPROC - we do this in query analyzer...

    http://www.vbforums.com/showthread.p...t=backup+funds

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: StoredProc to Back-up SQL Server

    Isnt that code very similar to the code I posted in my first post? Is the path could be passed as a parameter to that stored proc?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: StoredProc to Back-up SQL Server

    Quote Originally Posted by dee-u
    Isnt that code very similar to the code I posted in my first post? Is the path could be passed as a parameter to that stored proc?
    First - USE is not needed, unless you are trying to "not be connected" to the database you are backing up. But you must realize that you being connected is not an issue.

    And yes you can pass the "path" as a parameter to the SPROC. I believe I've been in threads here that talked about that method.

    I'm pretty sure you can even parameterize the DB NAME - even though it's not in quotes!

    Code:
    Declare @DBName varchar(50)
    Declare @Path varchar(100)
    
    Set @DBName = 'SomeDB'
    Set @Path = 'd:\asdf\asdf\'
    
    Backup @DBName to @Path
    This should work - although I do not have SQL on me right now to test it with!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: StoredProc to Back-up SQL Server

    Here's another thread from a while back that has more examples of backup action like you are trying to do...

    http://www.vbforums.com/showthread.php?t=327401

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  17. #17

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: StoredProc to Back-up SQL Server

    Thanks, I made it work already.... Now problem is, I cannot seem to execute the Stored Procedure using a Command object (asynchronously), any idea why?

    Hmmmmnnn..... Perhaps this causes the error? While asynchrounously executing the stored procedure I closed the connection.... I just thought it this time while I'm typing! (It's not the exact code but similar one....

    VB Code:
    1. adoCommand.ActiveConnection=adoConnection
    2. adoCommand.Execute , , adAsynExecute
    3. adoConnection.Close

    This may really be a problem since I would like to execute the back-up procedure when my app is exiting, and when my app is exiting I tend to close the connections I have and set them to nothing.... I'll try to figure this out, perhaps some experts would help me.....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width