-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathimport.sh
executable file
·57 lines (45 loc) · 1.5 KB
/
import.sh
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
#!/usr/bin/env bash
set -e
if [ ! -f "backup.sql" ]; then
echo "The expected file to be imported (backup.sql) is not present in the current directory!"
exit 1
fi
if ! psql --version COMMAND &> /dev/null
then
echo "PSQL could not be found. Please make sure you have the PostgreSQL client installed https://www.postgresql.org/download/"
exit
fi
read -p "Input target HOSTNAME: " hostname
read -p "Input target PORT [default = 5432]: " port
port=${port:-5432}
read -p "Input target DATABASE NAME [default = sentry]: " database_name
database_name=${database_name:-sentry}
read -p "Input target USERNAME [default = sentry]: " username
username=${username:-sentry}
echo
echo
echo "<< Dropping target database (if exists) and recreating it >>"
echo "Input target PASSWORD: "
psql --host=$hostname --port=$port --username=$username --dbname=postgres --password << EOF
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
pid <> pg_backend_pid()
-- don't kill the connections to other databases
AND datname = '${database_name}';
DROP DATABASE IF EXISTS "${database_name}";
CREATE DATABASE "${database_name}";
EOF
echo
echo
echo "<< Importing data into database >>"
echo "Input target PASSWORD: "
pv backup.sql | psql --host=$hostname --port=$port --username=$username --dbname=$database_name --password
echo
echo
echo "Process finished successfully!"
echo
echo "Managed to import $(cat backup.sql | wc -l) SQL commands into target database!"