Solved

Copy Backup Files

Posted on 2007-11-21
2
188 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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Review MS SQL cluster diagram 9 79
SQL Encryption question 2 49
How to use TOP 1 in a T-SQL sub-query? 14 41
Help with simplifying SQL 6 47
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now