Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # ============================================================================
- # backup_ast -- A utility for backing up asterisk configuration files #
- # #
- # Version: 1.0 #
- # Written By: Mike Boylan #
- # Robert Morris University #
- # boylan@rmu.edu #
- # ============================================================================
- # Useful to store these...
- NOW=$(date "+%Y%m%d%H%M")
- ASTBACKUPDIR=/etc/asterisk/backup
- ASTERISKDIR=/etc/asterisk
- DAHDIDIR=/etc/dahdi
- DAHDIBACKUPDIR=/etc/dahdi/backup
- # Parse our options
- while getopts “adehl:ostv” my_opt; do
- case $my_opt in
- a ) all=1;;
- d ) dahdi=1;;
- e ) extensions=1;;
- h ) display_help=1;;
- l ) OVERRIDEBACKUPDIR=$OPTARG;;
- o ) other=1;;
- s ) sip=1;;
- t ) usetar=1;;
- v ) voicemail=1;;
- esac
- done
- # Make sure that $1, $2, etc. are references to everything after the flags
- shift $(($OPTIND - 1))
- # Used to copy a single file. Accounts for full filename trimming
- copy_file() {
- FILENAME=$(echo $file | cut -d "/" -f 4)
- cp $COPYDIR/$FILENAME $COPYBACKUPDIR/$FILENAME.$NOW
- }
- # Used to copy all files in a directory at once
- copy_all() {
- find $COPYDIR -mindepth 1 -maxdepth 1 \( ! -name backup \) -print0 | xargs -0I % cp -rf % $COPYBACKUPDIR/
- }
- # With -ta and -a you can't use anything else
- check_all() {
- if [[ -n $other ]] || [[ -n $dahdi ]] || [[ -n $extensions ]] || [[ -n $display_help ]] || [[ -n $sip ]] || [[ -n $voicemail ]]; then
- echo "$ERROR"
- exit 1
- fi
- }
- # Used to set the backup directory
- set_backup_dir() {
- if [[ -n $OVERRIDEBACKUPDIR ]]; then
- COPYBACKUPDIR=$OVERRIDEBACKUPDIR
- WASOVERRIDDEN=1
- else
- COPYBACKUPDIR=$1
- fi
- }
- # If they choose -h
- if [[ -n $display_help ]]; then
- echo ""
- echo "backup_ast -- Version 1.0 -- Mike Boylan -- Robert Morris University"
- echo ""
- echo "This script is used to back up Asterisk configuration files to the"
- echo "/etc/asterisk/backup directory & DAHDI configs to the /etc/dahdi/backup directory."
- echo ""
- echo "It supports the following flags:"
- echo ""
- echo "-a Backup ALL config files, including DAHDI and Asterisk"
- echo " This creates folders in /etc/asterisk/backup and /etc/dahdi/backup with"
- echo " the current date and time and copies all relevant files to them. This cannot"
- echo " be used with any other option besides -t to create a tarball & -l to override location"
- echo "-d Backup dahdi configs to /etc/dahdi/backup"
- echo "-e Backup extensions.conf"
- echo "-h Display this help"
- echo "-l Override the location to store the backup files"
- echo "-o Backup another file given as an argument"
- echo "-s Backup sip.conf"
- echo "-t Can only be used with -a and -l. If used with -a it will create a tarball named with"
- echo " the current date and time in /etc/asterisk/backup unless put somewhere else with -l"
- echo "-v Backup voicemail.conf"
- echo ""
- echo "Usage: backup_ast -o /etc/asterisk/features.conf OR backup_ast -o features.conf"
- echo "Backs up features.conf -- It's not necessary to always supply the full path of the file"
- echo ""
- echo "Usage: backup_ast -e"
- echo "Backs up extensions.conf"
- echo ""
- echo "Usage: backup_ast -l ~/ -s -e -v -o /etc/asterisk/extconfig.conf"
- echo "Backs up all the files, plus extconfig.conf to the current user's home directory"
- echo ""
- echo "IMPORTANT: If using the -l flag, it must come before the -o flag if also being used."
- echo ""
- echo "Usage: backup_ast -l ~/ -t -a"
- echo "Creates a tarball backup in the current user's home directory with folders inside for asterisk & DAHDI"
- echo ""
- exit 0
- fi
- # Check for the asterisk and DAHDI backup directories before doing anything else
- if [[ ! -d $ASTBACKUPDIR ]]; then
- echo "Please create the Asterisk backup directory at /etc/asterisk/backup and then try again."
- exit 1
- fi
- if [[ ! -d $DAHDIBACKUPDIR ]]; then
- echo "Please create the DAHDI backup directory at /etc/dahdi/backup and then try again."
- exit 1
- fi
- # If they override the backup location
- if [[ -n $OVERRIDEBACKUPDIR ]]; then
- if [[ ! -d $OVERRIDEBACKUPDIR ]]; then
- echo "Please specify a valid backup directory!"
- exit 1
- fi
- fi
- # Check to make sure they're using -a if they're using -t, and that they're not using any other flags
- if [[ -n $usetar ]]; then
- if [[ -n $all ]]; then
- ERROR="-t and -a cannot be used with anything else"
- check_all
- TMPDIR=/tmp/astbackup$NOW
- mkdir -p $TMPDIR/asterisk
- mkdir -p $TMPDIR/dahdi
- COPYDIR=$ASTERISKDIR
- COPYBACKUPDIR=$TMPDIR/asterisk
- copy_all
- COPYDIR=$DAHDIDIR
- COPYBACKUPDIR=$TMPDIR/dahdi
- copy_all
- cd /tmp
- set_backup_dir $ASTBACKUPDIR
- tar -czf $COPYBACKUPDIR/astbackup$NOW.tar.gz astbackup$NOW
- rm -rf $TMPDIR
- all=""
- else
- echo "-t can only be used with -a"
- exit 1
- fi
- fi
- # If they want to backup them all
- # If they override with -l, it gets more tricky because we're backing up
- # asterisk and DAHDI. In that case, make one folder in the overridden location
- # named with the current date and time containing a folder for asterisk and dahdi
- # If no override is used, default to making a folder with the current date and time
- # inside /etc/asterisk/backup and /etc/dahdi/backup. We do this because there's simply
- # too many files to copy them all, dated, to the backup directories
- if [[ -n $all ]]; then
- ERROR="-a cannot be used with anything else"
- check_all
- COPYDIR=$ASTERISKDIR
- set_backup_dir $ASTBACKUPDIR
- if [[ -n $WASOVERRIDDEN ]]; then
- COPYBACKUPDIR=$COPYBACKUPDIR/astbackup$NOW/asterisk
- else
- COPYBACKUPDIR=$COPYBACKUPDIR/$NOW
- fi
- mkdir -p $COPYBACKUPDIR
- copy_all
- COPYDIR=$DAHDIDIR
- set_backup_dir $DAHDIBACKUPDIR
- if [[ -n $WASOVERRIDDEN ]]; then
- COPYBACKUPDIR=$COPYBACKUPDIR/astbackup$NOW/dahdi
- else
- COPYBACKUPDIR=$COPYBACKUPDIR/$NOW
- fi
- mkdir -p $COPYBACKUPDIR
- copy_all
- fi
- # Check to make sure we have a valid filename if they use -o
- if [[ -n $other ]]; then
- if [[ ! -f "$1" ]] && [[ ! -f "/etc/asterisk/$1" ]]; then
- echo "Please specify a valid filename as an argument!"
- exit 1
- fi
- file=$1
- COPYDIR=$ASTERISKDIR
- set_backup_dir $ASTBACKUPDIR
- copy_file file
- fi
- # If they choose -e
- if [[ -n $extensions ]]; then
- file=extensions.conf
- COPYDIR=$ASTERISKDIR
- set_backup_dir $ASTBACKUPDIR
- copy_file
- fi
- # If they choose -d
- # DAHDI has multiple files, so we need an appropriately named directory
- # to store them, if overridden. Otherwise date a folder in /etc/dahdi/backup
- if [[ -n $dahdi ]]; then
- COPYDIR=$DAHDIDIR
- set_backup_dir $DAHDIBACKUPDIR
- if [[ -n $WASOVERRIDDEN ]]; then
- COPYBACKUPDIR=$COPYBACKUPDIR/dahdi$NOW
- else
- COPYBACKUPDIR=$COPYBACKUPDIR/$NOW
- fi
- mkdir $COPYBACKUPDIR
- copy_all
- fi
- # If they choose -s
- if [[ -n $sip ]]; then
- file=sip.conf
- COPYDIR=$ASTERISKDIR
- set_backup_dir $ASTBACKUPDIR
- copy_file
- fi
- # If they choose -v
- if [[ -n $voicemail ]]; then
- file=voicemail.conf
- COPYDIR=$ASTERISKDIR
- set_backup_dir $ASTBACKUPDIR
- copy_file
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement