简体   繁体   中英

How to copy a database backup file to another server in SQL Server Agent job

I realize I'm probably missing something simple, but I can't figure it out.

If I use the following "copy" command in a batch file on the destination server, it works fine. However, if I add it in a step on the SQL Server Agent job that does the backup, I get a syntax error. So, what am I doing wrong? It seems like it might be getting stuck on the "/" on the "/y" parameter, but I do want it to overwrite the existing file.

copy /y "\\DBServer\c$\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\db_daily.bak" "\\DESTINATION\\db\db_daily.bak"

The error is:

Incorrect syntax near '/'.  [SQLSTATE 42000] (Error 102).  The step failed.

I removed the /y altogether and tried everything I can think of. Any help would be much appreciated.

Make sure your step was created as Type "Operating System (Cmd Exec) not as transact sql.

Or use the XP xp_cmdshell

   exec xp_cmdshell 'copy /y "\\DBServer\c$\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\db_daily.bak" "\\DESTINATION\\db\db_daily.bak"' 

Please note you may need to enable the xp_cmdshell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM