Commit 473266bf authored by Michał 'rysiek' Woźniak's avatar Michał 'rysiek' Woźniak
Browse files

more consistency in function argument order

parent 88e2ee5c
# General purpose utilities, mainly in Bash
These utilities might be useful for things ranging from database dumps/backups to making it possible to set certain programs that require arguments (`rsync`, `rsnapshot`) as the command for a key in SSH `athorized_keys`.
\ No newline at end of file
These utilities might be useful for things ranging from database dumps/backups to making it possible to set certain programs that require arguments (`rsync`, `rsnapshot`) as the command for a key in SSH `athorized_keys`.
## TODO
Some documentation would be welcome...
\ No newline at end of file
......@@ -133,16 +133,16 @@ export -f create_readonly_mysql_user
#
# this assumes we have *at least* read-only access to databases we want to dump
# $1 - targed directory; the directory and structure within ($1/, $1/schemas/, $1/dumps/) will be made sure to exist
# $2 - database host to connect to
# $3 - user to connect as
# $4 - password for that user
# $1 - database host to connect to
# $2 - user to connect as
# $3 - password for that user
# $4 - targed directory; the directory and structure within ($4/, $4/schemas/, $4/dumps/) will be made sure to exist
# $5 and further - optional, the databases to dump (dumping all if empty)
#
# relevant: https://dev.mysql.com/doc/refman/5.6/en/mysqldump.html
#
# databases are dumped as schema (containing grants related to the database and CREATE statements) and data dump spearately,
# put in $1/schemas/$database_name.schema.sql and $1/dumps/$database_name.dump.sql respectively
# put in $4/schemas/$database_name.schema.sql and $4/dumps/$database_name.dump.sql respectively
#
# this does *not* dump internal MySQL information_schema and performance_schema databases.
function dump_mysql_dbs {
......@@ -151,9 +151,9 @@ function dump_mysql_dbs {
clean_db_vars
# create the directory structure
mkdir -p "$1"
mkdir -p "$1/schemas"
mkdir -p "$1/dumps"
mkdir -p "$4"
mkdir -p "$4/schemas"
mkdir -p "$4/dumps"
#
# - if we have no additional arguments (5th and onwards), we need to dump all the databases (well, apart from information_schema and performance_schema)
......@@ -164,7 +164,7 @@ function dump_mysql_dbs {
fi
# get the list of databases
DATABASES="$( mysql -h "$2" -u "$3" --password="$4" --batch -e "SHOW DATABASES $DATABASES_WHERE;" | egrep -v '(Database|information_schema|performance_schema)' )"
DATABASES="$( mysql -h "$1" -u "$2" --password="$3" --batch -e "SHOW DATABASES $DATABASES_WHERE;" | egrep -v '(Database|information_schema|performance_schema)' )"
# either the code is 0, or the command failed; act accordingly.
[ $? -eq 0 ] || display_error_and_quit
......@@ -191,7 +191,7 @@ function dump_mysql_dbs {
echo " +-- found `echo "$DATABASES" | wc -l` databases..."
# is the db using authentication_string, or password for user passwords?
if [[ "$( mysql -h "$2" -u "$3" --password="$4" --batch --skip-column-names -e "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'mysql' AND TABLE_NAME = 'user' AND COLUMN_NAME = 'password';" )" == "0" ]]; then
if [[ "$( mysql -h "$1" -u "$2" --password="$3" --batch --skip-column-names -e "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'mysql' AND TABLE_NAME = 'user' AND COLUMN_NAME = 'password';" )" == "0" ]]; then
PWCOL="authentication_string"
else
PWCOL="password"
......@@ -199,7 +199,7 @@ function dump_mysql_dbs {
echo " +-- password column: $PWCOL"
# get the list of users
DBUSERS="$( mysql -h "$2" -u "$3" --password="$4" --batch --skip-column-names -e "SELECT CONCAT('',QUOTE(user),'@',QUOTE(host),':',$PWCOL) FROM mysql.user WHERE user<>'';")"
DBUSERS="$( mysql -h "$1" -u "$2" --password="$3" --batch --skip-column-names -e "SELECT CONCAT('',QUOTE(user),'@',QUOTE(host),':',$PWCOL) FROM mysql.user WHERE user<>'';")"
[ $? -eq 0 ] || display_error_and_quit
echo " +-- found `echo "$DBUSERS" | wc -l` users..."
......@@ -214,7 +214,7 @@ function dump_mysql_dbs {
DBUSER="$( echo "$user_host_pass" | cut -d ':' -f 1 )"
DBUSERPW="$( echo "$user_host_pass" | cut -d ':' -f 2 )"
# get the grants # add IDENTIFIED BY <password>, but only when there was no IDENTIFIED BY there already
DBGRANTS=`echo -ne "$DBGRANTS\n$( mysql -h "$2" -u "$3" --password="$4" --batch --skip-column-names -e "SHOW GRANTS FOR $DBUSER;" | sed -r -e "s/(IDENTIFIED BY.*)$/ IDENTIFIED BY PASSWORD '$DBUSERPW'/g" )"`
DBGRANTS=`echo -ne "$DBGRANTS\n$( mysql -h "$1" -u "$2" --password="$3" --batch --skip-column-names -e "SHOW GRANTS FOR $DBUSER;" | sed -r -e "s/(IDENTIFIED BY.*)$/ IDENTIFIED BY PASSWORD '$DBUSERPW'/g" )"`
[ $? -eq 0 ] || display_error_and_quit
done
......@@ -223,14 +223,14 @@ function dump_mysql_dbs {
echo " +-- working on db: $db"
# files
SCHEMA_FILE="$1/schemas/$db.schema.sql"
DUMP_FILE="$1/dumps/$db.dump.sql"
SCHEMA_FILE="$4/schemas/$db.schema.sql"
DUMP_FILE="$4/dumps/$db.dump.sql"
echo " +-- schema file : $SCHEMA_FILE"
echo " +-- dump file : $DUMP_FILE"
# schema
echo " +-- schema..."
mysqldump -h "$2" -u "$3" --password="$4" --add-drop-database --no-data --databases "$db" > "$SCHEMA_FILE"
mysqldump -h "$1" -u "$2" --password="$3" --add-drop-database --no-data --databases "$db" > "$SCHEMA_FILE"
[ $? -eq 0 ] || display_error_and_quit
# grants
......@@ -251,7 +251,7 @@ function dump_mysql_dbs {
# dump
echo " +-- dump..."
mysqldump -h "$2" -u "$3" --password="$4" --no-create-info --no-create-db --databases "$db" >> "$DUMP_FILE"
mysqldump -h "$1" -u "$2" --password="$3" --no-create-info --no-create-db --databases "$db" >> "$DUMP_FILE"
[ $? -eq 0 ] || display_error_and_quit
echo " +-- done."
......@@ -260,7 +260,7 @@ function dump_mysql_dbs {
# set the permissions so that
# this will not stop a root user from using these, but should make them stand out well enough
echo " +-- setting permissions on mysql.*"
chmod a= "$1"/*/mysql.*.sql
chmod a= "$4"/*/mysql.*.sql
# we're done
echo " +-- done."
......@@ -327,25 +327,25 @@ export -f create_readonly_postgres_user
#
# this assumes we have *at least* read-only access to databases we want to dump
# $1 - targed directory; the directory and structure within ($1/, $1/schemas/, $1/dumps/) will be made sure to exist
# $2 - database host
# $3 - user to connect as
# $4 - password for that user
# $1 - database host
# $2 - user to connect as
# $3 - password for that user
# $4 - targed directory; the directory and structure within ($4/, $4/schemas/, $4/dumps/) will be made sure to exist
# $5 and further - optional, the databases to dump (dumping all if empty)
#
# relevant: http://www.postgresql.org/docs/current/static/app-pgdump.html
#
# databases are dumped as schema (containing grants related to the database and CREATE statements) and data dump spearately,
# put in $1/schemas/$database_name.schema.sql and $1/dumps/$database_name.dump.sql respectively
# put in $4/schemas/$database_name.schema.sql and $4/dumps/$database_name.dump.sql respectively
function dump_postgres_dbs {
# clean the vars
clean_db_vars
# create the directory
mkdir -p "$1"
mkdir -p "$1/schemas"
mkdir -p "$1/dumps"
mkdir -p "$4"
mkdir -p "$4/schemas"
mkdir -p "$4/dumps"
# get the list of databases
# -l/--list List all available databases,then exit. Other non-connection
......@@ -353,7 +353,7 @@ function dump_postgres_dbs {
# -q/--quiet Specifies that psql should do its work quietly.
# -t/--tuples-only Turn off printing of column names and result row count footers, etc.
# ref. http://linuxcommand.org/man_pages/psql1.html
DATABASES="$( PGPASSWORD="$4" psql -h "$2" -U "$3" -lAqt | grep '|' | cut -d '|' -f 1 | egrep -v "template[0-9]" )"
DATABASES="$( PGPASSWORD="$3" psql -h "$1" -U "$2" -lAqt | grep '|' | cut -d '|' -f 1 | egrep -v "template[0-9]" )"
# either the code is 0, or the command failed; act accordingly.
[ $? -eq 0 ] || display_error_and_quit
......@@ -377,7 +377,7 @@ function dump_postgres_dbs {
fi
# list of users
DBUSERS="$( PGPASSWORD="$4" pg_dumpall -h "$2" --globals-only -U "$3" | egrep '(CREATE|ALTER) ROLE' )"
DBUSERS="$( PGPASSWORD="$3" pg_dumpall -h "$1" --globals-only -U "$2" | egrep '(CREATE|ALTER) ROLE' )"
[ $? -eq 0 ] || display_error_and_quit
echo " +-- found `echo "$DBUSERS" | egrep "^CREATE" | wc -l` users..."
......@@ -387,14 +387,14 @@ function dump_postgres_dbs {
echo " +-- working on db: $db"
# files
SCHEMA_FILE="$1/schemas/$db.schema.sql"
DUMP_FILE="$1/dumps/$db.dump.sql"
SCHEMA_FILE="$4/schemas/$db.schema.sql"
DUMP_FILE="$4/dumps/$db.dump.sql"
echo " +-- schema file : $SCHEMA_FILE"
echo " +-- dump file : $DUMP_FILE"
# schema
echo " +-- schema..."
PGPASSWORD="$4" pg_dump -h "$2" --create --clean --schema-only -U "$3" "$db" > "$SCHEMA_FILE"
PGPASSWORD="$3" pg_dump -h "$1" --create --clean --schema-only -U "$2" "$db" > "$SCHEMA_FILE"
[ $? -eq 0 ] || display_error_and_quit
# grants
......@@ -409,7 +409,7 @@ function dump_postgres_dbs {
# dump
echo " +-- dump..."
PGPASSWORD="$4" pg_dump -h "$2" --create --data-only --format custom -U "$3" "$db" >> "$DUMP_FILE"
PGPASSWORD="$3" pg_dump -h "$1" --create --data-only --format custom -U "$2" "$db" >> "$DUMP_FILE"
[ $? -eq 0 ] || display_error_and_quit
echo " +-- done."
......@@ -429,8 +429,8 @@ export -f dump_postgres_dbs
# restoring:
# https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-snapshots.html#_restoring_to_a_different_cluster
#
# $1 - targed directory
# $2 - database host
# $1 - database host
# $2 - targed directory
# $3 - optional; location where the temporary backup directory will be created;
# if not set, /tmp will be used
#
......@@ -469,12 +469,12 @@ function dump_elasticsearch_dbs {
# create the directory
echo " +-- setup..."
echo " +-- directory: $1"
mkdir -p "$1"
echo " +-- directory: $2"
mkdir -p "$2"
# create the temporary backup repository
echo " +-- ES snapshot repository in: $BACKUP_TEMP_DIR"
curl -X PUT -H "Content-Type: application/json" "http://$2:9200/_snapshot/$BACKUP_NAME" -d "{
curl -X PUT -H "Content-Type: application/json" "http://$1:9200/_snapshot/$BACKUP_NAME" -d "{
\"type\": \"fs\",
\"settings\": {
\"location\": \"$BACKUP_TEMP_DIR\"
......@@ -484,17 +484,17 @@ function dump_elasticsearch_dbs {
# create a snapshot, blocking until it's done
echo " +-- dump..."
curl -X PUT "http://$2:9200/_snapshot/$BACKUP_NAME/snapshot?wait_for_completion=true" || display_error_and_quit 'Error while generating the snapshot!'
curl -X PUT "http://$1:9200/_snapshot/$BACKUP_NAME/snapshot?wait_for_completion=true" || display_error_and_quit 'Error while generating the snapshot!'
echo
# copy the backup to a less temporary location
cp -a "$BACKUP_TEMP_DIR"/* "$1/" || display_error_and_quit 'Error while copying the snapshot data to the target directory!'
cp -a "$BACKUP_TEMP_DIR"/* "$2/" || display_error_and_quit 'Error while copying the snapshot data to the target directory!'
# cleanup
echo " +-- cleanup..."
curl -X DELETE "http://$2:9200/_snapshot/$BACKUP_NAME/snapshot" || display_error_and_quit 'Error while cleaning up the snapshot!'
curl -X DELETE "http://$1:9200/_snapshot/$BACKUP_NAME/snapshot" || display_error_and_quit 'Error while cleaning up the snapshot!'
echo
curl -X DELETE "http://$2:9200/_snapshot/$BACKUP_NAME/" || display_error_and_quit 'Error while cleaning up the backup repository!'
curl -X DELETE "http://$1:9200/_snapshot/$BACKUP_NAME/" || display_error_and_quit 'Error while cleaning up the backup repository!'
echo
rm -rf "$BACKUP_TEMP_DIR/*"
echo " +-- done..."
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment