SHARE
TWEET

backup_ast 1.0

mboylan May 9th, 2012 351 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # ============================================================================
  3. # backup_ast -- A utility for backing up asterisk configuration files        #
  4. #                                                                            #
  5. # Version:      1.0                                                          #
  6. # Written By:   Mike Boylan                                                  #
  7. #               Robert Morris University                                     #
  8. #               boylan@rmu.edu                                               #
  9. # ============================================================================
  10.  
  11. # Useful to store these...
  12. NOW=$(date "+%Y%m%d%H%M")
  13. ASTBACKUPDIR=/etc/asterisk/backup
  14. ASTERISKDIR=/etc/asterisk
  15. DAHDIDIR=/etc/dahdi
  16. DAHDIBACKUPDIR=/etc/dahdi/backup
  17.  
  18. # Parse our options
  19. while getopts “adehl:ostv” my_opt; do
  20.     case $my_opt in
  21.         a  ) all=1;;
  22.         d  ) dahdi=1;;
  23.         e  ) extensions=1;;
  24.         h  ) display_help=1;;
  25.         l  ) OVERRIDEBACKUPDIR=$OPTARG;;
  26.         o  ) other=1;;
  27.         s  ) sip=1;;
  28.         t  ) usetar=1;;
  29.         v  ) voicemail=1;;
  30.     esac
  31. done
  32.  
  33. # Make sure that $1, $2, etc. are references to everything after the flags
  34. shift $(($OPTIND - 1))
  35.  
  36. # Used to copy a single file. Accounts for full filename trimming
  37. copy_file() {
  38.         FILENAME=$(echo $file | cut -d "/" -f 4)
  39.         cp $COPYDIR/$FILENAME $COPYBACKUPDIR/$FILENAME.$NOW
  40. }
  41.  
  42. # Used to copy all files in a directory at once
  43. copy_all() {
  44.         find $COPYDIR -mindepth 1 -maxdepth 1 \( ! -name backup \) -print0 | xargs -0I % cp -rf % $COPYBACKUPDIR/
  45. }
  46.  
  47. # With -ta and -a you can't use anything else
  48. check_all() {
  49.         if [[ -n $other ]] || [[ -n $dahdi ]] || [[ -n $extensions ]] || [[ -n $display_help ]] || [[ -n $sip ]] || [[ -n $voicemail ]]; then
  50.                 echo "$ERROR"
  51.                 exit 1
  52.         fi
  53. }
  54.  
  55. # Used to set the backup directory
  56. set_backup_dir() {
  57.         if [[ -n $OVERRIDEBACKUPDIR ]]; then
  58.                 COPYBACKUPDIR=$OVERRIDEBACKUPDIR
  59.                 WASOVERRIDDEN=1
  60.         else
  61.                 COPYBACKUPDIR=$1
  62.         fi
  63. }
  64.  
  65. # If they choose -h
  66. if [[ -n $display_help ]]; then
  67.         echo ""
  68.         echo "backup_ast -- Version 1.0 -- Mike Boylan -- Robert Morris University"
  69.         echo ""
  70.         echo "This script is used to back up Asterisk configuration files to the"
  71.         echo "/etc/asterisk/backup directory & DAHDI configs to the /etc/dahdi/backup directory."
  72.         echo ""
  73.         echo "It supports the following flags:"
  74.         echo ""
  75.         echo "-a                Backup ALL config files, including DAHDI and Asterisk"
  76.         echo "          This creates folders in /etc/asterisk/backup and /etc/dahdi/backup with"
  77.         echo "          the current date and time and copies all relevant files to them. This cannot"
  78.         echo "          be used with any other option besides -t to create a tarball & -l to override location"
  79.         echo "-d                Backup dahdi configs to /etc/dahdi/backup"
  80.         echo "-e                Backup extensions.conf"
  81.         echo "-h                Display this help"
  82.         echo "-l                Override the location to store the backup files"
  83.         echo "-o                Backup another file given as an argument"
  84.         echo "-s                Backup sip.conf"
  85.         echo "-t                Can only be used with -a and -l. If used with -a it will create a tarball named with"
  86.         echo "          the current date and time in /etc/asterisk/backup unless put somewhere else with -l"
  87.         echo "-v                Backup voicemail.conf"
  88.         echo ""
  89.         echo "Usage: backup_ast -o /etc/asterisk/features.conf OR backup_ast -o features.conf"
  90.         echo "Backs up features.conf -- It's not necessary to always supply the full path of the file"
  91.         echo ""
  92.         echo "Usage: backup_ast -e"
  93.         echo "Backs up extensions.conf"
  94.         echo ""
  95.         echo "Usage: backup_ast -l ~/ -s -e -v -o /etc/asterisk/extconfig.conf"
  96.         echo "Backs up all the files, plus extconfig.conf to the current user's home directory"
  97.         echo ""
  98.         echo "IMPORTANT: If using the -l flag, it must come before the -o flag if also being used."
  99.         echo ""
  100.         echo "Usage: backup_ast -l ~/ -t -a"
  101.         echo "Creates a tarball backup in the current user's home directory with folders inside for asterisk & DAHDI"
  102.         echo ""
  103.         exit 0
  104. fi
  105.  
  106. # Check for the asterisk and DAHDI backup directories before doing anything else
  107. if [[ ! -d $ASTBACKUPDIR ]]; then
  108.         echo "Please create the Asterisk backup directory at /etc/asterisk/backup and then try again."
  109.         exit 1
  110. fi
  111.  
  112. if [[ ! -d $DAHDIBACKUPDIR ]]; then
  113.         echo "Please create the DAHDI backup directory at /etc/dahdi/backup and then try again."
  114.         exit 1
  115. fi
  116.  
  117. # If they override the backup location
  118. if [[ -n $OVERRIDEBACKUPDIR ]]; then
  119.         if [[ ! -d $OVERRIDEBACKUPDIR ]]; then
  120.                 echo "Please specify a valid backup directory!"
  121.                 exit 1
  122.         fi
  123. fi
  124.  
  125. # Check to make sure they're using -a if they're using -t, and that they're not using any other flags
  126. if [[ -n $usetar ]]; then
  127.         if [[ -n $all ]]; then
  128.                 ERROR="-t and -a cannot be used with anything else"
  129.                 check_all
  130.                 TMPDIR=/tmp/astbackup$NOW
  131.                 mkdir -p $TMPDIR/asterisk
  132.                 mkdir -p $TMPDIR/dahdi
  133.                 COPYDIR=$ASTERISKDIR
  134.                 COPYBACKUPDIR=$TMPDIR/asterisk
  135.                 copy_all
  136.                 COPYDIR=$DAHDIDIR
  137.                 COPYBACKUPDIR=$TMPDIR/dahdi
  138.                 copy_all
  139.                 cd /tmp
  140.                 set_backup_dir $ASTBACKUPDIR
  141.                 tar -czf $COPYBACKUPDIR/astbackup$NOW.tar.gz astbackup$NOW
  142.                 rm -rf $TMPDIR
  143.                 all=""
  144.         else
  145.                 echo "-t can only be used with -a"
  146.                 exit 1
  147.         fi
  148. fi
  149.  
  150. # If they want to backup them all
  151. # If they override with -l, it gets more tricky because we're backing up
  152. # asterisk and DAHDI. In that case, make one folder in the overridden location
  153. # named with the current date and time containing a folder for asterisk and dahdi
  154. # If no override is used, default to making a folder with the current date and time
  155. # inside /etc/asterisk/backup and /etc/dahdi/backup. We do this because there's simply
  156. # too many files to copy them all, dated, to the backup directories
  157. if [[ -n $all ]]; then
  158.         ERROR="-a cannot be used with anything else"
  159.         check_all
  160.         COPYDIR=$ASTERISKDIR
  161.         set_backup_dir $ASTBACKUPDIR
  162.         if [[ -n $WASOVERRIDDEN ]]; then
  163.                 COPYBACKUPDIR=$COPYBACKUPDIR/astbackup$NOW/asterisk
  164.         else
  165.                 COPYBACKUPDIR=$COPYBACKUPDIR/$NOW
  166.         fi
  167.         mkdir -p $COPYBACKUPDIR
  168.         copy_all
  169.         COPYDIR=$DAHDIDIR
  170.         set_backup_dir $DAHDIBACKUPDIR
  171.         if [[ -n $WASOVERRIDDEN ]]; then
  172.                 COPYBACKUPDIR=$COPYBACKUPDIR/astbackup$NOW/dahdi
  173.         else
  174.                 COPYBACKUPDIR=$COPYBACKUPDIR/$NOW
  175.         fi
  176.         mkdir -p $COPYBACKUPDIR
  177.         copy_all
  178. fi
  179.  
  180. # Check to make sure we have a valid filename if they use -o
  181. if [[ -n $other ]]; then
  182.         if [[ ! -f "$1" ]] && [[ ! -f "/etc/asterisk/$1" ]]; then
  183.            echo "Please specify a valid filename as an argument!"
  184.            exit 1
  185.         fi
  186.         file=$1
  187.         COPYDIR=$ASTERISKDIR
  188.         set_backup_dir $ASTBACKUPDIR
  189.         copy_file file
  190. fi
  191.  
  192. # If they choose -e
  193. if [[ -n $extensions ]]; then
  194.         file=extensions.conf
  195.         COPYDIR=$ASTERISKDIR
  196.         set_backup_dir $ASTBACKUPDIR
  197.         copy_file
  198. fi
  199.  
  200. # If they choose -d
  201. # DAHDI has multiple files, so we need an appropriately named directory
  202. # to store them, if overridden. Otherwise date a folder in /etc/dahdi/backup
  203. if [[ -n $dahdi ]]; then
  204.         COPYDIR=$DAHDIDIR
  205.         set_backup_dir $DAHDIBACKUPDIR
  206.         if [[ -n $WASOVERRIDDEN ]]; then
  207.                 COPYBACKUPDIR=$COPYBACKUPDIR/dahdi$NOW
  208.         else
  209.                 COPYBACKUPDIR=$COPYBACKUPDIR/$NOW
  210.         fi
  211.         mkdir $COPYBACKUPDIR
  212.         copy_all
  213. fi
  214.  
  215. # If they choose -s
  216. if [[ -n $sip ]]; then
  217.         file=sip.conf
  218.         COPYDIR=$ASTERISKDIR
  219.         set_backup_dir $ASTBACKUPDIR
  220.         copy_file
  221. fi
  222.  
  223. # If they choose -v
  224. if [[ -n $voicemail ]]; then
  225.         file=voicemail.conf
  226.         COPYDIR=$ASTERISKDIR
  227.         set_backup_dir $ASTBACKUPDIR
  228.         copy_file
  229. fi
  230.  
  231. exit 0
RAW Paste Data
Top