-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_folder.sh
More file actions
57 lines (48 loc) · 1.47 KB
/
sql_folder.sh
File metadata and controls
57 lines (48 loc) · 1.47 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
#!/bin/bash
SQL_FOLDER=$1
[ -z $SQL_FOLDER ] && SQL_FOLDER=$(pwd)
HOME_FOLDER=$2
[ -z $HOME_FOLDER ] && HOME_FOLDER=~
# Config
DB_PASS=$(cat $HOME_FOLDER/psql/.password)
DB_USER=$(cat $HOME_FOLDER/psql/.username)
DB_NAME=$(cat $HOME_FOLDER/psql/.database)
DB_PORT=$(cat $HOME_FOLDER/psql/.port)
DB_HOST=$(cat $HOME_FOLDER/psql/.host)
# Connect to DB and run Query
export PGPASSWORD=$DB_PASS
# Check if no arguments provided
if [ "$#" -lt 1 ] ; then
echo "Usage: $0 sql_folder home_folder"
exit 0
fi
# Iterate over each file name provided as argument
for SQL_FILES_DIR in "$SQL_FOLDER"
do
# Check if file exists and is a regular file
if [ -d "$SQL_FILES_DIR" ]; then
# Loop through every SQL file in the directory
# List all files in the folder
for SQL_FILE in $(ls $SQL_FILES_DIR)
do
SQL_FILE_PATH="$SQL_FILES_DIR/$SQL_FILE"
if [ -f "$SQL_FILE_PATH" ];
then
echo $SQL_FILE_PATH
# If $SQL_FILE is a file, import it
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f $SQL_FILE_PATH
#echo "Imported $SQL_FILE successfully"
# check connection status
if [ "$?" -eq "0" ]
then
echo "Successfully connected to the DB."
else
echo "Error in connecting to the DB."
fi
fi
done
else
echo "$SQL_FILES_DIR does not exist or is not a regular folder"
fi
done
unset PGPASSWORD