Solved

Copy Backup Files

Posted on 2007-11-21
2
229 Views
Last Modified: 2010-03-19
I have a script to copy backup files.

This is what is does:

--Make a new directory to hold last month's backup files
SET @cmd = 'mkdir C:\dest\Archive\' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0), 112)
EXEC master..xp_cmdshell @cmd, NO_OUTPUT

--Loop through the filenames in the temp table and copy them to the new directory
DECLARE @current_dir varchar(255)

DECLARE test_cursor CURSOR FOR
SELECT
current_dir
FROM #test_archive

      
OPEN test_cursor
FETCH test_cursor INTO
@current_dir

WHILE (@@FETCH_STATUS = 0)
BEGIN


     SET @cmd = 'copy' +  @log_current_dir + 'C:\test\PLFullArchive' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0), 112)
      EXEC master..xp_cmdshell @cmd, NO_OUTPUT

      FETCH test_cursor INTO
      @current_dir
END



CLOSE test_cursor
DEALLOCATE test_cursor


The # test_archive temp table contains :

current_dir
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN

For some reason the copy command does not copy the files to the destination.
Is there something I am missing here.
This is part of a larger stored procedure.
You can run the copy command seperately.

Thanks


0
Comment
Question by:sbagireddi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Accepted Solution

by:
Chris Mangus earned 250 total points
ID: 20330529
Print the contents of the @cmd variable and examine it.  I don't think you have spaces where you need them.

i.e. - your output command probably reads:

copyc:\mylogdirC:\test\PLFullArchive...etc

Try:

SET @cmd = 'copy ' +  @log_current_dir + ' C:\test\PLFullArchive' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0), 112)
0
 
LVL 18

Assisted Solution

by:Yveau
Yveau earned 250 total points
ID: 20330572
Run the xp_cmdshell statement without the NO_OUTPUT and you will see what the error is.

My guess is:
'copy' +  @log_current_dir + 'C:\test\PLFullArchive' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0)

will end up like:
copyC:\source\Archive\test_20071029_LOG_flip2.TRNC:\test\PLFullArchive200710

and you mean to have something like:
copy C:\source\Archive\test_20071029_LOG_flip2.TRN C:\dest\Archive\200710

so you are looking for:
SET @cmd = 'copy ' +  @log_current_dir + ' C:\dest\Archive' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0), 112)

Hope this helps ...
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question