Solved

Simple batch file to copy yyymmdd files to another location.

Posted on 2011-09-03
4
478 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 52

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I was supporting a handful of Windows 2008 (non-R2) 2 node clusters with shared quorum disks. Some had SQL 2008 installed and some were just a vendor application that we supported. For the purposes of this article it doesn’t really matter which so w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

939 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

6 Experts available now in Live!

Get 1:1 Help Now