fix bash syntax accordint to shellcheck

This commit is contained in:
Fco. Javier Delgado del Hoyo 2017-04-08 17:57:23 +02:00
parent cedc1c36b3
commit da419c79b1
3 changed files with 18 additions and 15 deletions

View file

@ -2,27 +2,30 @@
[ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; } [ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; }
[ -z "${MYSQL_PASS}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; } [ -z "${MYSQL_PASS}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
DATE=`date +%Y%m%d%H%M` DATE=$(date +%Y%m%d%H%M)
echo "=> Backup started at $DATE" echo "=> Backup started at $DATE"
databases=`mysql -u $MYSQL_USER -p$MYSQL_PASS -e "SHOW DATABASES;" | tr -d "| " | grep -v Database` databases=$(mysql -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
for db in $databases; do for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]]
then
echo "Dumping database: $db" echo "Dumping database: $db"
FILENAME=/backup/$DATE.$db.sql FILENAME=/backup/$DATE.$db.sql
if mysqldump -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASS $db > $FILENAME ;then if mysqldump -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$db" > "$FILENAME"
gzip -f $FILENAME then
gzip -f "$FILENAME"
else else
rm -rf $FILENAME rm -rf "$FILENAME"
fi fi
fi fi
done done
if [ -n "$MAX_BACKUPS" ]; then if [ -n "$MAX_BACKUPS" ]
while [ `ls -1 /backup | wc -l` -gt "$MAX_BACKUPS" ]; then
while [ $(find /backup -maxdepth 1 -name "*.sql.gz" | wc -l) -gt "$MAX_BACKUPS" ];
do do
TARGET=`ls -1 /backup | sort | head -n 1` TARGET=$(find /backup -maxdepth 1 -name "*.sql.gz" | sort | head -n 1)
echo "Backup ${TARGET} is deleted" echo "Backup $TARGET is deleted"
rm -rf /backup/${TARGET} rm -rf /backup/"$TARGET"
done done
fi fi

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
echo "=> Restore database from \$1" echo "=> Restore database from $1"
if mysql -h $MYSQL_HOST -P $MYSQL_POR -u $MYSQL_USER -p$MYSQL_PASS < \$1 ;then if mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" < "$1" ;then
echo "=> Restore succeeded" echo "=> Restore succeeded"
else else
echo "=> Restore failed" echo "=> Restore failed"

4
run.sh
View file

@ -7,12 +7,12 @@ if [ -n "${INIT_BACKUP}" ]; then
/backup.sh /backup.sh
elif [ -n "${INIT_RESTORE_LATEST}" ]; then elif [ -n "${INIT_RESTORE_LATEST}" ]; then
echo "=> Restore latest backup" echo "=> Restore latest backup"
until nc -z $MYSQL_HOST $MYSQL_PORT until nc -z "$MYSQL_HOST" "$MYSQL_PORT"
do do
echo "waiting database container..." echo "waiting database container..."
sleep 1 sleep 1
done done
ls -d -1 /backup/* | tail -1 | xargs /restore.sh find /backup -maxdepth 1 -name '*.sql.gz' | tail -1 | xargs /restore.sh
fi fi
echo "${CRON_TIME} /backup.sh >> /mysql_backup.log 2>&1" > /crontab.conf echo "${CRON_TIME} /backup.sh >> /mysql_backup.log 2>&1" > /crontab.conf