sicherung.sh | Backup Script

Nützliches Script für den "Hausgebrauch"

Beispiel: sicherung.sh full --without home (Sichert das komplette System bis auf die Home-Verzeichnisse)

  1. #!/bin/sh
  2.  
  3. ################################################################################
  4. #
  5. # Backup Script
  6. # Author: Manuel Strauch
  7. #
  8. ################################################################################
  9.  
  10. ### Zu sichernde Verzeichnisse + Dateien
  11. system="bin boot dev etc lib libexec proc sbin sys tmp usr var"
  12. rescue="rescue"
  13. optional="opt"
  14. mountpoints="media mnt"
  15. files="COPYRIGHT"
  16. compat=" "
  17. homes="home root"
  18. webpage="usr/local/etc/apache22 usr/local/www"
  19. jails="etc/rc.conf usr/jails"
  20.  
  21. ## Dateien oder Verzeichnisse nicht sichern!
  22. ## Backup-Verzeichnis wird automatisch nicht gesichert.
  23. exclude="usr/ports/distfiles"
  24.  
  25. ## Backup Verzeichnis
  26. tar_dir="/usr/Sicherung"
  27.  
  28. ### Tar Optionen usw.
  29. version="1.2"
  30. zip="bzip2"  
  31. # zip="gzip"
  32. compress="-v9"
  33.  
  34. ## =================== Don`t TOUCH ===================
  35. . /opt/etc/system.var
  36.  
  37. backup_date="`date +%Y-%m-%d`"
  38. check_without="no"
  39. tar_options="-cvp"
  40. system_name="`uname | tr [A-Z] [a-z]`"
  41. system_release="`uname -r | tr [A-Z] [a-z]`"
  42. system_hostname="`hostname | cut -f1 -d.`"
  43. tar_file_string="${system_hostname}-${1}-${system_name}-${system_release}-${backup_date}"
  44. backup_file="${tar_dir}/backup-${tar_file_string}.tar"
  45. log_file="${tar_dir}/backup-${tar_file_string}.log"
  46. exclude="`echo ${tar_dir} | sed 's/^\///g'` ${exclude}"
  47.  
  48. ### Funktionen
  49. search_options() {
  50.   testing="${2}"
  51.   if [ ! -z "$testing" ]; then
  52.     case "${2}" in
  53.       --without|--backup)
  54.         shift
  55.         for i in ${@}; do
  56.           case "${i}" in
  57.             --without)
  58.               check_without="yes"
  59.             ;;
  60.             --backup)
  61.               check_without="no"
  62.             ;;
  63.             *)
  64.               if [ "${check_without}" = "yes" ]; then
  65.                 exclude="${exclude} ${i}"
  66.               else
  67.                 tar_backup="${tar_backup} ${i}"
  68.               fi
  69.             ;;
  70.           esac
  71.         done
  72.       ;;
  73.       *)
  74.         echo "syntax error!!"
  75.         echo "option { "${@}" } was not supported!"
  76.         exit 1
  77.       ;;
  78.     esac
  79.   fi
  80. }
  81.  
  82. start_backup() {
  83.   cd /
  84.   if [ "${zip}" = "bzip2" ]; then
  85.     backup_file="${backup_file}.bz2"
  86.   else
  87.     if [ "$zip" = "gzip" ]; then
  88.       backup_file="${backup_file}.gz"
  89.     else
  90.       echo " "
  91.       echo "unsupported compress method!"
  92.       echo "use bzip2 or gzip"
  93.       exit 1
  94.     fi
  95.   fi
  96.   for i in ${exclude}; do
  97.     excludeline="${excludeline} --exclude=${i}"
  98.   done
  99.   echo "You can see the files in the logfile: "
  100.   echo "${log_file}"
  101.   echo -e " --> Backup: >>starting `date `<< ${ISRUNNING}"
  102.   (
  103.     tar ${tar_options} ${excludeline} -f - ${tar_backup} | \
  104.       ${zip} ${compress} > "${backup_file}"
  105.   ) > "${log_file}" 2>&1
  106.   echo -e " --> Backup: >>finished `date`<< ${ISTERMINATED}"
  107. }
  108.  
  109. ### Programm
  110. case "${1}" in
  111.   full)
  112.     tar_backup="${system} ${rescue} ${optional} ${mountpoints} ${files} ${compat} ${homes}"
  113.     search_options "${@}"
  114.     start_backup
  115.   ;;
  116.   www)
  117.     tar_backup="${webpage}"
  118.     search_options "${@}"
  119.     start_backup
  120.   ;;
  121.   homes)
  122.     tar_backup="${homes}"
  123.     search_options "${@}"
  124.     start_backup
  125.   ;;
  126.   jails)
  127.     tar_backup="${jails}"
  128.     search_options "${@}"
  129.     start_backup
  130.   ;;
  131.   manuell)
  132.     search_options "${@}"
  133.     start_backup
  134.   ;;
  135.   help)
  136. cat <<__USAGE__
  137.  
  138.     hoschis backup script version ${version}
  139.     ==================================
  140.  
  141.     usage: ${0} { full | www | homes | jails | manuell | help }
  142.     ----------------------------------
  143.     special options:
  144.       --without
  145.     ----------------------------------
  146.     manuell syntax:
  147.     ${0} manuell --backup bin boot --without home
  148.     or
  149.     ${0} manuell --without bin boot --backup home
  150.     ----------------------------------
  151.     syntax:
  152.     ${0} full --without home
  153.     ----------------------------------
  154.  
  155. __USAGE__
  156.   ;;
  157.   *)
  158.     echo "usage: ${0} help"
  159.     echo "for help"
  160.   ;;
  161. esac
  162. exit 0