Click to See Complete Forum and Search --> : Bash backup script
`k|in3d
12-11-2003, 11:52 PM
I'm just starting to learn bash scripting, but I'm in need a script to backup some log files. I basically want to back up my log files in /var/log, stuff them into a tar.gz file, copy them over to a Win2K box on my network then delete all the log files/tar.gz file. Does this make sense? Is it possible? I'm sure there are bash scripts for this task, but I can't seem to find any. Maybe a push or website in the right direction could get me started...Thanks!
scinerd
12-12-2003, 12:31 AM
You should be able to do most if not all of this with the log rotate script that's on your system what ever that is. but if needed something like this should work.
#!/bin/bash
DATE=`date +%b%d%y`
tar -zcBpf log-$DATE.tar.gz ./log
cd log
rm -rf ./*
terribleRobbo
12-12-2003, 12:32 PM
VERY IMPORTANT.
Change 'cd log' to 'cd /var/log'.
If you execute the script as it is now from, say, '/', then it'll try to change to the '/log' dir, fail, then 'rm -rf ./', ie. delete everything on the disk. Not good.
(Edit - Also, make sure you _really_ want to recursively delete all the files in /var/log).
goon12
12-12-2003, 01:01 PM
I recently stumbled on this site, if you are new to bash scripting this site is great:
http://tille.soti.org/training/bash/
-goon12
scinerd
12-12-2003, 01:55 PM
sorry should have noted that good catch terribleRobbo. This is a little better:
#!/bin/bash
DATE=`date +%b%d%y`
cd /var || echo "exit due to error" && exit
tar -zcBpf log-$DATE.tar.gz ./log
cd /var/log && rm -rf ./*
The script moves to var first. Then tars up the logs. Then cd /var/log but if it fails it doesn't run the rm. I would change var to a test directory and runs this as a non root. Once you feel it works the way you want put it in place.
Sepero
12-12-2003, 02:15 PM
If I may improve further:
#!/bin/bash
DATE=`date +%b%d%y`
cd /var/log || echo "/var/log/ does not exist" ; exit
tar -zcBpf log-$DATE.tar.gz . || echo "tar: Backup error" ; exit
rm -rf ./* || echo "rm: Backup error" ; exit
echo "Backup successful" ; exit