Original: https: //www.cnblogs.com/jaday/p/6088200.html
Demand Introduction: official database every day online backup and copy the backup file to the test server, the server automatically test database backup files to restore.
An Introduction:
Step 1: create a stored procedure in a formal library is used to back up the database and copied to the test server, then New Job timed execute a stored procedure created every day.
Step 2: Create a stored procedure on a test database server to restore the database, then create a job execution timing of stored procedures created every day.
ready:
File sharing backup file on the official server folder, and everyone's rights to the folder.
Create a shared folder on the test server, and file permissions to everyone's folder.
The official database backup and replicate databases Code:
--开启 xp_cmdshell sp_configure 'show advanced options',1 reconfigure go sp_configure 'xp_cmdshell',1 reconfigure go
USE [master] GO /****** Object: StoredProcedure [dbo].[backup_db_ksedu] Script Date: 11/22/2016 08:41:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --备份数据库 Create proc [dbo].[backup_db_ksedu] as Declare @strPsw varchar(50) Declare @strUsr varchar(50) Declare @strCmdShell varchar(300) Declare @strDataBaseName varchar(20) Declare @FullFileName Varchar(200) Declare @FullFileName1 Varchar(200) Declare @FullFileName2 Varchar(200) Declare @FileFlag varchar(50) Declare @FileFlag2 varchar(50) DECLARE @FileFlag3 varchar(50) Declare @ToFileName varchar(200) @SQLStr VARCHAR DECLARE (500) DECLARE @ SQLStr2 VARCHAR (500) DECLARE @ SQLStr3 VARCHAR (500) DECLARE @FlagDel VARCHAR (20) - the database name defined backup Set @ strDataBaseName = 'database name' - the definition of local backup file the name of the Set @ FileFlag strDataBaseName @ + = '_db_' + Replace (Convert (char (20 is), getdate (), 112), '', '') - defining local backup file name of the day before --Set @ FileFlag3 strDataBaseName @ + = '_db_' + Replace (Convert (char (20 is), getdate () - 1,112), '', '') - defines the remote backup file server name is 3 days before --Set @ FileFlag2 = @ strDataBaseName + '_db_' + the replace (Convert For (char (20), getdate () - 3,112), '', '') - set up a remote server's domain and user name to log the set @ strUsr = 'ZONGHEGUANLI-SQ \ Administrator' - - set up a remote server login password set @ strPsw = 'abc123 ' - set up a remote connection server @ StrCmdShell = SET 'NET xx.xxx.x.xxx use \\ \ to db_backup' @strPsw + + '/ User:' @ + strUsr - Set local backup file name Set @ FullFileName = 'e: \ bak \' + + @FileFlag '. BAK' - set the day before the local backup file name --set @ FullFileName1 = 'D: \ backup \' + @ FileFlag3 + '. BAK' - set up a remote server backup file name three days ago - @ FullFileName2 = -set '\\ 172.17.1.10 \ J $ \ db_backup \' + @ + FileFlag2 'BAK.' - set up a remote server to save the backup file directory set @ToFileName = '\\ xx.xxx.x.xxx \ db_backup \ ' --set @ ToFileName =' E: \ backup \ ' - when set to True, the backup is deleted, is set to False, which does not delete the backup files --set @ FlagDel =' False ' - set from a local copy the backup file to a remote server statement the set @ sqlstr = 'copy' + @ FullFileName + '' + @ ToFileName - local backup set to delete files the day before the --Set @ SQLStr2 = 'del'@ FullFileName1 + - Setting the remote server to delete the backup files 3 days ago = @ SQLStr3 the --set 'del' + @ FullFileName2 - compressed backup database backup the BACKUP DATABASE the TO DISK = @FullFileName the WITH @strDataBaseName the INIT, STATS = 20 is - connected to the remote server Exec master..xp_cmdshell @strCmdShell - copy the backup file to a remote server Exec master..xp_cmdshell @SQLStr - deleted one day before the local backup file --IF (@FlagDel = 'True') --exec Master .. the xp_cmdshell @ SQLStr2 ---- delete three days before the remote server backup file --IF (@FlagDel = 'True') --exec the xp_cmdshell Master .. @ SQLStr3
Test database restore database code:
The USE [Master] the GO / ****** Object: the StoredProcedure [the dbo] [restore_db_ksedu] Script a Date:. 11/22/2016 08:40:12 ****** / the SET the ANSI_NULLS the ON the GO the SET the QUOTED_IDENTIFIER the ON the GO PROCEDURE the ALTER [the dbo] [restore_db_ksedu]. the AS the Declare @strDataBaseName VARCHAR (50) the Declare @FileFlag VARCHAR (50) - defined backup database name Set @ strDataBaseName = 'database name' the Set @ FileFlag = 'D: \ to db_backup \' strDataBaseName @ + + '_db_' + Replace (Convert (char (20 is), getdate (), 112), '', '') + '. BAK' - plus phrase database is used to prevent the successful execution cause ALTER dATABASE [database name] the SET OFFLINE the wITH ROLLBACK IMMEDIATE Restore database [database name] from Disk = @ FileFlag with File = 1, REPLACE, recovery ALTER database [database name] set online
To create a job plan: Reference http://wenku.baidu.com/link?url=HLojwfVr1gkEuKjhk3Twsy7SxTWNMxovLg4LXpqvg_a3r50XQE5hZt8e03uPhbW8qUNjEx0IKa39DjyJ0spuNN1TV42UCIAMNDUQllhjsZu