Skip to content

Commit 5fd5544

Browse files
committed
Allow set cnf file as arg for automatization
Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>
1 parent 7beac28 commit 5fd5544

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ These queries are safe to run, but if you have a large database please note that
1414
or
1515

1616
user@host-$ ./checkscript.sh
17+
user@host-$ ./checkscript.sh ./zbx_db_info.cnf
1718

1819
Usage: delete the orphaned rows
1920
===============================
@@ -25,3 +26,4 @@ Use the queries above on your won risk. Take *backups* first. Once you have back
2526
or
2627

2728
user@host-$ ./cleanup.sh
29+
user@host-$ ./cleanup.sh ./zbx_db_info.cnf

checkscript.sh

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,27 @@ CONF="$(mktemp)"
55
cleanup_conf(){ rm "$CONF"; }
66
trap cleanup_conf SIGINT SIGTERM EXIT
77

8-
echo -n "MySQL username: " ; read -r username
9-
echo -n "MySQL password: " ; stty -echo ; read -r password ; stty echo ; echo
10-
echo -n "MySQL database: " ; read -r database
11-
12-
{
13-
echo [client]
14-
echo user = "$username"
15-
echo password = "$password"
16-
echo database = "$database"
17-
} > "$CONF"
8+
if [ ! -z "$1" ] && [ -f "$1" ]; then
9+
echo "Use cnf file: $1"
10+
for val in user password database; do
11+
grep -q "$val" "$1" || {
12+
echo "Missing $val field in $1"
13+
exit 1
14+
}
15+
done
16+
cp "$1" "$CONF"
17+
else
18+
echo -n "MySQL username: " ; read -r username
19+
echo -n "MySQL password: " ; stty -echo ; read -r password ; stty echo ; echo
20+
echo -n "MySQL database: " ; read -r database
21+
22+
{
23+
echo [client]
24+
echo user = "$username"
25+
echo password = "$password"
26+
echo database = "$database"
27+
} > "$CONF"
28+
fi
1829

1930
mysql_w(){
2031
[ ! -f "$CONF" ] && exit 1

cleanup.sh

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,27 @@ CONF="$(mktemp)"
55
cleanup_conf(){ rm "$CONF"; }
66
trap cleanup_conf SIGINT SIGTERM EXIT
77

8-
echo -n "MySQL username: " ; read -r username
9-
echo -n "MySQL password: " ; stty -echo ; read -r password ; stty echo ; echo
10-
echo -n "MySQL database: " ; read -r database
11-
12-
{
13-
echo [client]
14-
echo user = "$username"
15-
echo password = "$password"
16-
echo database = "$database"
17-
} > "$CONF"
8+
if [ ! -z "$1" ] && [ -f "$1" ]; then
9+
echo "Use cnf file: $1"
10+
for val in user password database; do
11+
grep -q "$val" "$1" || {
12+
echo "Missing $val field in $1"
13+
exit 1
14+
}
15+
done
16+
cp "$1" "$CONF"
17+
else
18+
echo -n "MySQL username: " ; read -r username
19+
echo -n "MySQL password: " ; stty -echo ; read -r password ; stty echo ; echo
20+
echo -n "MySQL database: " ; read -r database
21+
22+
{
23+
echo [client]
24+
echo user = "$username"
25+
echo password = "$password"
26+
echo database = "$database"
27+
} > "$CONF"
28+
fi
1829

1930
mysql_w(){
2031
[ ! -f "$CONF" ] && exit 1

0 commit comments

Comments
 (0)