Solved

Simple batch file to copy yyymmdd files to another location.

Posted on 2011-09-03
4
479 Views
Last Modified: 2012-05-12
Hi,

We have 30 days archive of daily database dump backup e.g. backup.dmp.yyymmdd

We only wish to copy today's dump and keep the last 3 days in the offsite backup server e.g.
1. Delete oldest (-3) backup file.
2. copy today's backup c:\backup\backup.dmp.yyyymmdd to DR site (\\DRserver\backup\backup.dmp.yyymmdd)

Can anyone provide this batch file? It will run on a Windows 2008 STD server without powershell. I will then use Task scheduler to run the batch file on a daily schedule.

Thanks.
0
Comment
Question by:sidartra
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
dsacker earned 350 total points
ID: 36478104
I do the same thing with daily backups, but I drop the date portion of the file name on my target server, so that I can overlay it with the current backup each night. If that's cool with you, here are a few of my .BAT files that accomplish that:

Get_Last_FileName.bat - I call this to get the last backup in a directory. It passes back two things: the path and the filename. They are separated so that you can work with them easily.
@echo off

    if "%1" == "" goto Exit

:GetPath

    for /f "delims=~" %%a in ('echo %1') do set zpath=%%~dpa
    if "%zpath:~-1%" == "\" set zpath=%zpath:~0,-1%

:GetLastFile

    if not exist %1 (
        for /f "delims=~" %%a in ('echo %1') do set zfile=%%~nxa
        goto Display
    )

    for /f "delims=\ tokens=1-8" %%a in ('dir /b /o:-d %1') do (
        set zfile=%%a
        goto Display
    )

:Display

    echo %zpath% %zfile%

:Exit

Open in new window


So, in your code, you simply call it as follows:

for /f "tokens=1-2" %%a in ('call Get_Last_FileName.bat %1') do (
    set zfromsvr=%%a
    set zfromfile=%%b
)

set zfrom=%%a\%%b

Open in new window


This example gives me my source server, source file, and (optionally, if I need to work with both) as you can see, I put them together for ease of use.

You may not prefer this direction, but if you like it and wish to proceed further, I'll share more.
0
 
LVL 20

Assisted Solution

by:dsacker
dsacker earned 350 total points
ID: 36478179
If you're wanting to keep the last three days on disk, then rather than deleting the oldest 3 files, wouldn't you rather want to keep ONLY the latest three files?

Deleting the oldest three backups is also pretty easy, using a FOR loop. You would actually create a temporary .BAT file with the following:
@echo off
setlocal enabledelayedexpansion
 
set /A Counter=1

set TmpFile=%TMP%\deletefiles.bat
echo @echo off> %TmpFile%
echo if not exist c:\backup\Temp md c:\backup\Temp>> %TmpFile
 
for /f %%f in ('dir /b /o:-d c:\backup\backup.dmp.*') do (
	echo move /y c:\backup\%%f c:\backup\Temp\. >> %TmpFile%
	if !Counter! == 3 goto FinishBatFile
	set /A Counter+=1
)

:FinishBatFile

echo del /y c:\backup\backup.dmp.* >> %TmpFile%
echo move /y c:\backup\Temp\*.* . >> %TmpFile%
echo rd c:\backup\Temp >> %TmpFile%

type %TmpFile%
rem call %TmpFile%

Open in new window


I commented out the actual "call", and used a "type" instead, so you can see the resulting .BAT file. Once you've proven it, then you can revert to calling it instead of typing it out.
0
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points
ID: 36479021
I think it could be as simple as this.  Basically gets the list of matching backup files, sorted in reverse date order, and keeps the first one, and then deletes any after the 3rd one.

@echo off
setlocal EnableDelayedExpansion

REM Define from and to directory locations
set BaseDir=c:\backup
set DestDir=\\DRserver\backup

REM Loop through all backup files in basee folder, in reverse
REM chronological order. Newest file gets copied to backup
REM location. The 3 newest files are kept, and older ones are removed.
set i=0
for "tokens=*" %%F in ('dir /a-d /b /o-d "%BaseDir%\backup.dmp.*"') do (
  set /A i+=1
  if !i! EQU 1 copy "%BaseDir%\%%~F" "%DestDir%"
  if !i! GTR 3 del "%BaseDir%\%%~F"
)

Open in new window

~bp
0
 
LVL 2

Assisted Solution

by:atea_bjorn
atea_bjorn earned 50 total points
ID: 36480256
This robocopy line will move all files from the sourcefolder to the destination that is older than 3 days.

robocopy c:\sourcefolder \\destination\folder *.* /MOVE /MINAGE:3

It's no script but a oneliner ;)

Cheers
Björn
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Redirected folders in a windows domain can be quite useful for a number of reasons, one of them being that with redirected application data, you can give users more seamless experience when logging into different workstations.  For example, if a use…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

786 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