Cari Blog Ini

13 Juni 2012

Backup Database and Copying File To Another Server

------------------------------------------------------------------------------------
-----code:start
------------------------------------------------------------------------------------
Declare
    @Backup as varchar(100),
    @BackupFile as varchar(100)
   
select @BackupFile='DB_Test'
    +'_'+ convert(varchar(20),getdate(),12)
    +'_'+ replace (CONVERT(VARCHAR(5),GETDATE(),108), ':', '')+'.dat'
   
select @Backup='C:\Backup_DBTest\'+@BackupFile

backup database [DB_Test] to DISK=@Backup


Declare @CopyCommand as varchar(200)

-----For Copying Files Backup
select
@CopyCommand='Copy '+@Backup+' \\xxx.xxx.x.44\Share\BackupDB\'+@BackupFile

EXEC master..xp_cmdshell @CopyCommand, NO_OUTPUT
------------------------------------------------------------------------------------
-----code:end
------------------------------------------------------------------------------------


Result Messages:
Processed 160 pages for database 'DB_Test', file 'DB_Test' on file 1.
Processed 1 pages for database 'DB_Test', file 'DB_Test_log' on file 1.
BACKUP DATABASE successfully processed 161 pages in 0.116 seconds (10.843 MB/sec).