How SQL remotely backs up a database to a local
--1, enabling xp_cmdshell Use Masterexec sp_configure ' show advanced options ', 1RECONFIGURE with overrideexec sp_configure ' xp_cmdshell ', 1RECONFIG URE with overrideexec sp_configure "show advanced options", 0RECONFIGURE with OVERRIDE
--2, using variables for backup declare @db sysnamedeclare @dbpath varchar Set @db = ' Shenyin '--file name based on database name + date yyyymmdd+ extension set @dbpath = ' d:\ ' + @db +convert (varchar), GETDATE (), () + '. Bak ')
--3, build a mapping exec master in SQL . xp_cmdshell ' net use Y:\\192.168.0.156\DBBackup"Jindou"/user:192.168.0.156\administrator '/* Description: Y: Is the Map network path corresponding to the local drive letter 192.168.0.156 is my native IP dbbackup is my native shared folder Administrator is the login username "Jindou" is the password for the administrator user * /--4, Backup (@db is the name of the database to be backed up)backup database @db to[email protected] --5, copydeclare @copypath varchar set @copypath = ' Copy' + @dbpath + 'Y: ' Select @copypathexec Master. xp_cmdshell @copypath --6, delete (this sentence can be removed)declare @deletepath varchar set @deletepath = ' del' + @dbpathSelect @deletepathexec Master. xp_cmdshell @deletepath --7, delete map exec master after completion . xp_cmdshell ' net use Y:/delete '
--8, close xp_cmdshell
How SQL remotely backs up a database to a local