In order to reduce data loss caused by database server problems, the database is generally backed up, depending on the importance of data and performance compromise using different backup solutions.
The general backup solution is:
Full backup (once a week) + differential backup (once a day) + transaction log backup (once every two hours, depending on the importance of data)
In this case, even if a problem occurs, the data loss range is two hours.
First, describe the Database Backup Types separately:
1. Full backup
This is a common method for most people. It can back up the entire database, including user tables, system tables, indexes, views, stored procedures, and other database objects. However, it requires more time and space. Therefore, we recommend that you perform a full backup once a week.
Code
-- Weekly full backup
-- The back up file looks like this: test_2009121512_weekly.bak
Declare @ Filename Nvarchar ( 100 )
Set @ Filename = ' D: \ dbbackup \ test _ ' + Replace ( Replace ( Replace ( Replace ( Convert ( Char ( 16 ), Getdate (), 120 ), ' - ' , '' ), '
' , '' ), ' : ' , '' ), ' ' , '' ) + ' _ Weekly. Bak '
Declare @ Dbname Nvarchar ( 50 )
Declare @ SQL Nvarchar ( 500 )
Set @ Dbname = ' Test '
Set @ SQL = ' Backup Database ' + @ Dbname + ' To disk = ''' + @ Filename + ''' With noinit, nounload, noskip, stats = 10, noformat '
Exec ( @ SQL )
2. Transaction Log backup
The transaction log is a separate file that records database changes. During backup, you only need to copy the changes made to the database since the last backup. Therefore, it takes only a small amount of time. To make the database more robust, we recommend that you back up transaction logs hourly or even more frequently.
Code
-- Hourly log backup
-- The backup file looks liek this: test_log_2009121547_hourly.bak
Declare @ Filename Nvarchar ( 100 )
Set @ Filename = ' D: \ dbbackup \ test_log _ ' + Replace ( Replace ( Replace ( Replace ( Convert ( Char ( 16 ), Getdate (), 120 ), ' - ' , '' ), '
' , '' ), ' : ' , '' ), ' ' , '' ) + ' _ Hourly. Bak '
Declare @ Dbname Nvarchar ( 50 )
Declare @ SQL Nvarchar ( 500 )
Set @ Dbname = ' Test '
Set @ SQL = ' Backup log ' + @ Dbname + ' To disk = ''' + @ Filename + ''' With no_truncate '
Exec ( @ SQL )
3. Differential backup
It is another way to back up only a part of the database. It does not use transaction logs. Instead, it uses a new image of the entire database. It is smaller than the original full backup because it only contains the database changed since the last full backup. It has the advantage of fast storage and recovery. We recommend that you perform differential backup once a day.
Code
-- Daily differential backup
-- The backup file looks like this: test_200912151543_daily.bak
Declare @ Filename Nvarchar ( 100 )
Set @ Filename = ' D: \ dbbackup \ test _ ' + Replace ( Replace ( Replace ( Replace ( Convert ( Char ( 16 ), Getdate (), 120 ), ' - ' , '' ), '
' , '' ), ' : ' , '' ), ' ' , '' ) + ' _ Daily. Bak '
Declare @ Dbname Nvarchar ( 50 )
Declare @ SQL Nvarchar ( 500 )
Set @ Dbname = ' Test '
Set @ SQL = ' Backup Database ' + @ Dbname + ' To disk = ''' + @ Filename + ''' With noinit, nounload, noskip, stats = 10, noformat '
Exec ( @ SQL )
4. file backup
The database can be composed of many files on the hard disk. If the database is very large and cannot be backed up in one night, you can use file backup to back up part of the database every night. Generally, the database is not large enough to use multiple file storage, so this backup is not very common.