This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
computing:windows:mssql_backup [2012/01/20 21:56] gcooper |
computing:windows:mssql_backup [2022/02/10 07:42] (current) gcooper |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Microsoft SQL Server Backup ====== | ====== Microsoft SQL Server Backup ====== | ||
- | ===== Database Recovery Models ===== | + | **SQL Backup Background**: |
+ | **Download SQL Server Management Studio**: https:// | ||
+ | <note tip>In general, it is **simpler and easier to restore a complete backup** (single file) than a differential or log backup (multiple file). | ||
- | ==== Script | + | ===== Database Restore ===== |
- | Set all databases to the Full recovery model: | + | **[[https:// |
- | <file> | + | <note important>Many MSSQL backup systems (like Ahsay) |
- | /*-------------------------------------------------------------------------------- | + | |
- | Sometimes it is necessary | + | <note tip> |
- | In this script, we use a system table to get all the database names on our server. | + | <note important> |
- | Then we use a cursor to loop through all the records and then change the recovery | + | ==== MSSQL Management Studio ==== |
- | model using Alter Database command. | + | |
- | --------------------------------------------------------------------------------*/ | + | **Restore a Database Backup Using SSMS**: https:// |
- | --Declaration of variables | + | ==== Transact-SQL ==== |
- | declare | + | |RESTORE DATABASE database_name FROM full_database_backup WITH NORECOVERY |complete database backup |
- | @dbnm sysname, | + | |RESTORE DATABASE database_name FROM differential_backup WITH NORECOVERY |
- | @sql varchar(100) | + | |RESTORE DATABASE database_name FROM log_backup WITH NORECOVERY |
+ | |RESTORE DATABASE database_name FROM log_backup WITH NORECOVERY | ||
+ | |... |in chronological order | | ||
+ | |RESTORE DATABASE database_name FROM log_backup WITH NORECOVERY | ||
+ | |RESTORE DATABASE database_name WITH RECOVERY | ||
+ | ===== Database Recovery Models ===== | ||
- | -- Declare begin cursor to get the database names and get info from sys.databases catalog | + | ==== Script ==== |
- | declare cursor_db cursor | + | Set all databases |
- | for select name from sys.databases | + | |
+ | < | ||
+ | USE master | ||
+ | GO | ||
- | -- Using a cursor | + | -- Declare |
+ | DECLARE @dbname sysname, @cmd varchar(1000) | ||
- | open cursor_db | + | -- Declare a cursor to iterate through the list of databases |
- | fetch next from cursor_db into @dbnm | + | |
- | --While Loop with Alter database command | + | DECLARE db_recovery_cursor CURSOR FOR |
+ | SELECT name from sysdatabases | ||
- | while @@fetch_status = 0 | + | -- Open the cursor |
+ | OPEN db_recovery_cursor | ||
- | begin | + | -- Perform the first fetch and store the value in a variable. |
+ | FETCH NEXT FROM db_recovery_cursor INTO @dbname | ||
- | --print 'database is ' | + | -- loop through cursor until no more records fetched |
+ | WHILE @@FETCH_STATUS = 0 | ||
+ | BEGIN | ||
+ | IF (SELECT DATABASEPROPERTYEX(@dbname, | ||
- | --set @sql='alter database | + | -- create the alter database |
- | set @sql='alter database | + | SET @cmd = 'ALTER DATABASE "' + @dbname |
- | print 'sql is ' + @sql | + | |
- | exec (@sql) | + | |
+ | -- alter each database setting the recovery model to full | ||
- | fetch next from cursor_db into @dbnm | + | EXEC(@cmd) |
- | end | + | PRINT @dbname |
+ | END | ||
- | --clean up objects | + | FETCH NEXT FROM db_recovery_cursor INTO @dbname |
+ | END | ||
- | close cursor_db | + | -- close the cursor and deallocate |
- | deallocate | + | CLOSE db_recovery_cursor |
+ | DEALLOCATE db_recovery_cursor | ||
</ | </ |