浏览代码

added to database_functions.sh script

joel
Simon Bowie 2 年前
父节点
当前提交
142959c69a
共有 2 个文件被更改,包括 37 次插入10 次删除
  1. +2
    -0
      .gitignore
  2. +35
    -10
      database_functions.sh

+ 2
- 0
.gitignore 查看文件

.DS_Store .DS_Store
instance instance
database_functions_live.sh database_functions_live.sh
old_database_schema
db_exports

+ 35
- 10
database_functions.sh 查看文件

# Display Help # Display Help
echo "This script performs database functions for the ExPub Compendium" echo "This script performs database functions for the ExPub Compendium"
echo echo
echo "Syntax: database_functions.sh [-l|h|e]"
echo "Syntax: database_functions.sh [-l|h|e|i|c]"
echo "options:" echo "options:"
echo "l Print the MIT License notification." echo "l Print the MIT License notification."
echo "h Print this Help." echo "h Print this Help."
echo "e Export."
echo "i Import."
echo "e Export whole database."
echo "i Import whole database."
echo "c Export single table as CSV."
echo "v Import CSV file to table."
echo echo
} }


Export() Export()
{ {
docker exec -it $CONTAINER mysqldump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_FILENAME`date +"%Y%m%d"`.sql
docker exec -it $CONTAINER mysqldump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_SQL_FILENAME`date +"%Y%m%d"`.sql
} }


Import() Import()
{ {
docker exec -i $CONTAINER mysql -u $USERNAME -p$PASSWORD $DATABASE < $DIRECTORY/$IMPORT_FILENAME
docker exec -i $CONTAINER mysql -u $USERNAME -p$PASSWORD $DATABASE < $IMPORT_SQL_DIRECTORY/$IMPORT_SQL_FILENAME
}

CSV_table_export()
{
docker exec -it $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE --batch -e 'SELECT * FROM $TABLE'" > $EXPORT_CSV_DIRECTORY/$CSV_FILENAME.txt
}

CSV_table_import()
{
docker cp $IMPORT_CSV_FILE $CONTAINER:/tmp/import_file

docker exec -i $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE -e 'LOAD DATA LOCAL INFILE '\''/tmp/import_file'\'' REPLACE INTO TABLE $TABLE FIELDS TERMINATED BY '\''\t'\'' LINES TERMINATED BY '\''\n'\'' IGNORE 1 ROWS;'"
} }
############################################################ ############################################################
############################################################ ############################################################
DATABASE=toolkit DATABASE=toolkit
USERNAME=xxxxxxxx USERNAME=xxxxxxxx
PASSWORD=xxxxxxxx PASSWORD=xxxxxxxx
EXPORT_FILENAME=toolkit_db_
DIRECTORY="/Users/ad7588/Downloads"
IMPORT_FILENAME=toolkit_db.sql
EXPORT_SQL_FILENAME=toolkit_db_
IMPORT_SQL_DIRECTORY="/Users/ad7588/Downloads"
IMPORT_SQL_FILENAME=toolkit_db.sql
EXPORT_CSV_DIRECTORY="./db_exports"
CSV_FILENAME=$2`date +"%Y%m%d"`


# Get the options # Get the options
while getopts ":hlimzaespxdw" option; do
case $option in
while getopts ":hleicv" flag; do
case $flag in
l) # display License l) # display License
License License
exit;; exit;;
i) # import database from file i) # import database from file
Import Import
exit;; exit;;
c) # export single table as CSV
TABLE=$2
CSV_table_export
exit;;
v) # import single CSV to table
TABLE=$2
IMPORT_CSV_FILE=$3
CSV_table_import
exit;;
\?) # Invalid option \?) # Invalid option
echo "Error: Invalid option" echo "Error: Invalid option"
exit;; exit;;

正在加载...
取消
保存