Some Useful Scripts for bsd

tsiou

Verified User
Joined
Sep 15, 2006
Messages
236
Location
Larisa, Greece
Hi,
I want to share some scripts i use for backup/maintenance in my 6.2 freebsd box (i believe you can use it without any mods in any version as it doesnt use any version specific command)

1. Backups
DA's backups are ok but you may to have your own too. I get mysql backups once a day, my whole system ghost like once a month and i rotate them so i have always 100 mysql backups to save space.

---------------------------------mysql shell script ------------------
todayis=`date "+%Y-%m-%d"`
tar -cpf /disk/mysql-backup/$todayis-mysql.tar /home/mysql && \
gzip /disk/mysql-backup/$todayis-mysql.tar

a=`ls -la /disk/mysql-backup | grep "-" |wc -l`
b=100
c=`expr $a - $b`

if [ $c -gt 0 ]
then
ls -la /disk/mysql-backup |head -$c |awk '{print $9}' |grep "-"> /tmp/backup1
for i in `cat /tmp/backup1`
do
rm -rf /disk/mysql-backup/$i
done
fi


echo "Mysql backup in /disk/mysql-backup" |mailx -s "Daily MySQL Backup" root
----------------------------------------------------------------

you can change the path to put the backup (mine in /disk/mysql-backup) and how many days to keep in disk (b=100)


-------------------------system backup shell script----------------------
todayis=`date "+%Y-%m-%d"`
mkdir /disk/custom-backup/$todayis
mkdir /disk/custom-backup/$todayis/home

tar -cpf /disk/custom-backup/$todayis/etc.tar /etc && \
tar -cpf /disk/custom-backup/$todayis/var-mail.tar /var/mail && \
tar -cpf /disk/custom-backup/$todayis/var-named.tar /var/named && \
tar -cpf /disk/custom-backup/$todayis/var-spool.tar /var/spool && \
tar -cpf /disk/custom-backup/$todayis/var-www.tar /var/www && \
tar -cpf /disk/custom-backup/$todayis/usr.tar /usr && \
tar -cpf /disk/custom-backup/$todayis/log.tar /var/log && \

for i in `ls /home|grep -v tmp |grep -v "quota*" |grep -v ".snap"`
do
tar -cpf /disk/custom-backup/$todayis/home/$i.tar /home/$i
done
gzip /disk/custom-backup/$todayis/*.tar
gzip /disk/custom-backup/$todayis/home/*.tar

a=`ls -la /disk/custom-backup | wc -l`
b=10
c=`expr $a - $b`

if [ $c -gt 0 ]
then
ls -la /disk/custom-backup |head -$c |awk '{print $9}' |grep "-"> /tmp/backup
for i in `cat /tmp/backup`
do
rm -rf /disk/custom-backup/$i
done
fi


echo "Main backup in /disk/custom-backup" |mailx -s "Weekly Main System Backup" root
----------------------------------------------------------------------

Here i have a backup for the main system directories and i'm keeping a 10 week time backup because of the many gb that it reserves

------------------------DAs backup shell script------------------------
a=`ls -la /disk/backup | wc -l`
b=4
c=`expr $a - $b`

if [ $c -gt 0 ]
then
ls -la /disk/backup |head -$c |awk '{print $9}' |grep "-"> /tmp/backup2
for i in `cat /tmp/backup2`
do
rm -rf /disk/backup/$i
done
fi


echo "DA files deleted" |mailx -s "Weekly DA Files Delete" root
----------------------------------------------------------------------

DA's backup resides in /disk/backup so i just keep the last 4 backup files (many of the DA's backup files are in system backup too)

-----------------ghost like backup-----------------------------
dd if=/dev/da0s1 bs=512 count=1 > /disk/bz2-backup/mbr

dd if=/dev/da0s1a bs=512 | bzip2 > /disk/bz2-backup/slash.bz2
sleep 240
dd if=/dev/da1s1e bs=512 | bzip2 > /disk/bz2-backup/home.bz2
sleep 240
dd if=/dev/da0s1d bs=512 | bzip2 > /disk/bz2-backup/usr.bz2
sleep 240
dd if=/dev/da1s1d bs=512 | bzip2 > /disk/bz2-backup/var.bz2


echo "Backup in /disk/bz2-backup/" |mailx -s "Monthly Full System Backup" root
----------------------------------------------------------------------
da0s1d are your partitions as you can see it when run the df command


------------------------majordomo digest clean script--------------------
#/bin/bash

for i in `ls /etc/virtual`
do
if test -d /etc/virtual/$i
then
if test -d /etc/virtual/$i/majordomo/digests
then
for j in `ls /etc/virtual/$i/majordomo/digests`
do
rm /etc/virtual/$i/majordomo/digests/$j/*
done
fi
fi
done
---------------------------------------------------------------------
if you wish to clean the digests for all users


---------------------rkhunter and chrootkit script------------------------
#!/bin/sh

cd /usr/ports && pkg_version -v > /disk/bz2-backup/pkg_version.packages
[root@pindos ~/backup]# more rkhunter
#!/bin/sh
(
/usr/local/bin/rkhunter --versioncheck
/usr/local/bin/rkhunter --update
/usr/local/bin/rkhunter --cronjob --report-warnings-only

echo ""
echo "ChrootKit Checks"
/usr/local/sbin/chkrootkit | grep "INFECTED"

) | /usr/bin/mail -s 'Rkhunter Weekly Run' root
--------------------------------------------------------------------
to have a report from rkhunter and chrootkit programs


you can run it from /etc/crontab like :

10 3 * * * root /wheremyscriptis/backup-mysql 2>&1


if anyone can post some scripts for maintenance please do !
 
Back
Top