LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-04-2005, 01:38 PM   #1
tgreiner
LQ Newbie
 
Registered: Jan 2005
Posts: 20

Rep: Reputation: 0
Script questions


How do I write a script that create a backup folder that contains all the files that have been updated after a specific date.

In the days of DOS/Windows I did it in a batch file like this:

.....
echo off

set day=3-22-05

set disk=D:

mkdir update_from_%day%

cd update_from_%day%

mkdir Fib
cd Fib

xcopy %disk%\Fib\*.* /d:%day% /s


cd ..
mkdir FibStr#
cd FibStr#


cd ..

Echo DONE!

.....


This batch would read the subdirectories and only update those that had newer dates. With a similar file structure, how do I do this in Linux?
 
Old 04-04-2005, 01:49 PM   #2
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 48
A more simple way would be to do something like:

Code:
cp -Rudfpb --backup=numbered [sourcedir] [targetdir]
This would copy all the files from [sourcedir] to [targetdir]. If a file by the same name already exists in [targetdir], it will get backed up with prefix ~1~, ~2~, ~3~, and so on. The newest file will always have the same name in [targetdir] as in [sourcedir]. Only files that have been changed from the versions in [targetdir] will get copied.
 
Old 04-04-2005, 01:59 PM   #3
tgreiner
LQ Newbie
 
Registered: Jan 2005
Posts: 20

Original Poster
Rep: Reputation: 0
cp -Rudfpb --backup=numbered [sourcedir] [targetdir]


Thanks, but the problem with this method is that it will not do the same thing. This method will end up copying all the files, not just the updated ones, that's the point of specifying a date in the DOS batch file. I would then have to manually delete the files that are numbered, which beside being a time waster would quickly fill up my hard drive. The data base I am using this for is about 20 GB. Copying files for no reason is not cost/space/time effective.

What I wrote works great in DOS. I just want an equivalent batch file for linux.
 
Old 04-04-2005, 02:11 PM   #4
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 48
Well, if you don't want old versions to be kept, the command becomes:

Code:
cp -Rudfp [sourcedir] [targetdir]
Like I said, only the files that differ from the ones currently in [targetdir] will get copied, i.e. not all the files. That's what u switch in the command is for. See man cp for more detailed information. I use find command to expire the files in [targetdir] if they are older than 14 days, i.e. the files will get refreshed (or removed if they no loger exist in [sourcedir]) every two weeks and the maximum age for backup files is two weeks:

Code:
find [targetdir] -atime +14 -exec rm {} \;
cp -Rudfpb --backup=numbered [sourcedir] [targetdir]
This of course causes the files that have been in the backup for more than two weeks to be re-copied also, so it might not be what you want. To expire only backups, use something like:

Code:
find [targetdir] -name *.~?~ -atime +14 -exec rm {} \;
If you choose to use the expire method, consider that you are dealing with removing files here. BE CAREFUL! and test your script before relying on it.

Last edited by alienDog; 04-04-2005 at 02:17 PM.
 
Old 04-04-2005, 02:24 PM   #5
tgreiner
LQ Newbie
 
Registered: Jan 2005
Posts: 20

Original Poster
Rep: Reputation: 0
I guess I'm not making myself clear.

Targetdir should be empty to begin with (ideally it should be created by the batch file). Only files that are newer than the specified date should be copied into targetdir. There will be no "old versions" of the files anywhere on the harddisk, either before or after the batch is run. I want to be able to copy only the files that have been modified after a certain date, while preserving their file directory locations.


What you have given me is a way to synch up two versions of series of files. That's a different problem.
 
Old 04-04-2005, 02:34 PM   #6
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 48
cp Doesn't support copying files according to their modification times, so the you will still have to do it with find command.

Code:
$day="`date +%d%m%y`"
mkdir update_from_$day
find [sourcedir] -mtime [-days] -exec cp -Rd {} update_from_$day \;
-mtime might also support specifying a date instead of a number of days, I'm not sure though. Study: man date, man find, man cp.

--Edit--

Here $day is set to the current day. Change it if you like.

Last edited by alienDog; 04-04-2005 at 02:43 PM.
 
Old 04-05-2005, 01:00 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
There is also a --after-date=DATE option for 'tar' that you could use to produce a tarball of new files, instead of copying files to a directory.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script questions, please help Joey^s Programming 4 11-29-2004 02:53 AM
script questions shaoshuai97 Linux - Certification 2 10-12-2004 10:00 AM
two shell script questions tutwabee Linux - General 4 08-10-2004 06:03 PM
Some questions about a rc.firewall script matruschka Linux - Security 3 08-01-2004 08:47 AM
Shell Script Questions Col Panic Linux - General 13 10-12-2003 12:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration