DECLARE @fname varchar(255)
DECLARE @fname_backup varchar(255)
DECLARE @fname_archive varchar(255)
DECLARE @archive_path varchar(255)
DECLARE @dbname varchar(255)
DECLARE @archive_cmd varchar(255)
DECLARE @put_cmd varchar(255)
DECLARE @delFname_backup varchar(255)
SET @dbname = 'SINHVIEN'
SET @fname = @dbname + '_' + LTRIM(STR(DAY(GETDATE()))) + '.' + LTRIM(STR(MONTH(GETDATE()))) + '.' + LTRIM(STR(YEAR(GETDATE()))) + '_' + LTRIM(STR(DATEPART(HOUR,(GETDATE())))) + '.' + LTRIM(STR(DATEPART(MINUTE,(GETDATE())))) + '.' + LTRIM(STR(DATEPART(SECOND,(GETDATE()))))
SET @archive_path = 'F:\Backup\'
SET @fname_backup = @archive_path + @fname + '.BAK'
SET @delFname_backup = 'del ' + @fname_backup
SET @fname_archive = @archive_path + @fname + '.RAR'
SET @archive_cmd = '"C:\Program Files\WinRAR\WinRAR.EXE" a ' + @fname_archive + ' ' + @fname_backup
SET @put_cmd = 'echo put ' + @fname_archive + '>> F:\Backup\ftpcmds.txt'
PRINT @fname_backup
PRINT @fname_archive
BACKUP DATABASE @dbname TO DISK = @fname_backup WITH INIT
EXEC xp_cmdshell @archive_cmd
EXEC xp_cmdshell @delFname_backup
EXEC xp_cmdshell 'echo open 192.168.1.98> F:\Backup\ftpcmds.txt'
EXEC xp_cmdshell 'echo user nguyenthao>> F:\Backup\ftpcmds.txt'
EXEC xp_cmdshell 'echo 123456>> F:\Backup\ftpcmds.txt'
EXEC xp_cmdshell 'echo cd Site_Backup>> F:\Backup\ftpcmds.txt'
EXEC xp_cmdshell 'echo bin>> F:\Backup\ftpcmds.txt'
EXEC xp_cmdshell 'echo ha>> F:\Backup\ftpcmds.txt'
EXEC xp_cmdshell @put_cmd
EXEC xp_cmdshell 'echo bye>> F:\Backup\ftpcmds.txt'
EXEC xp_cmdshell 'ftp -d -i -n -s:F:\BACKUP\ftpcmds.txt'