Automatic backup of SQL Server database

Let SQL Server 2008 automatic database backup, you need to create a SQL Server job, and start the SQL Server Agent, so the job to run periodically.
Specifically, follow these steps:

First, start the agent

Open SQL Server Management Studio, in Object Explorer,
confirm that SQL Server Agent is started, if not, right-click SQL Server Agent node, click "Start."

Second, New Job

Expand the SQL Server Agent node, right-click on the folder and select "New Job" in their job file in the pop-up "New Job" dialog box, name the work on the General election card, such as "backup TXDPCS".

Third, write code

Select the "Step" tab, click on the "New" button in the dialog box, create a name for that step, such as "Direct Backup", then at the command line, enter the following command:

DECLARE @fileName nvarchar(100) 
 SET @fileName='E:DB_Backup\' + REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(100), GETDATE(), 121), ' ', ''), ':', ''), '-', ''), '.', '') + '.bak' 
 PRINT 'Backup to ' + @fileName + '...' 
BACKUP DATABASE [数据库名称] TO DISK = @fileName WITH NOINIT , NOUNLOAD , NAME = N'数据库-备份', NOSKIP , STATS = 10, NOFORMAT

Click "OK" to save steps.

Fourth, set the timer task.

Select "New Job" program tab (this is the core of the periodic execution!), Click on the "New" button, name the plan, such as "One, three, five, seven weekly Execute" and other settings, such as 1 each Monday, Wednesday and Friday, seven execution time is 00:00:00 and so on. Finally, click "OK" to save the program.

V. Description

//默认账户
osql -E -Q "BACKUP DATABASE db_test TO DISK = 'D:\DB_BAK\db_test%date:~0,4%%date:~5,2%%date:~8,2%'"

//指定账户
osql -S .\MSSQL2008R2 -U netuser -P netuser2008 -Q"BACKUP DATABASE db_test TO DISK = 'D:\DB_BAK\db_test%date:~0,4%%date:~5,2%%date:~8,2%'"

db_test: the name of the database to be backed up
D: \ DB_BAK: Database Backup Path
% date: ~ 0,4 %% date: ~ 5,2 %% date: ~ 8,2%: YYYYMMDD date format

Sixth, you can use the manual backup batch

/************************************************************
 * 数据备份脚本:
 * 1.运行前,请根据实际配置修改相应账户信息;
 * 2.请将脚本执行结果另存为批处理文件,执行批处理文件即可;
 * 时间: 2015/3/10 14:21:15
 ************************************************************/
DECLARE @name        NVARCHAR(200)  
DECLARE @path        NVARCHAR(500)   
DECLARE @dbServer    NVARCHAR(500) 
DECLARE @dbUserName  NVARCHAR(500)   
DECLARE @dbUserPwd   NVARCHAR(500)   
--备份文件存放路径
SET @path = 'E:\05_DataBase_bakup\'   
--数据库服务器
SET @dbServer = '.'   
--数据库用户名       
SET @dbUserName = 'sa'  
--数据库用户密码        
SET @dbUserPwd = '123'        
DECLARE cursors CURSOR  
FOR
    --查询集合
    SELECT [name]
    FROM   [sysdatabases]
    WHERE  NAME NOT IN ('master', 'tempdb', 'model', 'msdb')
             
 OPEN cursors                       
 FETCH NEXT FROM cursors INTO @name  
WHILE @@fetch_status = 0
BEGIN
    --遍历集合
    PRINT 'osql -S ' + @dbServer + ' -U ' + @dbUserName + ' -P ' + @dbUserPwd + 
    ' -Q "BACKUP DATABASE ['
    + @name + '] TO DISK = ''' + @path +
    '[%date:~0,4%%date:~5,2%%date:~8,2%]' + @name + '.bak''"' 
    FETCH NEXT FROM cursors INTO @name
END
 CLOSE cursors                
 DEALLOCATE cursors

Guess you like

Origin www.cnblogs.com/blogcore/p/12467007.html