[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Simple batch file to copy yyymmdd files to another location.

Posted on 2011-09-03
4
Medium Priority
?
490 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 1400 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 1400 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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 400 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
Suggested Courses

834 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