[FUG-BR] Script Backup em Mail/FTP/SCP/DVDRW/CDRW

Fabricio Lima listas em fabriciolima.com.br
Qua Mar 15 21:03:46 BRT 2006


Gostaria de compartilhar com vcs um script q venho utilizado ha muito tempo.. e todo dia venho incrementando ele.

vc pode jogar no cron uma entrada pra semanalmente executar: /usr/local/etc/backup.sh start

a diretiva start executa: compress, rotate, upload e report.

ele gera varios .tar.gz.. um pra configuracao, um pra homes, etc...
a diretiva upload estah redefinida com um OR  junto com upload_ftp.. ou seja, se digitar backup.sh upload, ele ira fazer via ftp..
se vc quer backup via email, chame explicitamente.. upload_mail...

ou entao tire do lugar q está a diretiva upload default.

lembrando q pra funcionar upload_scp vc tem q jogar as chaves publicas pra la e pra ca, a fim de acontecer a autenticacao nao interativa

aceito comentarios.

Fabricio Lima
'Scripiteiro proficionau'   :)



#!/bin/sh
BASE=/var/tmp
HOST=192.168.231.2
NETADMIN=netadmin@`hostname`
USERNAME=anonymous
PASSWORD=email@
umask 027

case "$1" in
start)
 $0 rotate ; \
 $0 compress && \
 $0 upload ; \
 $0 report
 ;;

compress)
 echo ' compacting...'
 tar zcpf ${BASE}/Backup-conf-`hostname`.tar.gz \
  /usr/src/sys/i386/conf/MyKernel \
  /etc /usr/local/etc \
  /boot/loader.conf /boot/loader.rc \
      /root \
  /var/cron/tabs \
  /var/named/etc/namedb \
  /var/tmp/packages.info \
  /usr/local/pgsql/data/*.conf \
  /usr/local/lib/sasl/Sendmail.conf \
  /usr/local/www/cgi-bin/openwebmail/etc/openwebmail.conf \
  /usr/local/www/awstats/data /usr/local/www/awstats/cgi-bin/*.conf
 tar zcpf ${BASE}/Backup-db-`hostname`.tar.gz /usr/local/pgsql
 tar zcpf ${BASE}/Backup-www-`hostname`.tar.gz /usr/local/www --exclude=/usr/local/www/cgi-bin-dist/openwebmail/etc/users
 tar zcpf ${BASE}/Backup-mail-`hostname`.tar.gz /var/mail /usr/local/www/cgi-bin/openwebmail/etc/users
 tar zcpf ${BASE}/Backup-homes-`hostname`.tar.gz /root /usr/home
 ;;

full)
 echo ' compacting entire system...'
 tar zcpf ${BASE}/Backup-`hostname`.tar.gz \
  /etc /boot /bin /sbin /lib /libexec /usr /var /root /usr/home --exclude=${BASE} --exclude=/usr/ports
 ;;

upload_mail)
 if [ -r ${BASE}/Backup-conf-`hostname`.tar.gz ]; then
      echo ' sending email...'
      for fileset in `/bin/ls ${BASE}/Backup*.tar.gz`
      do
         /usr/bin/uuencode $fileset `/usr/bin/basename $fileset` | \
         /usr/bin/mailx -s "Backup Automatico" ${NETADMIN}
      done
   fi 
   ;;
   
upload_dvdrw)
 if [ -r ${BASE}/Backup-conf-`hostname`.tar.gz ]; then
      #echo ' generating image...'
      #mkisofs -o /tmp/backup.iso -JrT /var/tmp/
      umount /mnt/cdrom
      echo ' erasing media...'
      dvd+rw-format /dev/cdrom
      echo ' burning media...'
      growisofs -dvd-compat -Z /dev/cd0 -J -R /var/tmp
      #growisofs -dvd-compat -Z /dev/cd0=/tmp/backup.iso
      #rm -f /tmp/backup.iso

   fi 
   ;;

upload_cdrw)
 if [ -r ${BASE}/Backup-conf-`hostname`.tar.gz ]; then
      echo ' generating image...'
      mkisofs -o /tmp/backup.iso -JrT /var/tmp/
      umount /mnt/cdrom
      echo ' erasing media...'
      cdrecord dev=0,0,0 -v speed=8 blank=all -force
      echo ' burning media...'
      cdrecord dev=0,0,0 -v speed=8 -eject -multi -data /tmp/backup.iso
      rm -f /tmp/backup.iso

   fi 
   ;;
   
upload_scp)
 if [ -r ${BASE}/Backup-conf-`hostname`.tar.gz ]; then
      echo ' uploading...'
      /usr/bin/scp ${BASE}/Backup*.tar.gz ${USERNAME}@${HOST}:/var/tmp
   fi 
   ;;
   
upload|upload_ftp)
 if [ -r ${BASE}/Backup-conf-`hostname`.tar.gz ]; then
      echo ' uploading...'
      /usr/bin/ftp -ni ${HOST} << EOT
         user ${USERNAME} ${PASSWORD}
         binary
         passive on
         lcd /var/tmp
         cd /temp
         mput Backup*.tar.gz
         bye
EOT
   fi
 ;;

rotate)
 echo ' rotating old backups...'
 for tarball in `ls ${BASE}/Backup*.tar.gz`
 do
  mv -f $tarball $tarball.old
 done
 ;;

erase-all)
 echo ' erasing all...'
 rm -f ${BASE}/Backup*.tar.gz*
 ;;

erase)
 echo ' erasing...'
 rm -f ${BASE}/Backup*.tar.gz
 ;;

report)
 echo ' reporting...'
 ls -lh ${BASE}/Backup*.tar.gz | mail -s "Backup Automatico" ${NETADMIN}
 ;;

verify)
 echo ' listing...'
 ls -lh ${BASE}/Backup*.tar.gz
 ;;

stop)
 killall -9 tar && echo ' stoping...'
 ;;

*)
 echo "Usage: `basename $0` {start|stop|compress|full|upload|verify|report|rotate|erase|erase-all}" >&2
 ;;

esac

exit 0
_______________________________________________
freebsd mailing list
freebsd em fug.com.br
http://lists.fug.com.br/listinfo.cgi/freebsd-fug.com.br



Mais detalhes sobre a lista de discussão freebsd