join the MSSQLTips community

MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

 
Simple script to backup all SQL Server databases - MSSQLTips

MSSQLTips

MSSQLTips.com - your daily source for SQL Server tips
Welcome to MSSQLTips Sign in | Join | Help
in Search

Simple script to backup all SQL Server databases

Last post 12-10-2009 5:44 PM by nirajan. 25 replies.
Page 1 of 2 (26 items) 1 2 Next >
Sort Posts: Previous Next
  • 09-13-2006 12:00 AM

    Simple script to backup all SQL Server databases

    This post is related to this tip: Simple script to backup all SQL Server databases

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

  • 07-09-2008 3:17 PM In reply to

    Re: Simple script to backup all SQL Server databases

    Good Script man, it was very helpfull to me, and i hope it will be very much helpfull to others tooo....

    Thanks a lot. I needed the file name also in the same naming convention, it was really helpful to me

    .

    Thnak you so much.  :)

  • 08-13-2008 2:05 AM In reply to

    Re: Simple script to backup all SQL Server databases

     After many hundreds of years of searching the net and speaking to SQL X Spurts, a last a backup that I can understand

    Thanks so much 

  • 10-10-2008 2:26 PM In reply to

    • grobido
    • Top 10 Contributor
    • Joined on 10-11-2007
    • Wilton, NH
    • Posts 103

    Re: Simple script to backup all SQL Server databases

    Here is an update to the script to also include the time in the filename.

    DECLARE @name VARCHAR(50) -- database name
    DECLARE @path VARCHAR(256) -- path for backup files
    DECLARE @fileName VARCHAR(256) -- filename for backup
    DECLARE @fileDate VARCHAR(20) -- used for file name

    SET @path = 'C:\'

    SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) + '_' + REPLACE(CONVERT(VARCHAR(20),GETDATE(),108),':','')

    DECLARE db_cursor CURSOR FOR

    SELECT name
    FROM master.dbo.sysdatabases
    WHERE name IN ('test5')

    OPEN db_cursor

    FETCH NEXT FROM db_cursor INTO @name

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
    BACKUP DATABASE @name TO DISK = @fileName
    FETCH NEXT FROM db_cursor INTO @name

    END

    CLOSE db_cursor

    DEALLOCATE db_cursor

  • 11-04-2008 5:49 AM In reply to

    • JohanA
    • Not Ranked
    • Joined on 11-04-2008
    • Posts 1

    Re: Simple script to backup all SQL Server databases

    Hi, Thanks for this script.

     Is there anyway to get this to overwrite the files?

    I want to dump every day without date in the filename to put them on tape and not get a bunch of files stacked on the server.

    But when the job runs the next day it appends on the previous files and they get double in size.

  • 11-04-2008 7:43 AM In reply to

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

    Re: Simple script to backup all SQL Server databases

     If you want to overwrite the backup file, add a WITH INIT to the end of the BACKUP command.  This will overwrite as opposed to appending.

  • 11-20-2008 6:54 AM In reply to

    Re: Simple script to backup all SQL Server databases

    Great script but not sure how to get around the date variable as this creates a new database each time. With INIT as the command to overwrite the database, doesn't help if the database has a different name each time.

     

    Not a sql DBA obviously and if you were to change the following to a static name what syntax would you use? thanks

    SELECT @fileDate CONVERT(VARCHAR(20),GETDATE(),112)

    SET @fileName @path @name '_' @fileDate '.BAK' 

  • 11-20-2008 10:08 AM In reply to

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

    Re: Simple script to backup all SQL Server databases

     I think all you need to do is eliminate the filedate piece

    i.e.

    from

    SET @fileName @path @name '_' @fileDate '.BAK' 

    to

    SET @fileName @path @name '.BAK' 

     

  • 11-20-2008 10:13 AM In reply to

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

    Re: Simple script to backup all SQL Server databases

    Greg's script modified for a db that is backed up to a single file name with its contents overwritten would look something like this

    DECLARE @name VARCHAR(50) -- database name
    DECLARE @path VARCHAR(256) -- path for backup files
    DECLARE @fileName VARCHAR(256) -- filename for backup
    DECLARE @fileDate VARCHAR(20) -- used for file name

    SET @path = 'C:\'

    SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) + '_' + REPLACE(CONVERT(VARCHAR(20),GETDATE(),108),':','')

    DECLARE db_cursor CURSOR FOR

    SELECT name
    FROM master.dbo.sysdatabases
    WHERE name IN ('armando')

    OPEN db_cursor

    FETCH NEXT FROM db_cursor INTO @name
    WHILE @@FETCH_STATUS = 0

    BEGIN

        SET @fileName = @path + @name + '.BAK'
        BACKUP DATABASE @name TO DISK = @fileName WITH INIT
        FETCH NEXT FROM db_cursor INTO @name

    END

    CLOSE db_cursor
    DEALLOCATE db_cursor

  • 12-24-2008 9:56 PM In reply to

    • manish
    • Top 50 Contributor
    • Joined on 12-25-2008
    • India
    • Posts 10

    Re: Simple script to backup all SQL Server databases

     As I learned some where that cursors are not to be used so just updated the main article without cursors.

     

     

    DECLARE @name VARCHAR(50) -- database name
    DECLARE @path VARCHAR(256) -- path for backup files
    DECLARE @fileName NVARCHAR(256) -- filename for backup
    DECLARE @fileDate VARCHAR(20) -- used for file name
     
    SET @path = 'd:\Backup\'
    SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
    SELECT name,flag=0 into #tempbackup FROM master.dbo.sysdatabases WHERE name NOT IN ('master','model','msdb','tempdb')
    set rowcount 1
    WHILE (exists(SELECT * FROM #tempbackup WHERE flag=0))
     BEGIN
           Select @name=name from #tempbackup WHERE flag=0
           SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
           BACKUP DATABASE @name TO DISK = @fileName       
           Update #tempbackup set flag=1 WHERE flag=0
     END 
    set rowcount 0
    drop table #tempbackup
     
     
    Thanks
    Manish
     
  • 02-12-2009 4:09 AM In reply to

    Re: Simple script to backup all SQL Server databases

    Yours scipts are superb, very helpful - Thanks.

    I was wondering can the script be modified to backup half of the database instead of the whole DB?

    I have a large database and i want to backup half of it because of space isues on our hard disks.

    Regards, Riaz

  • 02-12-2009 5:58 AM In reply to

    • manish
    • Top 50 Contributor
    • Joined on 12-25-2008
    • India
    • Posts 10

    Re: Simple script to backup all SQL Server databases

    Riaz,

    How can we justify the half, as you said you want "to backup half of the database instead of the whole DB".

     

     

     

  • 02-12-2009 7:17 AM In reply to

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

    Re: Simple script to backup all SQL Server databases

     It sounds like to what you're referring to is archiving data.  Backing up a database just makes a copy of the data.  If you're experiencing data bloat, then you should look into archiving old data.

  • 02-12-2009 7:25 AM In reply to

    Re: Simple script to backup all SQL Server databases

    If the database is setup using mulitiple Files you can do file level backups instead of a full backup.

    If you have enough space on other drives you can split the backup into multiple files.

    See this tip:

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

    You can also do a backup to a network drive.  See this tip:

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

  • 02-12-2009 7:42 AM In reply to

    Re: Simple script to backup all SQL Server databases

    Our database is 22553.56 MB

    I would like to backup a small amount of this, i am not interested in having all the data backed up as space is an issue on our disk.

    Could this be chopped in say half?

    maybe i should read into archiving and do it this way.

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