title: Making remote database backup into local folder
body: <p>I call this T-SQL script against a remote SQL Server database from a batch file in my local machine:</p>
  
  <p><code>MyBatchFile.bat</code>:</p>
  
  <pre><code>"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" 
        -S RemoteIPaddress -U username -P userPassword -i D:\MyBackups\backup_db.sql
  </code></pre>
  
  <p><code>backup_db.sql</code>:</p>
  
  <pre><code>declare @backupfile nvarchar(255)
  
  set @backupfile = N'D:\MyLocalBackups\' +
                    CONVERT(varchar(4), datepart(yy, getdate())) +
                    right('0' + CONVERT(varchar(2), datepart(mm, getdate())),2) +
                    right('0' + CONVERT(varchar(2), datepart(dd, getdate())),2) +
                    N'_' +
                    right('0' + CONVERT(varchar(2), datepart(hour, getdate())),2) +
                    right('0' + CONVERT(varchar(2), datepart(minute, getdate())),2) +
                    N'_MyDbBackup.bak'
  
  BACKUP DATABASE MyDatabase TO DISK = @backupfile
  WITH NOFORMAT, INIT,  NAME = N'MyDatabase Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
  
  GO
  </code></pre>
  
  <p>And I want the backup to be placed into my local machine from where I execute it but it seems it is trying to do the backup and place it into the remote machine instead so I get below error:</p>
  
  <blockquote>
    <p>Cannot open backup device 'D:\MyLocalBackups\20131011_1200_MyDbBackup.bak'. Operating system error 21(The device is not ready.).</p>
  </blockquote>
  
  <p>How can I do this?</p>
  
  <p>Note that <code>.bat</code> file and <code>.sql</code> file are placed in my local machines, not in the remote.</p>