automatic backup and restore job for a database

I need to schedule a daily task that restores a database from a backup everyday ..how to create a job for this task in sql server 2008R2?
Teoman SahinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
From the same backup file or from the last backup file?
Are you restricting only a solution based on backup/restore or you are available to accept other solution?
0
Teoman SahinAuthor Commented:
from the last backup based on backup /restore
0
Vikas GargBusiness Intelligence DeveloperCommented:
Hello,

You can simply create the job by right clicking on the Job tab of SQL server Agent.

In the Step -> Click New and in that Give suitable Step NAme
 
Type - > T SQL

Database -> the database you want to restore on

In the command write the T SQL script to restore the db from the file location

RESTORE DATABASE CDCTest
FROM DISK = 'D:\SQLDatabases\SQL2012\MSSQL\Backup\CDCTest.bak'

and Then Click OK

You can also schedule it as per the requirement.

Hope this will helpful for you
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You'll need to create the RESTORE command dynamically. Also will you restore in the same SQL Server instance with a different database name or you'll restore to a different SQL Server instance?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
@Vikas:
How that can restore from the last backup? The requirement is to restore every day, so every single day should be from a different backup, right Teoman?
0
Teoman SahinAuthor Commented:
for example, there are two databases on the server, Sales and Sales_Test..in the evening,the database Sales will be backuped and restored to Sales_Test on the same server everyevening..
0
Vikas GargBusiness Intelligence DeveloperCommented:
I think if the backup file name is kept same then the given solution will work for restore.

However if you want to do the back up in the same job then add this query to step 1

BACKUP DATABASE CDCTest TO disk = 'E:\SQLDatabases\SQL2012\MSSQL\Backup\CDCTest.bak'

this will create a backup and from here the same can be restored to the required server
0
Deepak ChauhanSQL Server DBACommented:
Here is the backup and restore script with date stamp you can schedule these in SQL server agent job.

Backup Script

declare @dt varchar(30)
declare @db varchar(100)
declare @location nvarchar(max)
declare @name nvarchar(max)
declare @sql nvarchar(max)

set @db='Test'
set @dt=(select REPLACE(CONVERT(VARCHAR, GETDATE(), 103), '/', '')+'_'+ REPLACE(CONVERT(VARCHAR, GETDATE(), 108), ':', ''))
set @location='D:\Backup\'
set @name='N'''+@db+'-Full Database Backup'+''''

set @sql='Backup Database ['+@db+']  To Disk=N'''+@location+@db+'_'+@dt+'.bak'' WITH NOFORMAT, NOINIT, NAME ='+@name +
          ', SKIP, NOREWIND, NOUNLOAD,  STATS = 10'

--print @sql
Exec (@sql)

Open in new window


Restore script

declare @dt varchar(30)
declare @db varchar(100)
declare @location nvarchar(max)
declare @name nvarchar(max)
declare @sql varchar(max)

set @db='Test'
set @dt=(select REPLACE(CONVERT(VARCHAR, GETDATE(), 103), '/', ''))
set @location='D:\Backup\'
set @name='N'''+@db+'-Full Database Backup'+''''

set @sql='ALTER DATABASE ['+@db+']  SET SINGLE_USER WITH ROLLBACK IMMEDIATE' +CHAR(13)+char(10)+

'Restore Database ['+@db+']  From Disk=N'''+@location+@db+'_'+@dt+'.bak'' WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 5'+ CHAR(13)+char(10)

+'ALTER DATABASE ['+@db+']   SET MULTI_USER'
--print @sql
Exec (@sql)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Deepak ChauhanSQL Server DBACommented:
Just replace the @db variable value with your database name and @location with backup location.

set @db='Test'
set @location='D:\Backup\'
0
pr0t0c0l12Commented:
Backup:
You need to look into the "microsoft SQL maintenance plans." They allow you to schedule a backup of your sql database.  
Restore:

There is no 'Restore DB Task' in the maintenance planbut you can use the Execute T-SQL Statement task. In that task, add a restore script in your T-SQL statement.

example:

RESTORE DATABASE YourDBName FROM DISK = 'D:\DBbackups\YourDB.BAK'
WITH REPLACE
GO

Open in new window


This is the simplest way I can think of.
Good luck!
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
If you'll restore to the same instance then you can query the msdb directly for the last backup:
DECLARE @BakFile AS VARCHAR(MAX)

SELECT TOP 1 @BakFile = mf.physical_device_name
FROM msdb.dbo.backupset AS s
	INNER JOIN msdb.dbo.backupmediaset AS ms ON s.media_set_id = ms.media_set_id
	INNER JOIN msdb.dbo.backupmediafamily AS mf ON ms.media_set_id = mf.media_set_id
WHERE s.type = 'D'
	AND s.database_name = N'Sales' 
ORDER BY s.backup_finish_date DESC;

RESTORE DATABASE sales_test 
FROM DISK = @BakFile
WITH RECOVERY,
   MOVE 'Sales_Data' TO 'D:\NewLocation\Sales_test_Data.mdf', 
   MOVE 'Sales_Log' TO 'D:\NewLocation\Sales_test_Log.ldf'

Open in new window

NOTE:Set in the MOVE clause the correct logical names and new location.
0
Teoman SahinAuthor Commented:
the backup of Sales will be overwrited on the same place so otherwise,there will be lots of backup file on the disk and it will run out of disk space
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
the backup of Sales will be overwrited on the same place
Do you realize that when you overwrite the last backup file that you can't restore the database to an older date than the last 24 hours?
Backups should be always moved to another location and shouldn't stay in the server or they will be useless.
0
Deepak ChauhanSQL Server DBACommented:
In this case backup file name will always be same. You can use static backup file name let says Sales_DB.bak in the restore statement.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.