Solved

Simple batch file to copy yyymmdd files to another location.

Posted on 2011-09-03
4
483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 54

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
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…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…

726 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