for loop
I am trying to write a shell script in bash that will back up, compress and restore critical system and user files. Im just a newbie trying to learn Linux.
|
|
Im sorry if my post brought tears to you eyes.
|
you can also read this guide
http://www.tldp.org/LDP/abs/html/index.html |
thanks for the help
I am not understanding this is why im asking. I have books and web resources just thought i could get some help.
|
Please
Ok I wanna use a for loop process the file by reading each line of the file. Use tar to collect these files
/tc/httpd/conf/* /home/* /etc/services /etc/xinetd/* pipe the files to gzip write the output file into the roots hone dir as backup where PID=ProcessID of the running shell script send all the output from the script to a log file in the roots home dir. called backup.log when that completes send the root an email listing the name of the backup file and report that the back up was complete. and thats all i am really stuck its not making sense to me I really need the help!!!!!!!! |
Im still puzzled by what you want to do. Read what file, and what do you want to do with each line.
To do the actual backup, try cd / tar czf backup.tar.gz tc/httpd/conf home etc/services etc/xinetd/ 2> backup.log |
I wrote a shell script that does this same thing, minus the info from the PID, but that is an interesting idea ;)
Instead of cutting and pasting the whole thing here, I could give you a few ideas, and if that doesn't do the trick I'll stick the whole file up for download. This one works with the .bz2 compression, which I found to be much more compressed (although more time consuming). you can do it in any compression you like of course. #******************************* #!/bin/bash #******************************* # #day of month cannot have the padded ( 1) format #it must be (01) format therefore the %m #You can read about the options for "date" in the man pages #and customize it for your app # #now call date variables for file naming #****************************** cyear=`date +'%y'` fyear=`date +'%Y'` wday=`date +'%a'` cdate=`date +'%d'` month=`date +'%m'` company=dba sco=dba dirpath=/mnt/nt/autobackup backuppath=/mnt/central/Default logpath=/var/log/hh file=$company-$wday-$month-$cdate-$fyear.tar.bz2 #variables all set...now back up # echo Creating date file... #this begins the log file building # cd $logpath/$sco echo $backuppath/$company > datefile echo This File created by automated Linux backup program running in cron >> datefile echo -n "Time started: " >> datefile date >> datefile date # echo " *********Starting $sco Backup now*********** " # echo changing directories cd $dirpath/$sco echo Starting backup attempt on $backuppath/$company tar -Icf $company-$wday-$month-$cdate-$fyear.tar.bz2 $backuppath/* |sleep 5 # # Finish building the time stamp in the log file echo -n "Time finished: " >> $logpath/$sco/datefile date >> $logpath/$sco/datefile # this ends log file building # log files are overwritten every time it runs # This last check goes to see if the file/folder we # "think" we backed up is actually there test -e $dirpath/$sco/$file # I forget why this works...but it does! new=$? if [ "$new" = "0" ] then cat $logpath/$sco/datefile | /bin/mail -s "$sco backed up" you@your.com echo Hooray $dirpath/$sco/$file Successfully Created! else echo Backup NOT completed: Go Fix it! /bin/mail -s "Something is wrong with $sco" you@your.com echo Exiting backup prgram for $sco exit fi echo Exiting backup program for $sco..... ##################### This will give you dated .tar.bz2 files in a directory. Looks like: dba-Fri-08-08-2003.tar.bz2 Hope this helps! |
here is the senerio (Im very confused)
Ive only had 6 linux classes and the teacher is not that helpfull. I need to write a script in bash called (rootbackup) that will back-up,compress, and restore critical system and user files. I need to create a file in roots home dir called (critical-files) using VI, In this file, store the fully qualified path and file names of the files to be backed up. The files are /etc/httpd/conf/* /home/* /etc/services /etc/xinetd/* Each feat ure should be on its own line in the script, use a for-loop to process the file by reading each line of the file. Use the (1) tar or (1L) cpio command to collect the files. Pipe the files in to (1) gzip to compress the archive. Write the output file into the roots home dir as backup. {PID} where PID = process ID of the running shell script. Send all output from the shell script to a log file in roots home dir called (backup.log). When the process completes successfully, send root an email reporting that the backup was taken. In the email, list the name of the back-up file. The script should take the name of the configuration file, (critical-files) as a command line parameter. If the file is not given at the command line, Display a help message. |
Well here's a hint.
If you had a file called list that contained this: /etc/httpd/conf/* /home/* /etc/services /etc/xinetd/* You could get the list into a string with cat, like this: list=`cat list` Now $list is the same as the file list. :cool: Then a basic for loop would be: for i in $list; do #Now do whatever you want to do with the file or directory! tar -czvf $i.tar $i gzip $i rm -f $i mv $i $i.bak done |
Am i getting close ???
any suggestions?? #!/bin/bash #rootbackup #This program backs up critical files [ "$#" -ne 1 ] && echo "Usage: rootbackup <input file list>" && exit 1 [ -f backup.[0-9]* ] && rm backup.[0-9]* [ -f /tmp/backupfiles ] && cat /dev/null > /tmp/backupfiles # for i in `cat $1` do find $i depth -print >> /tmp/backupfiles done # cat /tmp/backupfiles | cpio -o 2>backup.log | gzip >> /backup.$$ msg=`echo \`ls backup.[0-9]*\`` echo "back-up file name is $msg" > junk mail root <junk rm junk |
here is the senerio (Im very confused)
Ive only had 6 linux classes and the teacher is not that helpfull. I need to write a script in bash called (rootbackup) that will back-up,compress, and restore critical system and user files. I need to create a file in roots home dir called (critical-files) using VI, In this file, store the fully qualified path and file names of the files to be backed up. The files are /etc/httpd/conf/* /home/* /etc/services /etc/xinetd/* Each feat ure should be on its own line in the script, use a for-loop to process the file by reading each line of the file. Use the (1) tar or (1L) cpio command to collect the files. Pipe the files in to (1) gzip to compress the archive. Write the output file into the roots home dir as backup. {PID} where PID = process ID of the running shell script. Send all output from the shell script to a log file in roots home dir called (backup.log). When the process completes successfully, send root an email reporting that the backup was taken. In the email, list the name of the back-up file. The script should take the name of the configuration file, (critical-files) as a command line parameter. If the file is not given at the command line, Display a help message. ( post #11) Am i getting close ??? any suggestions?? #!/bin/bash #rootbackup #This program backs up critical files [ "$#" -ne 1 ] && echo "Usage: rootbackup <input file list>" && exit 1 [ -f backup.[0-9]* ] && rm backup.[0-9]* [ -f /tmp/backupfiles ] && cat /dev/null > /tmp/backupfiles # for i in `cat $1` do find $i depth -print >> /tmp/backupfiles done # cat /tmp/backupfiles | cpio -o 2>backup.log | gzip >> /backup.$$ msg=`echo \`ls backup.[0-9]*\`` echo "back-up file name is $msg" > junk mail root <junk rm junk |
I really need some help here
|
I'm trying to do this in cygwin bash shell, and sometimes there is stuff missing (like cpio). But it looks to me like it should work. Are you getting errors or what?
|
You can use the cat command as suggested before to read in the file. The file can contain paths to invidual files or whole directories to be passed to tar which appears to work fine when used like below no matter whether it's passed a file or whole directory
files_to_be_archived=`cat $1`; tar -cvzf backup.tar.gz $my_files &>backup_log; tar/gzip seems to work fine without a for loop to read each line and &>backup_log outputs all output from the tar command to backup_log That's the way I'd basically do it, of course with added error check and all the other stuff..blah..blah |
All times are GMT -5. The time now is 10:04 PM. |