-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackupAllDatabases.sql
More file actions
48 lines (37 loc) · 1.31 KB
/
Copy pathBackupAllDatabases.sql
File metadata and controls
48 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
declare @dbName as nvarchar(500);
declare @sql as nvarchar(1000);
declare @errorMessage as nvarchar(max);
set @errorMessage = '';
declare @parameters cursor;
set @parameters = cursor static for select [name] from master.sys.databases
where [name] NOT IN ('master','model','msdb','tempdb')
open @parameters;
fetch next from @parameters into @dbName
While @@FETCH_STATUS = 0
Begin
set @sql = 'BACKUP DATABASE ['+@dbName + ']
TO DISK = N''C:\CRM\db_zaloha\'+@dbName+'_daily.bck''
WITH NOFORMAT, INIT, NAME = N'''+@dbName+'-Full Database Backup'', SKIP, NOREWIND, NOUNLOAD, STATS = 10';
BACKUP DATABASE @name TO mig_backup;
begin try
exec sp_executesql @Sql
--print @sql;
end try
begin catch
print ERROR_MESSAGE();
print @sql;
set @errorMessage = @errorMessage + @sql + char(10)+char(13);
end catch
fetch next from @parameters into @dbName;
End
close @parameters
deallocate @parameters
-- tu sa odosiela email, ak je dlzka chybovej hlasky vacsia ako 0
IF len(@errorMessage)>0
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'devcrm_sql_mail', --treba edivat na platny profil
@recipients = 'rtakacs@gratex.com',--treba editovat mjo@gratex.com
@body = @errorMessage,
@subject = 'backup of database failed';
end