-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase_functions.sh
More file actions
executable file
·159 lines (144 loc) · 5.79 KB
/
database_functions.sh
File metadata and controls
executable file
·159 lines (144 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
# @name: database_functions.sh
# @creation_date: 2022-11-02
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <simonxix@simonxix.com>
# @purpose: Runs database functions for the Experimental Publishing Compendium
# @acknowledgements:
# https://www.redhat.com/sysadmin/arguments-options-bash-scripts
# https://askubuntu.com/questions/1389904/read-from-env-file-and-set-as-bash-variables
############################################################
# subprograms #
############################################################
License()
{
echo 'Copyright 2022-2025 Simon Bowie <simonxix@simonxix.com>'
echo
echo 'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:'
echo
echo 'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.'
echo
echo 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.'
}
Help()
{
# Display Help
echo "This script performs database functions for the Experimental Publishing Compendium"
echo
echo "Syntax: database_functions.sh [-l|h|e|i|c|v|d|b]"
echo "options:"
echo "l Print the MIT License notification."
echo "h Print this Help."
echo "e Export whole database."
echo "i Import whole database."
echo "c Export single table as tab-delimited txt."
echo "v Import tab-delimited txt file to table."
echo "d Drop table."
echo "b Backup database to remote storage."
echo
}
Export()
{
docker exec -i $DATABASE_CONTAINER mariadb-dump --single-transaction -u $DATABASE_USERNAME -p$DATABASE_PASSWORD $MYSQL_DATABASE > $EXPORT_DIRECTORY/$EXPORT_SQL_FILENAME`date +"%Y%m%d"`.sql
}
Import()
{
docker exec -i $DATABASE_CONTAINER mariadb -u $DATABASE_USERNAME -p$DATABASE_PASSWORD $MYSQL_DATABASE < $IMPORT_SQL_FILE
}
Table_export()
{
docker exec -i $DATABASE_CONTAINER bash -c "mariadb -u $DATABASE_USERNAME -p$DATABASE_PASSWORD $MYSQL_DATABASE --batch -e 'SELECT * FROM $TABLE'" > $EXPORT_TXT_DIRECTORY/$EXPORT_TXT_FILENAME.txt
}
Table_import()
{
docker cp $IMPORT_TXT_FILE $DATABASE_CONTAINER:/tmp/import_file
docker exec -i $DATABASE_CONTAINER bash -c "mariadb -u $DATABASE_USERNAME -p$DATABASE_PASSWORD $MYSQL_DATABASE -e 'LOAD DATA LOCAL INFILE '\''/tmp/import_file'\'' REPLACE INTO TABLE $TABLE FIELDS TERMINATED BY '\''\t'\'' LINES TERMINATED BY '\''\r'\'' IGNORE 1 ROWS;'"
}
Drop_table()
{
docker exec -i $DATABASE_CONTAINER bash -c "mariadb -u $DATABASE_USERNAME -p$DATABASE_PASSWORD $MYSQL_DATABASE -e 'DROP TABLE IF EXISTS $TABLE;'"
}
Backup()
{
docker exec -i $DATABASE_CONTAINER mariadb-dump --single-transaction -u $DATABASE_USERNAME -p$DATABASE_PASSWORD $MYSQL_DATABASE > $EXPORT_DIRECTORY/$BACKUP_SQL_FILENAME.sql
scp $EXPORT_DIRECTORY/$BACKUP_SQL_FILENAME.sql $STORAGE_USERNAME@$STORAGE_SERVER:$STORAGE_DIRECTORY
}
############################################################
############################################################
# main program #
############################################################
############################################################
# retrieve variables from .env file (see .env.template for template)
# source the .env file from the script's directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/.env"
# error message for no flags
if (( $# == 0 )); then
Help
exit 1
fi
# get the options
while getopts ":hleicvdb" flag; do
case $flag in
l) # display License
License
exit;;
h) # display Help
Help
exit;;
e) # export whole database
Export
exit;;
i) # import database from file
if [ -z "$2" ]
then
echo "-i requires a file as an argument"
echo
echo "Syntax: database_functions.sh -i [file]"
else
IMPORT_SQL_FILE=$2
Import
exit 1
fi;;
c) # export single table as tab-delimited txt
if [ -z "$2" ]
then
echo "-c requires a table name as an argument"
echo
echo "Syntax: database_functions.sh -c [table name]"
else
TABLE=$2
Table_export
exit 1
fi;;
v) # import single tab-delimited txt to table
if [ -z "$2" ] || [ -z "$3" ]
then
echo "-v requires a table name as an argument and the file to be imported"
echo
echo "Syntax: database_functions.sh -v [table name] [file]"
else
TABLE=$2
IMPORT_TXT_FILE=$3
Table_import
exit 1
fi;;
d) # drop table
if [ -z "$2" ]
then
echo "-d requires a table name as an argument"
echo
echo "Syntax: database_functions.sh -d [table name]"
else
TABLE=$2
Drop_table
exit 1
fi;;
b) # backup database to secure storage
Backup
exit;;
\?) # invalid option
Help
exit;;
esac
done