-
Notifications
You must be signed in to change notification settings - Fork 6
/
BackupDataClient_pg.sh
117 lines (103 loc) · 4.47 KB
/
BackupDataClient_pg.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
:
#
# Author: Carlos Antonio Ruiz Gomez - globalqss
# Create a pg_dump file to export data just from one client
#
# History:
# 2011-01-13 - CarlosRuiz - First version
#
#############################################
# NOTES:
#
# If running this program psql throw errors like
# ERROR: column «ad_client_id» does not exist
# LINE 1: COPY (SELECT * FROM c_project_access WHERE AD_Client_ID IN (...
# it means the table is wrongly defined, the column ad_client_id is on dictionary, but not on database
# please fix the database problem synchronizing column to avoid these errors
#
# If importing the resulting dump throw errors like
# ERROR: insert or update on table «ad_preference» violates foreign key constraint «ad_user_preference»
# DETAIL: Key (ad_user_id)=(1000010) is not present in table «ad_user».
# it means you have some corrupted data on tenants, data from one tenant written in a different tenant
# please fix the data corruption to avoid these errors
#############################################
AD_CLIENT_ID="$1"
DBNAME="$2"
HOSTNAME="$3"
USERNAME="$4"
PORT="$5"
if [ $# -lt 5 ]
then
echo "
Usage: $0 ad_client_id dbname hostname username port
Parameters:
1 - AD_Client_ID (comma separated ids from client)
2 - DB Name
3 - Host name
4 - User name
5 - DB Port
Examples of usage:
To export System and GardenWorld from the database idempiere
BackupDataClient_pg.sh 0,11 idempiere localhost adempiere 5432
To export just System
BackupDataClient_pg.sh 0 idempiere localhost adempiere 5432
To export just GardenWorld
BackupDataClient_pg.sh 11 idempiere localhost adempiere 5432
" > /dev/tty
exit 1
fi
CLIENTVALUE=`echo "select string_agg(value,'+') from ad_client where ad_client_id in (:clientid)" | psql --tuples-only --no-align --quiet -h ${HOSTNAME} -p ${PORT} -U ${USERNAME} -d ${DBNAME} -v clientid=${AD_CLIENT_ID}`
OUTFILE=BackupData_${DBNAME}_${CLIENTVALUE}_${AD_CLIENT_ID}.dmp
# prepare a query to obtain the data in one transaction - filtering tables and clients
echo "BEGIN;" > /tmp/get$$
echo "
SELECT '
/* all tables with ad_client_id column */
SELECT ''--
-- Data for Name: '||table_name||'; Type: TABLE DATA; Schema: adempiere; Owner: adempiere
--
COPY '||table_name||' ('||col_columns||') FROM stdin;'';
COPY (SELECT '||col_columns||' FROM '||table_name||' WHERE ad_client_id in (:clientid)) TO stdout;
SELECT ''\.
'';'
FROM information_schema.tables t
JOIN (SELECT table_name AS col_table_name, string_agg(case when column_name='limit' then '\"limit\"' else column_name end,', ') AS col_columns from information_schema.columns c GROUP BY table_name) c ON (c.col_table_name=t.table_name)
WHERE table_schema='adempiere' AND table_type='BASE TABLE'
AND EXISTS (SELECT 1 FROM information_schema.columns c WHERE c.table_name=t.table_name AND c.column_name='ad_client_id')
UNION
/* ad_pinstance_log - required but it doesn't have ad_client_id */
SELECT '
SELECT ''--
-- Data for Name: '||table_name||'; Type: TABLE DATA; Schema: adempiere; Owner: adempiere
--
COPY '||table_name||' ('||col_columns||') FROM stdin;'';
COPY (SELECT '||col_columns||' FROM '||table_name||' WHERE ad_pinstance_id IN (SELECT ad_pinstance_id FROM ad_pinstance WHERE ad_client_id IN (:clientid))) TO stdout;
SELECT ''\.
'';'
FROM information_schema.tables t
JOIN (SELECT table_name AS col_table_name, string_agg(case when column_name='limit' then '\"limit\"' else column_name end,', ') AS col_columns from information_schema.columns c GROUP BY table_name) c ON (c.col_table_name=t.table_name)
WHERE table_name='ad_pinstance_log'
UNION
/* dual - required but it doesn't have ad_client_id */
SELECT '
SELECT ''--
-- Data for Name: '||table_name||'; Type: TABLE DATA; Schema: adempiere; Owner: adempiere
--
COPY '||table_name||' ('||col_columns||') FROM stdin;'';
COPY (SELECT '||col_columns||' FROM '||table_name||') TO stdout;
SELECT ''\.
'';'
FROM information_schema.tables t
JOIN (SELECT table_name AS col_table_name, string_agg(case when column_name='limit' then '\"limit\"' else column_name end,', ') AS col_columns from information_schema.columns c GROUP BY table_name) c ON (c.col_table_name=t.table_name)
WHERE table_name='dual'
ORDER BY 1
" | psql --tuples-only --no-align --quiet -h ${HOSTNAME} -p ${PORT} -U ${USERNAME} -d ${DBNAME} >> /tmp/get$$
# get the data
echo "BEGIN;
" > ${OUTFILE}
psql --tuples-only --no-align --quiet -h ${HOSTNAME} -p ${PORT} -U ${USERNAME} -d ${DBNAME} -v clientid=${AD_CLIENT_ID} -f /tmp/get$$ >> ${OUTFILE}
echo "
COMMIT;" >> ${OUTFILE}
tar cvjf ${OUTFILE}.tbz ${OUTFILE}
rm ${OUTFILE}
rm /tmp/get$$