SQL xp_cmdshell copy files between servers

JPVoogt picture JPVoogt · Oct 5, 2012 · Viewed 31.4k times · Source

I am trying to move all .zip in a specific folder to another folder. the source folder is located on another server, currently i am using

EXECUTE xp_cmdshell 'copy \\server1\e$\ETL\*.zip \\server2\e$\ETL\'
GO

Which is working if I am logged into both server, but the goal is to automate this process VIA sql server job agent. I have tried

EXECUTE sp_xp_cmdshell_proxy_account 'domain\useracc','pass'
GO
EXECUTE xp_cmdshell 'copy \\server1\e$\ETL\*.zip \\server2\e$\ETL\'
GO

but I am receiving the following error;

An error occurred during the execution of sp_xp_cmdshell_proxy_account. Possible reasons: the provided account was invalid or the '##xp_cmdshell_proxy_account##' credential could not be created. Error code: '0'.

And also not sure if this is my solution. Please help with how I can achieve this. The file names on server1 change name and quantity everyday.

Answer

Pete Carter picture Pete Carter · Oct 5, 2012

I would strongly advise...Do not use xp_cmdshell. It opens up large security wholes in your surface area and makes you vulnerable to attack. xp_cmdshell should be disabled!

Instead, if you want to automate this with server agent you have 2 options. My preference would be to write a simple SSIS package with a file system task and schedule this package with server agent. SSIS is underutilized for this kind of task but is actually pretty good at it.

Alternatively re-write your script to use Server Agent CmdExec job steps. This does not require xp_cmdshell to be enabled and reduces the attack surface.