I have written this shell script to backup MySQL database to disk. It works fine but I am not well proficient in shell scripting. So what do you think can it be improved?
#!/bin/bash
#For taking backup
APPEND_CLI=$3
DIR=/media/storage/backup/db_backup/
DATESTAMP=$(date +%d-%m-%y-%H-%M)
DB_USER=backup
DB_PORT=$2
DB_PASS='readonly'
HOST=$1
if [[ `id -u` != 0 ]]; then
echo "Must be root to run script"
exit
fi
if test -z "$HOST"
then
echo "HOST not passed."
exit 1
fi
if test -z "$DB_PORT"
then
echo "PORT not passed."
exit 1
fi
# remove backups older than $DAYS_KEEP
DAYS_KEEP=7
find ${DIR}* -mtime +$DAYS_KEEP -exec rm -f {} \; 2> /dev/null
# create backups securely
umask 006
# list MySQL databases and dump each
DB_LIST=`$APPEND_CLI mysql -h $HOST -u $DB_USER -p"$DB_PASS" -e'show databases;'`
echo "Listing databases"
echo ${DB_LIST##Database}
DB_LIST=${DB_LIST##Database}
for DB in $DB_LIST;
do
if [[ "${DB}" == "performance_schema" ]]
then
echo "Skipping database ${DB}."
continue
fi
FILENAME=${DIR}${DB}-${DATESTAMP}.msql.${DB_PORT}.gz
echo "Initiating backup of $DB for ${HOST} on port ${DB_PORT}"
$APPEND_CLI mysqldump -h $HOST -P $DB_PORT -u $DB_USER -p"$DB_PASS" $DB --single-transaction | gzip > $FILENAME
echo "Done backing up ${FILENAME}"
done