3 # Copyright 2014, Equinox Software, Inc.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # Simple script to export records belonging to a specific Evergreen OU
20 # and its descendents.
22 # Usage: export_evergreen_library shortname scratch_dir output_dir db_conn_params
27 # $ ./export_evergreen_library SYS1 inter out '-U evergreen'
29 # The script emits MARC bibs, bibs + MFHDs, undeleted patrons, all items
30 # for the OU, all loans for the OU's patrons and items, current hold
31 # requests, and patron billings. It also exports all serials for the
34 # The script expects to be run on a box that can access the Evergreen database
35 # via psql and an OpenSRF stack.
42 export PATH=/openils/bin:$PATH
44 function select_bibs {
45 echo Selecting bib IDs for $baseou
46 psql $dbconn -A -t -vbaseou=$baseou > $workdir/bib_ids <<_END_
47 SELECT DISTINCT bre.id
48 FROM biblio.record_entry bre
49 JOIN asset.call_number acn ON (bre.id = acn.record)
51 AND acn.owning_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')
56 function export_marc_bibs {
57 echo Exporting bibs in MARC format
58 cat $workdir/bib_ids | marc_export --format USMARC --encoding UTF-8 > $outdir/${baseou}_bibs.mrc
61 function select_bibs_with_mfhds {
62 echo Selecting MFHD IDs for $baseou
63 psql $dbconn -A -t -vbaseou=$baseou > $workdir/bibs_with_serial_ids <<_END_
64 SELECT DISTINCT sre.record
65 FROM serial.record_entry sre
67 AND sre.owning_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')
72 function export_bibs_with_mfhds {
73 echo Exporting MFHDs in MARC format
74 if [[ ! -s $workdir/bibs_with_serial_ids ]];
76 # work-around for marc_export
77 cat /dev/null $outdir/${baseou}_bibs_and_mfhds
79 cat $workdir/bibs_with_serial_ids | marc_export --format USMARC --encoding UTF-8 --mfhd > $outdir/${baseou}_bibs_and_mfhds.mrc
83 function export_table_description {
86 psql $dbconn -c "\\d $table" > $subdir/$table.description
87 psql $dbconn -c "\\dt+ $table" >> $subdir/$table.description
90 function export_entire_table {
93 echo Exporting table $table
94 export_table_description $table $subdir
95 psql $dbconn -A -t -c "\\COPY $table TO $subdir/$table.tsv"
98 function export_table_ou_subset {
103 echo "Exporting table $table (limited to OU $baseou)"
104 export_table_description $table $subdir
105 psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou') $clause) TO $subdir/$table.tsv"
108 function export_table_item_subset {
113 echo "Exporting table $table (limited by items)"
114 export_table_description $table $subdir
115 psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol IN (SELECT id FROM asset.copy WHERE circ_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')) $clause) TO $subdir/$table.tsv"
118 function export_table_patron_subset {
123 echo "Exporting table $table (limited by patrons)"
124 export_table_description $table $subdir
125 psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol IN (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou') AND NOT deleted) $clause) TO $subdir/$table.tsv"
128 function export_table_patron_item_subset {
134 echo "Exporting table $table (limited by patrons and items)"
135 export_table_description $table $subdir
136 psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol1 IN (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou') AND NOT deleted) AND $limitcol2 IN (SELECT id FROM asset.copy WHERE circ_lib IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou')) $clause) TO $subdir/$table.tsv"
139 function export_table_xact_subset {
144 echo "Exporting table $table (limited by xacts)"
145 export_table_description $table $subdir
146 psql $dbconn -A -t -c "\\COPY (SELECT * FROM $table WHERE $limitcol IN (SELECT id FROM money.billable_xact WHERE usr IN (SELECT id FROM actor.usr WHERE home_ou IN (SELECT (actor.org_unit_descendants(id)).id FROM actor.org_unit WHERE shortname = '$baseou') AND NOT deleted)) $clause) TO $subdir/$table.tsv"
149 function export_items {
150 mkdir -p $outdir/items
151 export_entire_table config.copy_status $outdir/items
152 export_entire_table config.circ_modifier $outdir/items
153 export_entire_table config.floating_group $outdir/items
154 export_entire_table config.floating_group_member $outdir/items
155 export_entire_table config.rule_age_hold_protect $outdir/items
156 export_entire_table config.rule_circ_duration $outdir/items
157 export_entire_table config.rule_max_fine $outdir/items
158 export_entire_table config.rule_recurring_fine $outdir/items
159 export_entire_table asset.call_number_prefix $outdir/items
160 export_entire_table asset.call_number_suffix $outdir/items
161 export_entire_table asset.stat_cat $outdir/items
162 export_entire_table asset.stat_cat_entry $outdir/items
163 export_table_ou_subset actor.org_unit id $outdir/items
164 export_table_ou_subset asset.copy_location owning_lib $outdir/items
165 export_table_ou_subset asset.copy circ_lib $outdir/items
166 export_table_ou_subset asset.call_number owning_lib $outdir/items
167 export_table_item_subset asset.copy_note owning_copy $outdir/items
168 export_table_item_subset asset.stat_cat_entry_copy_map owning_copy $outdir/items
171 function export_patrons {
172 mkdir -p $outdir/patrons
173 export_entire_table config.net_access_level $outdir/patrons
174 export_entire_table config.standing_penalty $outdir/patrons
175 export_entire_table actor.stat_cat $outdir/patrons
176 export_entire_table actor.stat_cat_entry $outdir/patrons
177 export_table_ou_subset actor.usr home_ou $outdir/patrons "AND NOT deleted"
178 export_table_patron_subset actor.card usr $outdir/patrons
179 export_table_patron_subset actor.usr_setting usr $outdir/patrons
180 export_table_patron_subset actor.usr_standing_penalty usr $outdir/patrons
181 export_table_patron_subset actor.usr_note usr $outdir/patrons
182 export_table_patron_subset actor.usr_address usr $outdir/patrons
183 export_table_patron_subset actor.usr_saved_search owner $outdir/patrons
184 export_table_patron_subset actor.stat_cat_entry_usr_map target_usr $outdir/patrons
187 function export_loans {
188 mkdir -p $outdir/loans
189 export_table_patron_item_subset action.circulation usr target_copy $outdir/loans
192 function export_hold_requests {
193 mkdir -p $outdir/current_hold_requests
194 export_table_patron_subset action.hold_request usr $outdir/current_hold_requests 'AND cancel_time IS NULL AND fulfillment_time IS NULL AND (expire_time IS NULL OR expire_time > NOW())'
197 function export_billings {
198 mkdir -p $outdir/patron_bills
199 export_entire_table config.billing_type $outdir/patron_bills
200 export_table_xact_subset money.billing xact $outdir/patron_bills
201 export_table_xact_subset money.payment xact $outdir/patron_bills
202 export_table_patron_subset money.grocery usr $outdir/patron_bills
205 function export_serials {
206 mkdir -p $outdir/serials
207 export_entire_table serial.basic_summary $outdir/serials
208 export_entire_table serial.index_summary $outdir/serials
209 export_entire_table serial.supplement_summary $outdir/serials
210 export_entire_table serial.caption_and_pattern $outdir/serials
211 export_entire_table serial.distribution $outdir/serials
212 export_entire_table serial.distribution_note $outdir/serials
213 export_entire_table serial.issuance $outdir/serials
214 export_entire_table serial.item $outdir/serials
215 export_entire_table serial.item_note $outdir/serials
216 export_entire_table serial.materialized_holding_code $outdir/serials
217 export_entire_table serial.stream $outdir/serials
218 export_entire_table serial.unit $outdir/serials
219 export_entire_table serial.routing_list_user $outdir/serials
220 export_entire_table serial.subscription $outdir/serials
221 export_entire_table serial.subscription_note $outdir/serials
232 select_bibs_with_mfhds
233 export_bibs_with_mfhds