Monday, March 19, 2012

Backup Database with code

Hello there
On beggining of any month i:
1. Backup the database,
2. Restore it as diffrent name
3. Run some actions that change the database
So far i have store procedure for the the changes only
Is there a way to build code/script that do the backup action and the
restore as diffrent name action?
..
roy@.atidsm.co.il
: 03-5611606
' 050-7709399Roy
> Is there a way to build code/script that do the backup action and the
> restore as diffrent name action?
If I understand you correctly , you need to write a stored procedure that
does backup of the database and the restore the database with a differnet
name. Am I right?
CREATE PROC myProc_Backup
AS
DECLARE @.FileName AS VARCHAR(255)
DECLARE @.Date AS VARCHAR(20)
SELECT @.Date =SELECT CONVERT(VARCHAR(50),GETDATE(),112)
SELECT @.FileName = 'N:\MyFolder\DatabaseName' + @.Date+'.bak'
BACKUP DATABASE DatabaseName
TO DISK = @.FileName
CREATE PROC myProc_Restore
AS
DECLARE @.FileName AS VARCHAR(255)
DECLARE @.Date AS VARCHAR(20)
SELECT @.Date =SELECT CONVERT(VARCHAR(50),GETDATE(),112)
SELECT @.FileName = 'N:\MyFolder\DatabaseName' + @.Date+'.bak'
RESTORE DATABASE DatabaseName_New FROM DISK= @.FileNameWITH RECOVERY,
MOVE 'DataBase_Data' TO 'N:\Program Files\Microsoft SQL
Server\MSSQL\Data\DataBase_data.mdf',
MOVE 'DataBase_Log' TO 'N:\Program Files\Microsoft SQL
Server\MSSQL\Data\DataBase__log.ldf'
Note: Create a job with two steps (backup/restore) and schedule it. Please,
make sure that you cannot restore operation while users perform some
activities at the database, it must be in SINGLE USER mode and before
RESTORE you need to drop an old database. Look , I don't need your business
requirements ,sothe logic might be changed.
If you want the SP to accept some parameters as Backup's Path or something
like this you can rewrite it
"Roy Goldhammer" <roygoldh@.hotmail.com> wrote in message
news:e$ZpbwUsFHA.2520@.TK2MSFTNGP10.phx.gbl...
> Hello there
> On beggining of any month i:
> 1. Backup the database,
> 2. Restore it as diffrent name
> 3. Run some actions that change the database
> So far i have store procedure for the the changes only
> Is there a way to build code/script that do the backup action and the
> restore as diffrent name action?
>
> --
>
> ..
> roy@.atidsm.co.il
> : 03-5611606
> ' 050-7709399
>

No comments:

Post a Comment