ksanjeevp Posted July 18, 2019 Report Share Posted July 18, 2019 How to Take backup of All of the Databases available on instance ? Sometimes Its required to take all of the Databases to be backed up periodically for more reliable backup process. If You require to get backup of any critical server's Database then You shouldn't do manually always, It will be better to automate the Process.SQL Query to take backup for All the Databases:- 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-- specify database backup directorySET @path = 'D:\BKP\' -- specify filename formatSELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) DECLARE db_cursor CURSOR READ_ONLY FOR SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master','model','msdb','tempdb') -- exclude these databasesOPEN 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 copy the above query and save it with file name i.e- sqlbackupquery. The above query will give the Database backed up file (.bak) on the location You will set.You can run it over SQL server Management Studio (SSMS) or on sqlcmd.Query will give you the database backup in the described format below.DBName_YYYMMDD.BAKYou can run it from SQL CMD, with the required parameters. for Example see below.sqlcmd -S .\InstanceName -i C:\SQLBackup\SQLBackupQuery.sql (backup query file location and file name)If you require it to schedule the Process for backup of all SQL databases on the path where you want You can schedule it from Task Scheduler.To create a task go to Run (windows +R) - taskschd.msc and create a task. schedule it as per the requirement. Here define the action. Note- User should have the Admin right,Logon as batch Job Right. You also can automate on SQL Server Express from above query and Process.Enjoy, Now You will be able to take the databases backup in one go and schedule it as per your requirement for convenience and saving the time.Thanks,SanjeevPS- Please write to me or comment if you have any question/Feedback. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.