Hi,
Any one please help me out.

Server: In Windows 2008, am having the database file (*.bak ) in the following locations

[B]C:\program files\Microsoft Sql server\backup\test.bak[/B]

I needs to copy, the test.bak file in my local machine (windows xp) in the following path

C:\backup

Using batch script, i needs to complete this task and then i needs to schedule it out. please solve my issue. Thanks

Dani AI

Generated

Run the batch on the client (destination) machine and schedule it there. The scheduled task must run under a Windows account that has read access to the server share. Using a UNC share (\Server\Share...) is more reliable than mapping drives inside a scheduled task, and it's safer to create a normal share with read permission instead of using the admin c$ share unless you really need it. As hinted, quoting paths with spaces is important.

Your date problem comes from relying on %DATE% which is locale dependent (you reported "3/8/2012"). A more reliable method is to query Windows for an ISO timestamp and build YYYY-MM-DD before copying. Example .bat (to run as a scheduled task on the client):

@echo off
setlocal EnableDelayedExpansion

set "SRC=\\MyServer\SQLBackups\test.bak"
set "DST=C:\backup"

rem create destination if missing
if not exist "%DST%" mkdir "%DST%"

rem get locale-independent date (YYYY-MM-DD)
for /f "tokens=2 delims==." %%A in ('wmic os get LocalDateTime /value') do set "dt=%%A"
set "YYYY=!dt:~0,4!"
set "MM=!dt:~4,2!"
set "DD=!dt:~6,2!"
set "DATESUFF=!YYYY!-!MM!-!DD!"

rem build name and copy
for %%F in ("%SRC%") do (
  set "NAME=%%~nF"
  set "EXT=%%~xF"
)
copy "%SRC%" "%DST%\!NAME!_!DATESUFF!!EXT%"

endlocal

Scheduling and troubleshooting notes: schedule the .bat on the client with Task Scheduler using the domain/local account that has share access (choose "Run whether user is logged on or not" and provide credentials). If you rely on mapped drives, map them inside the script (NET USE /PERSISTENT:NO) because mapped drives aren't available in different sessions. Ensure SQL Server finished writing the .bak before copying (race conditions produce corrupt copies) — ideally have the backup job produce a timestamped file on the server or run the copy after the backup job completes. If files are large or you need resume/retry behavior, use robocopy or add size-stability checks and logging to the script.

Recommended Answers

All 6 Replies

Hello,

This should work provided you have access to the file and directory listed:

copy "\\ServerNameOrIP\c$\Program Files\Microsoft Sql server\backup\test.bak" C:\backup\

Hi,
Thanks for your script.From where,i should run this?In the sense, i should run in the CLIENT MACHINE(destination(client window)machine from the server window machine) using DOS command.

Hi,

what do you meant by this line?
This should work provided you have access to the file and directory listed:

Hi,
thanks its working, but while doing copying i needs to append CURRENT DATE over the file.

I tries this, is not working:

[B]set folderdate=%date:~0,4%-%date:~5,2%-%date:~8,2%
COPY "\\server\c$\DB.bak" C:\temp\DB.bak%folderdate%[/B]

please solve this issue thanks.

Hello,
am not get you. First, am running this script in dos command and then making as a batch file and keep doing scheduling.

my system date format is it like 3/8/2012

While on doing copying, i needs to appending current system date towards the file. plz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.