-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOSTGRES-dump.sh
More file actions
executable file
·63 lines (55 loc) · 1.34 KB
/
Copy pathPOSTGRES-dump.sh
File metadata and controls
executable file
·63 lines (55 loc) · 1.34 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
#!/bin/bash
# Function to display usage information
function display_usage {
echo "Usage: $0 [-h <host>] [-U <user>] [-p <port>] [-d <database>] [-f <output_file>]"
exit 1
}
# Initialize variables with default values
host=""
user=""
port=""
database=""
output_file=""
# Parse command-line arguments using getopts
while getopts ":h:U:p:d:f:" opt; do
case $opt in
h)
host="$OPTARG"
;;
U)
user="$OPTARG"
;;
p)
port="$OPTARG"
;;
d)
database="$OPTARG"
;;
f)
output_file="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG"
display_usage
;;
:)
echo "Option -$OPTARG requires an argument."
display_usage
;;
esac
done
# Check if mandatory parameters are provided
if [[ -z "$host" || -z "$user" || -z "$port" ]]; then
echo "Error: Missing required parameters."
display_usage
fi
# Generate the PostgreSQL dump using the provided parameters
command="pg_dump --host=$host --username=$user --port=$port"
if [[ -n "$database" ]]; then
command+=" --dbname=$database"
fi
if [[ -n "$output_file" ]]; then
command+=" --file=$output_file"
fi
eval "$command"
echo "PostgreSQL dump generated successfully."