• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Bash script issues

Hi all,

I wrote a bash script before for bash2, and trying to get it to work with bash 3.  It's giving me a tremendous amount of issues. Would anyone be kind enough to help me spot why I can't use some of this functionality in bash3?

Will award points to whoever finds the error and is very specific about it. General inquiries are ignored.
--

#!/bin/sh
# Backup utility
#
# define vars
DAY=`/bin/date +%d`
MONTH=`/bin/date +%m`
YEAR=`/bin/date +%Y`
BACKUP_DIR="/opt/backup/${YEAR}/${MONTH}/${DAY}"
SERVICES="mysql web"
MAILLOG="/opt/backup/logs/backup.`date +%Y%m%d`.log"

exec 1>${MAILLOG}
exec 2>&1
#
#
# define functions
function mailer {
MAIL="/usr/sbin/sendmail -tfoo@example.com -fbackups@example.com"
if [ x"$!" == "x" ]; then
  SUBJECT="Daily Backup Logs"
  BODY=$(/bin/cat /opt/backup/logs/backup.`date +%Y%m%d`.log)
else
  SUBJECT=$1
  BODY="Fix it"
fi

${MAIL} << EOF
From: Backups <backups@example.com>
To: Foo <foo@example.com>
Subject: ${SUBJECT}

${BODY}
EOF
}

function create_dir {
  echo "Checking whether destination backup directories exist for services..."
  for service in ${SERVICES}; do
    if [ ! -d ${BACKUP_DIR}/${service} ]; then
      /bin/mkdir -p ${BACKUP_DIR}/${service}
      echo "Directory ${BACKUP_DIR}/${service} successfully created."
    else
      echo "Directory ${BACKUP_DIR}/${service} already exists."
    fi
  done
}

function database_backup {
# define vars
DB_HOSTS="foo.example.com"
DBBACKUP_DIR="${BACKUP_DIR}/mysql"
sleep 10
echo "Initiating MySQL dumps ..."
  for db in ${DB_HOSTS}; do
    if [ ${db} == "foo.example.com" ]; then
      /usr/bin/mysqldump -Q -uroot -pfoo -h${db} --all-databases > ${DBBACKUP_DIR}/${db}.sql
      if [ $? != 0 ]; then
        mailer mysql_${db}
      fi
    else
      /usr/bin/mysqldump -Q -uroot -pfoo -h${db} --all-databases > ${DBBACKUP_DIR}/${db}.sql
      if [ $? != 0 ]; then
        mailer mysql_${db}
      fi
    fi
  done
echo "MySQL dumps complete."
}

function web_content_backup {
sleep 10
APACHE_BOXES="foo.example.com"
APACHE_BACKUP_DIR="${BACKUP_DIR}/web"
  for www in ${APACHE_BOXES}; do
    echo "Initiating web content backup of ${www} ..."
    /usr/bin/rsync -avrz ${www} ${APACHE_BACKUP_DIR}/${www}
    if [ $? != 0 ]; then
      mailer apache_${www}
    fi
  done
    echo "Web content has been backed up."
}

create_dir
database_backup
web_content_backup
mailer
0
stevefNYC
Asked:
stevefNYC
1 Solution
 
TintinCommented:
It would be really helpful if you gave us some more information about what these "tremendous amount of issues" are.

Do you get any error messages?
Is the result not as intended?
0
 
stevefNYCAuthor Commented:
Never mind. It's the first time I was running this on a debian box. Debian links /bin/sh to /bin/dash, which is not the proper interpreter for the script above.

Please disregard this.

Moderators, feel free to delete this question.
0
 
ahoffmannCommented:
if you really mean and want to be executed bash, then use a proper hashbang line, like

#! /bin/bash
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now