To track any modification in a specfic file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To track any modification in a specfic file
# 1  
Old 06-29-2010
To track any modification in a specfic file

Hi All,

I am a pretty new to programming or scripting. Please help me in my below query.

I want to write a script which can track a file for any kind of modification and if there is any modification then it should move that file or i should say backup that file to another server.

Please help me in writing this script.

Thanks and regards.
# 2  
Old 06-29-2010
so, basically, you want first to check

if backup_of_file exists on the server
then
if diff -q fileS
then
backup file
fi
fi
# 3  
Old 06-29-2010
Code:
#!/bin/sh

file="/path/to/file"
destdir="/dest/dir"

if [ ! -e $file ]; then
echo "file $file not present"
exit 1;
fi

l=$(stat -f %Sm $file)

while true; do
 c=$(stat -f %Sm $file 2>/dev/null)
  if [ "$l" != "$c" ]; then
   mv "$file" $destdir 2>/dev/null
  fi
 sleep 5
  if [ ! -e $file ]; then
   echo "file $file not present, waiting for $file..."
  fi
done

# 4  
Old 06-29-2010
Quote:
Originally Posted by daPeach
so, basically, you want first to check

if backup_of_file exists on the server
then
if diff -q fileS
then
backup file
fi
fi
Not exactly.. actually file would be always there but I want to copy it only when somebody change it. And one more thing I need to copy file on different server.
# 5  
Old 06-29-2010
you need the "rsync" utility. It can do exactly what you want.
# 6  
Old 06-29-2010
Quote:
Originally Posted by a_programmer
you need the "rsync" utility. It can do exactly what you want.
sorry if it sounds a stupid query but I am from networking so dont know much about unix stuff.
How rsync would work, will it be running in background tracking and watching one file for any kind of modification?
# 7  
Old 06-29-2010
You can download it from here:

rsync

You will have to go through the documentation to tweak for your exact requirements. But here is a sample command that I use to keep my profile and settings in sync across multiple home directories (across multiple servers):

Code:
##
## Update the following variables as per your settings.
##
## Path of the rsync utility on the remote server
RSYNC_PATH=/path/to/rsync
## List of files to be backed up every time something changes
FILE_LIST="$home/.profile $home/.functions $home/.vimrc $home/install"
## Remote server
REMOTE_SERVER=my_backup_server
## Remote path where to put the backup files
REMOTE_BACKUP_PATH=/my/backup/path

rsync --links --recursive --rsync-path=$RSYNC_PATH --verbose \
     $FILE_LIST $REMOTE_SERVER:$REMOTE_BACKUP_PATH

Note that before this can be used, you will have to make sure that rsync is installed on both local and the remote server.

You can put this in a script and schedule it in the cron to run on a set frequency based on how often you expect your files to get changed.

~A Programmer

Moderator's Comments:
Mod Comment Removed self-promotion. Put your personal homepage in your public profile, not as a signature

Last edited by pludi; 06-30-2010 at 01:51 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Some trace file to track timings of a process

I will initiate a process from Server1 and the flow is as follow Server1 --> Web server --> Application server --> DB Server Note all seperate unix servers. Now I need to put a trace for that process to track the timings from each server. Like the below trace should be there: ... (5 Replies)
Discussion started by: saraperu
5 Replies

2. UNIX for Dummies Questions & Answers

Trying To Write File Contents To Specfic .csv Cell

Hi, I'm attempting to write the entire contents of a file to a specific .csv cell. So far have only a nawk one liner that will write a value into a specific .csv cell. Trying to use man page but can't seem to get any farther. Any help would be appreciated. nawk -v r=2 -v c=3 -v val=5 -F,... (7 Replies)
Discussion started by: jimmyf
7 Replies

3. Shell Programming and Scripting

track one file and copy to another server

Hello Friends, I am new to scripting, please guide me in below problem: I want to track a file, if anyone make any changes to that file then script should copy it to another server. my whole purpose is to backup a critical file automatically when any change is done to that specific... (1 Reply)
Discussion started by: makkar4u
1 Replies

4. Shell Programming and Scripting

track the errors in log file

OS: SuSE Linux Enterprise Server 10 Goal: To track the errors in log file, If they exits users will be notify by email. We have a script below: SrchKey="SRVE0242I:" LogFile=/PATHtemOut.log MailTo="DN@mail.com http:// ! -f PATH/alert.last && touch PATH/alert.last egrep $SrchKey $LogFile... (3 Replies)
Discussion started by: sdhn1900
3 Replies

5. UNIX for Dummies Questions & Answers

How to change the file modification time of a file on nfs mount point

Hi I am accessing a file on nfs mounted device, after completing using of the file, i am tring to restore the access time and modification times of the file. So i got the previous modified time of the file using stat() function and trying to set the date and time for the file, To set these... (6 Replies)
Discussion started by: deepthi.s
6 Replies

6. UNIX for Dummies Questions & Answers

How to track the modification history on file in unix

How do we track the modification history on a file in UNIX. IS there any command or any script that we could run. Many Thanks (5 Replies)
Discussion started by: RSPDaemon
5 Replies

7. UNIX for Dummies Questions & Answers

Possible to track FTP user last login? Last and Finger don't track them.

Like the topic says, does anyone know if it is possible to check to see when an FTP only user has logged in? Because the shell is /bin/false and they are only using FTP to access the system doing a "finger" or "last" it says they have never logged in. Is there a way to see when ftp users log in... (1 Reply)
Discussion started by: LordJezo
1 Replies

8. UNIX for Dummies Questions & Answers

add a line to a specfic point in a file

Hello, Simple if you know how, which I don't.... I would like to add a line to file file.env file.env consists of the following : line 1-20 here .. # Begin customizations $AVARIABLE/dir1/dir2;export $AVARIABLE # End customizations eof I would like to use a shell script to... (2 Replies)
Discussion started by: d__browne
2 Replies

9. Shell Programming and Scripting

add a line to a specfic point in a file

Hello, Simple if you know how, which I don't.... I would like to add a line to file file.env file.env consists of the following : line 1-20 here .. # Begin customizations $AVARIABLE/dir1/dir2;export $AVARIABLE # End customizations eof I would like to use a shell script to... (4 Replies)
Discussion started by: d__browne
4 Replies
Login or Register to Ask a Question