Back up all databases

Source: Internet
Author: User
Tags getdate

Use master;
Go
CREATE PROCEDURE Bakdb (@f varchar (max))
As
Begin
DECLARE @file varchar (max)
Set @[email protected]

Declare auth_cur cursor for select name from sysdatabases where name is not in (' Master ', ' model ', ' msdb ', ' reportserver ', ' Repor Tservertempdb ', ' tempdb ')
Open auth_cur;
DECLARE @dbname varchar (max)
FETCH NEXT from Auth_cur to @dbname
while (@ @fetch_status =0)
Begin
DECLARE @db varchar (max), @sql varchar (max)
Set @db = ' [' [email protected]+ '] '
Set @sql = ' '
Set @sql + = ' DECLARE @filename VARCHAR (500); '
Set @sql + = ' DECLARE @date DATETIME; '
Set @sql + = ' DECLARE @OLD_DATE DATETIME; '
Set @sql + = ' Set @date =getdate (); '
Set @sql + = ' Set @OLD_DATE =getdate ()-5; '--a backup of more than 5 days is about to be deleted
Set @sql + = ' Set @FILENAME = ' ' [email protected]+ ' \ ' +replace (@db, '/', ' _ ') + '-' +cast (DATEPART (YYYY, @DATE) as VARCHAR (10 ) + "-" +cast (DATEPART (MM, @DATE) as varchar) + '-' +cast (DATEPART (DD, @DATE) as varchar (10)) + '. BAK '; '
Set @sql + = ' BACKUP DATABASE ' [email protected]+ ' to DISK = @filename with COMPRESSION; '
Set @sql + = ' EXECUTE master.dbo.xp_delete_file 0,n ' [email protected]+ ', N ' bak ', @OLD_DATE, 1; '

EXEC (@sql)
FETCH NEXT from Auth_cur to @dbname
End
Close auth_cur;
Deallocate auth_cur;
End
GO

Back up all databases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.