-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopiardb
82 lines (67 loc) · 1.68 KB
/
copiardb
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
#!/usr/bin/env bash
# Made by Sinfallas <[email protected]>
# Licence: GPL-2
LC_ALL=C
SECONDS=0
IFS=" "
LOCKFILE="/tmp/$(basename $0)_$(whoami)"
LOCKFD="150"
nom=$(basename "$0")
function clean_1() {
rm -f /run/$(basename $0).pid
rm -f "$LOCKFILE"
}
function ayuda() {
echo "Ejemplo: $nom IP1 PORT1 IP2 PORT2 DBNAME USER1 PASSWORD1 USER2 PASSWORD2"
exit 1
}
function lock() {
echo {LOCKFD}>$LOCKFILE
flock -n $LOCKFD
}
function exit_error() {
echo "ERROR: Ya hay una instancia en ejecución. Saliendo"
exit 1
}
echo "$BASHPID" > /run/$(basename $0).pid
trap "clean_1; exit" 0 1 2 3 15
lock || exit_error
if [[ -z "$IP1" ]]; then
ayuda
fi
if [[ -z "$PORT1" ]]; then
ayuda
fi
if [[ -z "$IP2" ]]; then
ayuda
fi
if [[ -z "$PORT2" ]]; then
ayuda
fi
if [[ -z "$DBNAME" ]]; then
ayuda
fi
if [[ -z "$USER1" ]]; then
ayuda
fi
if [[ -z "$PASS1" ]]; then
ayuda
fi
if [[ -z "$USER2" ]]; then
ayuda
fi
if [[ -z "$PASS2" ]]; then
ayuda
fi
rm -f /app/*.dump
clear
echo "backup"
verori=$(PGPASSWORD=$PASS1 psql -h $IP1 -p $PORT1 -U $USER1 -d $DBNAME -c 'show server_version;' | grep -v '(1 row)' | grep -v 'server_version' | sed 1d | sed 2d | cut -c 1-6)
echo "Version: $verori"
PGPASSWORD=$PASS1 pg_dump --quote-all-identifiers -Fc -h $IP1 -p $PORT1 -U $USER1 -d $DBNAME > /app/$DBNAME.dump
echo "restore"
verdes=$(PGPASSWORD=$PASS2 psql -h $IP2 -p $PORT2 -U $USER2 -d $DBNAME -c 'show server_version;' | grep -v '(1 row)' | grep -v 'server_version' | sed 1d | sed 2d | cut -c 1-6)
echo "Version: $verdes"
PGPASSWORD=$PASS2 pg_restore --single-transaction -h $IP2 -p $PORT2 -U $USER2 -d $DBNAME /app/$DBNAME.dump
echo "duracion $SECONDS segundos"
echo "Finalizado."