Create procedure [DBO]. [usp_data_backup] (@ database as varchar (100)
Begin
Declare @ bk_max_count as int,
@ Sqlstring as nvarchar (500 ),
@ Fname as varchar (100 ),
@ Path_prefix as varchar (100 ),
@ Variable_fname as varchar (100 ),
@ Del_variable_fname1 as varchar (100 ),
@ Del_variable_fname2 as varchar (100 ),
-- @ Database as varchar (100 ),
@ Devicename as varchar (100 ),
@ Log_devicename as varchar (100 ),
@ Shellstring as varchar (100)
Set @ bk_max_count = 12
Set @ devicename = 'dbk _ '+ @ Database
Set @ log_devicename = 'lbk _ '+ @ Database
Set @ path_prefix = 'd:/data_managerdb_backup /'
Set @ variable_fname = convert (char (8), getdate (), 112)
+ Convert (varchar (2), datepart (hour, getdate ()))
+ Convert (varchar (2), datepart (minute, getdate ()))
Set @ del_variable_fname1 = convert (char (8), getdate ()-7,112)
Set @ del_variable_fname2 = convert (char (8), getdate ()-7,112)
----------------------------------------------------------
-- Create D:/data_managerdb_backup/Trans
Set @ shellstring = 'mkdir' + @ path_prefix + 'Trans'
Exec master. DBO. xp_mongoshell @ shellstring
-- Create D:/data_managerdb_backup/keeping
Set @ shellstring = 'mkdir' + @ path_prefix + 'keeping'
Exec master. DBO. xp_mongoshell @ shellstring
-- Move the backup file starting with DBK _ in the d:/data_managerdb_backup/Trans folder to D:/data_managerdb_backup/keeping
Set @ shellstring = 'move/y' + @ path_prefix + 'Trans/'+ @ devicename +' _ *. * '+ @ path_prefix + 'keeping'
Exec master. DBO. xp_mongoshell @ shellstring
-- Move the backup file starting with LBK _ in the d:/data_managerdb_backup/Trans folder to D:/data_managerdb_backup/keeping
Set @ shellstring = 'move/y' + @ path_prefix + 'Trans/'+ @ log_devicename +' _ *. * '+ @ path_prefix + 'keeping'
Exec master. DBO. xp_mongoshell @ shellstring
Bytes -----------------------------------------------------------------------------------------------
----------------------------------------------------------------------
-- Delete the backup files starting with DBK _ in the d:/data_managerdb_backup/keeping folder one week ago.
Set @ shellstring = 'del '+ @ path_prefix + 'Keeping/' + @ devicename + '_' + @ del_variable_fname1 + '*. Bak'
Exec master. DBO. xp_mongoshell @ shellstring
-- Delete the backup file starting with LBK _ in the d:/data_managerdb_backup/keeping folder
Set @ shellstring = 'del '+ @ path_prefix + 'Keeping/' + @ log_devicename + '_' + @ del_variable_fname2 + '*. Bak'
Exec master. DBO. xp_mongoshell @ shellstring
Bytes -----------------------------------------------------------------------------------------------
Bytes -----------------------------------------------------------------------------------
Set @ sqlstring = n' select * From sysdevices where name = ''' + @ log_devicename + ''''
Exec sp_executesql @ sqlstring
If (@ rowcount> 0)
Begin
-- Delete the backup file of the disk if it exists.
Exec sp_dropdevice @ logicalname = @ log_devicename
End
--
Set @ fname = @ path_prefix + 'Trans/'+ @ log_devicename +' _ '+ @ variable_fname +'. Bak'
-- Add new disk backup files
Exec sp_addumpdevice 'disk', @ log_devicename, @ fname
Set @ sqlstring = n'backup log' + @ database + 'to' + @ log_devicename + 'with noinit'
-- Back up log files
Exec sp_executesql @ sqlstring
Bytes ------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Set @ sqlstring = n' select * From sysdevices where name = ''' + @ devicename + ''''
Exec sp_executesql @ sqlstring
If (@ rowcount> 0)
Begin
--
Exec sp_dropdevice @ logicalname = @ devicename
End
Set @ fname = @ path_prefix + 'Trans/'+ @ devicename +' _ '+ @ variable_fname +'. Bak'
Exec sp_addumpdevice 'disk', @ devicename, @ fname
Set @ sqlstring = n' backup database' + @ database + 'to' + @ devicename + 'with noinit'
-- Back up data files
Exec sp_executesql @ sqlstring
Bytes ------------------------------------------------------------------------------------------------
End