Batch Script - Delete Files Older then #Date# 10 replies

  • 1
  • 2

Please wait...

Sgt. D. Pilla

Uber Geek

50 XP

23rd October 2007

0 Uploads

3,473 Posts

0 Threads

#1 9 years ago

Hey, I have a batch script for a client that copys specific data and places it on an EHDD in a folder that is created with the date the data was copied. As below... @ECHO OFF TITLE Backing Up Data... ECHO Mapping network drive... ECHO Deleting 'O' Network Drive... net use O: /delete /y ECHO. ECHO Mapping 'O' Network Drive... net use O: "\\FCA01\FCA Data" /PERSISTENT:YES ECHO. ECHO Creating Directory Structure... for /f "tokens=1-4 delims=/- " %%a in ('date /t') do set XDate=%%a-%%b-%%c-%%d ECHO Directory will be named "%XDate%" MD E:\Backups\%Xdate% ATTRIB E:\%Xdate% -s -h ECHO. ECHO Backing Up Data... xcopy "O:\" "E:\Backups\%Xdate%" /E /V /C /I /F /R /Y /Z ATTRIB E:\%Xdate% -s -h IF ERRORLEVEL 1 goto Completed IF ERRORLEVEL 0 goto failed :failed ECHO Copy Failed ECHO Exiting in 10 seconds... ping localhost -n 10 >nul exit :Completed ECHO Copy Successful ECHO Exiting in 10 seconds... ping localhost -n 10 >nul exit

The issue arrises when the destination is full it stops backing up. I want to moddify the script to first delete any folders/files that are older then a given time period. So say...2 weeks.

The script would thus keep all files/folders that are 2 weeks or newer, anything older then 2 weeks is deleted to free up space. I realise this is primative, but the client is only very small and can't afford an SMB backup solution as of yet (Such as BES). Any ideas on this? Google hasn't been at all helpful on the matter...




ConstanceJill

Huh yeah, whatever ^^

38,763 XP

6th December 2006

0 Uploads

3,246 Posts

1 Threads

#2 9 years ago

Hello ^^

As far as I know there is no function to make calculations, and even less calcultations on dates, in the DOS standard commands.

You may either look for some DOS port of unix commands (*maybe* eval could do it ?) or use dedicated programs.

I know at least PKZIP's command line version has a "-t" switch to keep only files for which the date is newer or equal to what you specify... though I don't think it can by itself calculate a date starting from current.




Freyr VIP Member

A2Files Staff

46,875 XP

6th February 2005

11 Uploads

4,275 Posts

0 Threads

#3 9 years ago
Sgt. D. Pilla;4953886 I realise this is primative, but the client is only very small and can't afford an SMB backup solution as of yet (Such as BES). Any ideas on this? Google hasn't been at all helpful on the matter...

Why pray tell are you using a batch file for backups instead of NTBACKUP? In case you hadn't heard, it's the utility microsoft provided so we could take the batch file kludge out and shoot it.

Batch files for backups only really have any place on 9x platforms, and you must have replaced any 9x platforms by now. Your not verifying that your backups actually are in a usable state, and that all but negates the point of doing them.

If you absolutely MUST continue with this method, grab your companies copy of Windows Command - Line; Administrators Pocket Consultant. You should have a copy in your departments library, everywhere I have worked has had. If you don't have a copy in your library and you can't talk the company into buying it then i'd buy it yourself. It's a useful little book and you'll probably find the first couple of chapters interesting reading.

Personally, If absolutely forced to use batch scripting for backups I wouldn't name the backups by date. Go for being a little more simple, and number your folders 1-14.

Shedule your batch file to run once a day. Delete folder #14, then rename #13 to #14, #12 to #13, #11 to #12, etc and then create a new #1 folder for the new backup. It's messy, but simple and effective. Just make sure you safeguard against your users running your batch file manually, or its going to give you headaches.

This however does NOT get around the problems of the backup not being verified and still on storage media attached to the server. Its not going to be much good if the place burns down, the hardware fails or they get hacked and someone deletes it. In other words, its pretty close to useless!




Sgt. D. Pilla

Uber Geek

50 XP

23rd October 2007

0 Uploads

3,473 Posts

0 Threads

#4 9 years ago

Why pray tell are you using a batch file for backups instead of NTBACKUP? In case you hadn't heard, it's the utility microsoft provided so we could take the batch file kludge out and shoot it.

Batch files for backups only really have any place on 9x platforms, and you must have replaced any 9x platforms by now. Your not verifying that your backups actually are in a usable state, and that all but negates the point of doing them.

We have many clients using NT Backup, and many of them move to BES mearly days later - it doesn't work. A flat file copy and paste backup system is all that is needed and does provide verification of the backup. Besides, this is a client side backup, not server side, thus no NTBackup. Our Companys 'libary' is purerly online and 'own material' based, we are a paper less office. We will continue to use date named folders so we know the date of when to restore from at first glance, this backup is setup in such a way that the client end users are able to go "This date > This File" They do run BES backups server side which we manage, but this backup is strictly for their ease of use after a multiple backup failure (Tapes and Revs both failed to restore from successful backups, could only restore from a 3 month old backup) This backup is run from a workstation as a secondary EOM backup which has been scheduled to run on the last day of each month. Thus if we get hacked, big deal (Actually they couldn't get to this server anyway...it's not attached to the internet, puerly file and print VM). So if the place burns down - we have this flat file backup and the tapes and revs, if we get hacked, big deal, they aint getting to the servers data, if it gets deleted, we restore from either tapes or revs or this EOM backup. With this backup in place as a flat file, end user friendly, workstation side backup - they are quite immune to data lose. All good, found a way to delete files/folders of a certain age with command prompt, long, and ugly, but it works, bearly.




ConstanceJill

Huh yeah, whatever ^^

38,763 XP

6th December 2006

0 Uploads

3,246 Posts

1 Threads

#5 9 years ago
Sgt. D. Pilla;4954833Besides, this is a client side backup, not server side, thus no NTBackup.

NTBackup exists in Windows XP, at least in XP Pro : I have it on my machine here at work.




Bs|Archaon

I would die without GF

50 XP

15th March 2006

0 Uploads

5,910 Posts

0 Threads

#6 9 years ago

It should be available on XP and 2000 regardless, plus Vista has it's own equivalent, whatever it's called now...




ConstanceJill

Huh yeah, whatever ^^

38,763 XP

6th December 2006

0 Uploads

3,246 Posts

1 Threads

#7 9 years ago

NTBackup doesn't seem to exist on XP Home OEM, though. I suppose you won't have Home versions in a company, but still might be possible...




Sgt. D. Pilla

Uber Geek

50 XP

23rd October 2007

0 Uploads

3,473 Posts

0 Threads

#8 9 years ago
It should be available on XP and 2000 regardless, plus Vista has it's own equivalent, whatever it's called now...

Doesn't allow selective backups, its either all or nothing. We don't want all lol. Nah, all the new OS backup solutions are shit.

NTBackup doesn't seem to exist on XP Home OEM, though. I suppose you won't have Home versions in a company, but still might be possible...

Nah its Pro... But sus all the same, it doesn't exist ~_^ Even searching for the NTBackup exe reveals nothing.




Freyr VIP Member

A2Files Staff

46,875 XP

6th February 2005

11 Uploads

4,275 Posts

0 Threads

#9 9 years ago

Sgt. D. Pilla;4955508Doesn't allow selective backups, its either all or nothing. We don't want all lol. Nah, all the new OS backup solutions are shit.[/QUOTE]

Yes, it does allow for selective backups. I think you've been misled by your colleauges there a bit. Have a look, you might be surprised what you can do with it.

Start, run "NTBACKUP". Select the top box, "Backup Wizard (Advanced)". Press next, then select the 2nd radio button. "Back up selected files, drives or network data".

Then you get the option of where to save the backup file, and options for making the backup normal, a strait copy or incremental, differential or daily.

It's not as good a solution as a proper backup program ie veritas, but its a heck of a lot better than nothing. I'd get a little more familiar with it if I were you, it's a useful tool if you don't have veritas/symantex backup available which is sometimes the case if your dealing with cheapskates that are incapable of appreciating how important backups are.

[QUOTE=Sgt. D. Pilla;4955508] Nah its Pro... But sus all the same, it doesn't exist ~_^ Even searching for the NTBackup exe reveals nothing.

ntbackup is part of the defualt windows install, but it can be removed during an advanced setup if someone felt the burning need to save a megabyte when doing your install. You can do a install from the XP CD, or if your OEM didn't provide a disc then its easy to get a copy of it online.




Revenge VIP Member

Shizzle my nizzle

117,165 XP

28th July 2004

0 Uploads

10,354 Posts

0 Threads

#10 9 years ago

DOS wasn't designed for date calculations. This would be better served through the use of a programming language such as C or Java over the use of a scripting language. You'll probably save yourself hassle by googling something like 'c backup script' and tinkering it to work for your specific needs.




  • 1
  • 2