Code
-- ----------------------------------
-- -Back up the database
-- -Parameter: Backup folder address, database name exec dbbackup 'd: \ dbbak \ ', 'ihpmp'
-- -Note:
-- -1. You need to create a database folder and a RAR folder.
-- -2. Start the xp_cmdshell permission and configure the application configurator on the SQL Server peripheral.
-- -------------------------------------
Create Procedure [ DBO ] . [ Dbbackup ]
@ Prefix Nvarchar ( 100 ), -- Backup folder
@ Dbname Nvarchar ( 100 ) -- Database Name
As
Declare
@ Datefile Nvarchar ( 100 ),
@ Bakfile Nvarchar ( 100 ),
@ Rarfile Nvarchar ( 100 ),
@ Delcmd Nvarchar ( 100 ),
@ Rarcmd Nvarchar ( 150 ),
@ Bakname Nvarchar ( 150 )
Set @ Bakname = @ Dbname + ' Data backup '
Set @ Datefile = @ Dbname + Cast ( Year ( Getdate ()) As Varchar ( 4 )) + Cast ( Month ( Getdate ()) As Varchar ( 2 )) + Cast ( Day ( Getdate ()) As Varchar ( 2 ))
Set @ Bakfile = @ Prefix + @ Datefile + ' . Bak '
Set @ Rarfile = @ Prefix + ' RAR \ ' + @ Datefile + ' . Rar '
Backup Database @ Dbname To Disk = @ Bakfile With Init, nounload, name = @ Bakname , Noskip, stats = 10 , Noformat
Set @ Delcmd = ' Del ' + @ Prefix + ' RAR \ *. rar ' -- First clear the RAR backup folder
Set @ Rarcmd = ' C: \ progra ~ 1 \ WinRAR \ winrar.exe A-p123 ' + @ Rarfile + ' ' + @ Bakfile -- Change yourpw to the password you want to set for encrypted Compression
Exec Master .. xp_mongoshell @ Delcmd
Exec Master .. xp_mongoshell @ Rarcmd