Bash/Dash: Process killed on "fi" in an "If then else" statement?

271 views Asked by At

I have the following script which is creating SQL dumps out of my mysql database. The files are then historized by git. In this script I encounter a strange behaviour which I am -even with some research- not able to answer.

If I run the script I receive an error saying that in line 25 (which is the "fi" statement) the process "/usr/bin/git commit -m "$NOW" is terminated:

xxx@xxx:/var/log# /opt/mysql_backup.sh 
[master 12fd643] 2016-12-29
/opt/mysql_backup.sh: Line 25: 11163 Killed                /usr/bin/git commit -m "$NOW"

While debugging I commented out the if-then-else statement which checks for presence of git and the scipts runs without an error.

My Question is: Why does dash "terminate" the git command at the "fi" statement? In fact the commit is performed so I am wondering what the reason for the error message might be. The script is running on dash on Raspbian Jessie.

Thanks in advance!

Joerg

Script: /opt/mysql_backup.sh

#!/bin/dash
# TARGET: Backup-Ziel
# IGNORE: Liste zu ignorierender Datenbanken (durch | getrennt)
# CONF: MySQL Config-Datei, welche die Zugangsdaten enthaelt
TARGET=/srv_ext/z_backup/mysql
IGNORE="phpmyadmin|mysql|information_schema|performance_schema|test"
CONF=/etc/mysql/debian.cnf
if [ ! -r $CONF ]; then /usr/bin/logger "$0 - auf $CONF konnte nicht zugegriffen werden"; exit 1; fi
if [ ! -d $TARGET ] || [ ! -w $TARGET ]; then /usr/bin/logger "$0 - Backup-Verzeichnis nicht beschreibbar"; exit 1; fi

DBS="$(/usr/bin/mysql --defaults-extra-file=$CONF -Bse 'show databases' | /bin/grep -Ev $IGNORE)"
NOW=$(date +"%Y-%m-%d")

for DB in $DBS; do
    /usr/bin/mysqldump --defaults-extra-file=$CONF --skip-extended-insert --skip-comments $DB > $TARGET/$DB.sql
done

if [ -x /usr/bin/git ] && [ -d ${TARGET}/.git/branches ]
then
  cd $TARGET
  /usr/bin/git add .
  /usr/bin/git commit -m "$NOW"
else
  /usr/bin/logger "$0 - git nicht verfuegbar oder Backup-Ziel nicht unter Versionskontrolle"
fi

/usr/bin/logger "$0 - Backup von $NOW erfolgreich durchgefuehrt"
exit 0

Output of run with "set -x":

xxx@xxx:/opt# ./mysql_backup.sh 
+ TARGET=/srv_ext/z_backup/mysql
+ IGNORE=phpmyadmin|mysql|information_schema|performance_schema|test
+ CONF=/etc/mysql/debian.cnf
+ [ ! -r /etc/mysql/debian.cnf ]
+ [ ! -d /srv_ext/z_backup/mysql ]
+ [ ! -w /srv_ext/z_backup/mysql ]
+ /usr/bin/mysql --defaults-extra-file=/etc/mysql/debian.cnf -Bse show databases
+ /bin/grep -Ev phpmyadmin|mysql|information_schema|performance_schema|test
+ DBS=c_db
s1_db
s2_db
zabbix
+ date +%Y-%m-%d
+ NOW=2016-12-29
+ /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --skip-extended-insert --skip-comments c_db
+ /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --skip-extended-insert --skip-comments s1_db
+ /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --skip-extended-insert --skip-comments s2_db
+ /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --skip-extended-insert --skip-comments zabbix
+ [ -x /usr/bin/git ]
+ [ -d /srv_ext/z_backup/mysql/.git/branches ]
+ cd /srv_ext/z_backup/mysql
+ /usr/bin/git add .
+ /usr/bin/git commit -m 2016-12-29
[master b8952b9] 2016-12-29
Killed
+ /usr/bin/logger ./mysql_backup.sh - Backup von 2016-12-29 erfolgreich durchgefuehrt
+ exit 0
1

There are 1 answers

0
Armali On

Of course the raspberry has just 1G and I did not enabled swap due to wearleveling of the SD memory card. With swap its working! – Joerg