Professional Documents
Culture Documents
Below scripts dont backup or restore the databases. It will generate the scripts that will help DBA job
easier to copy the database from one server to other server. We have to run the script in source server.
It will be helpful when we have to migrate N of dtabases or database with more files. Press CTRL+T to get
the text output. You can modify however you want. Use yellow color highlighted where condition to
filter the databases.
set nocount on
set @backuptype = 1 -- 1 for FULL backup, 2 for DIFFERENTIAL backup, 3 for LOG backup
select name
from sys.sysdatabases
open dbnamecursor
while(@@FETCH_STATUS = 0)
begin
if @backuptype = 1
begin
print 'BACKUP DATABASE ['+@dbname+']'
end
if @backuptype = 2
begin
end
if @backuptype = 3
begin
end
if @backuptype = 2
else
print 'GO'
end
close dbnamecursor
deallocate dbnamecursor
set nocount on
set @isnorecovery = 1 -- 1 for norecovery , 0 for recovering the database while restoring
set @iscopytosecondary = 1 -- 1 in case you want to use same file path, 0 for above data & log file path
select name
from sys.sysdatabases
select database_id,type,name,physical_name
from sys.master_files
while(@@FETCH_STATUS = 0)
begin
from @dbfiles
begin
from @dbfiles
if @filetype = 0
if @iscopytosecondary =1
else
else if @filetype = 1
if @iscopytosecondary =1
else
end
if @isnorecovery =1
print 'NORECOVERY,'
else
print 'RECOVERY,'
print 'GO'
end
close dbnamecursor
deallocate dbnamecursor