Solved

Simple batch file to copy yyymmdd files to another location.

Posted on 2011-09-03
4
477 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 51

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

Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

Join & Write a Comment

Suggested Solutions

I had a question today where the user wanted to know how to delete an SSL Certificate, so I thought that I would quickly add this How to! Article for your reference. WHY WOULD YOU WANT TO DELETE A CERTIFICATE? 1. If an incorrect certificate was …
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

707 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