LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script questions (https://www.linuxquestions.org/questions/linux-newbie-8/script-questions-309658/)

tgreiner 04-04-2005 01:38 PM

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?

alienDog 04-04-2005 01:49 PM

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.

tgreiner 04-04-2005 01:59 PM

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.

alienDog 04-04-2005 02:11 PM

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.

tgreiner 04-04-2005 02:24 PM

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.

alienDog 04-04-2005 02:34 PM

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.

jschiwal 04-05-2005 01:00 AM

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.


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