I’ve just created/updated some useful Shell scripts on my UK server. One performs a mysqldump backup of a database and emails a compressed copy at regular intervals. The second script rotates transfer and error logs each month.
For the database backup email you will need to install a copy of the email client “mutt”. Just run: yum install mutt to get this added.
You can use CRON to run all these tasks at regular intervals.
#!/usr/bin/perl # # Run using: perl /path/to/file/logshrink.pl Jul 2011 $month = "$ARGV[0]"; $year = "$ARGV[1]"; if (!$month || !$year) { @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); $time = time - (1 * 86400); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time); $year=$year+1900; $month=$months[$mon]; } chdir "/home/logs"; open (OUTPUT,">logshrink.output"); @array = `find /home/*/transfer[-_]log`; foreach $file (@array) { $count=0; $flag=0; print OUTPUT $file; chop $file; open (INPUT,"<$file"); open (REST,">$file.rest"); while ($string=<INPUT>) { if ($string =~ /$month\/$year/o) { if ($flag==0) { open (MONTH,">$file.$month"); $flag=1; } print MONTH $string; $count++; } elsif ($string =~ /\/$year/) { print REST $string; } } close INPUT; close REST; system "mv -f $file.rest $file"; if ($flag==1) { close MONTH; system "gzip -fq $file.$month"; } } close OUTPUT; system "/usr/local/apache/bin/apachectl restart";
#!/usr/bin/perl # # Run using: perl /path/to/file/errorlogshrink.pl Jul 2011 $month = "$ARGV[0]"; $year = "$ARGV[1]"; if (!$month || !$year) { @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); $time = time - (1 * 86400); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time); $year=$year+1900; $month=$months[$mon]; } chdir "/home/logs"; open (OUTPUT,">errorlogshrink.output"); @array = `find /home/*/error[-_]log`; foreach $file (@array) { $count=0; $flag=0; print OUTPUT $file; chop $file; open (INPUT,"<$file"); open (REST,">$file.rest"); while ($string=<INPUT>) { if ($string =~ /$month \d{2} \d{2}:\d{2}:\d{2} $year/o) { if ($flag==0) { open (MONTH,">$file.$month"); $flag=1; } print MONTH $string; $count++; } elsif ($string =~ /\/$year/) { print REST $string; } } close INPUT; close REST; system "mv -f $file.rest $file"; if ($flag==1) { close MONTH; system "gzip -fq $file.$month"; } } close OUTPUT; system "/usr/local/apache/bin/apachectl restart";
#!/bin/sh SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin email=$1; name=database_backup.sql; attachment_name=database_backup_$(date +%Y%m%d).gz; backup_dir=/home/site; mysqldump --user=user --password=pass123 dbname > $backup_dir/$name; sleep 2; rm -f $backup_dir/$name.gz; gzip $backup_dir/$name; mv $backup_dir/$name.gz $backup_dir/$attachment_name; echo -e "Hi!\n\nThis is the automated SQL dump of your database on $(date '+%m/%d/%Y %T').\n\nBest wishes, \n\nThe Server" | mutt -a $backup_dir/$attachment_name -s "SQL Backup $(date +%m/%d/%Y)" $email; mv -f $backup_dir/$attachment_name $backup_dir/$name.gz; rm -f $backup_dir/$name;
I recommend running the transfer-log backup on the first day of the month. The error log two weeks into the month. The SQL backup once a week. You may need to edit the date calculations (<days> * 86400) in the log rotation scripts.
To use the shrink scripts add Jul 2010 after the script name and for the database dump to email script add the email address after the script name.
Last updated on