#!/usr/bin/perl use strict; use warnings; use DBI; use Data::Dumper; use Env qw( HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR ); use open ':encoding(utf8)'; use Cwd 'abs_path'; use Cwd qw(getcwd); use FindBin; my $mig_bin = "$FindBin::Bin/"; use lib "$FindBin::Bin/"; use XML::Writer; use open ':std', ':encoding(UTF-8)'; use Encode qw( decode_utf8 ); use KMig; my $dbh = KMig::db_connect(); $dbh->{mysql_enable_utf8mb4} = 1; #to do check for array passed and if not present then use tags my @taglist = @ARGV; my $arg_list_length = scalar @taglist; if($arg_list_length < 1) { @taglist = ("authorisedvalues","calendar","circrules","itemtypes","libraries","patrontypes","preferences","smsproviders"); } #borrowerattributes $MIGGITDIR =~ s/\/\//\//; my $timestamp = create_timestamp(); foreach my $backup (@taglist) { my $backupfile; if ($backup eq 'authorisedvalues') { $backupfile = $MIGGITDIR . 'authorisedvalues' . '.' . $timestamp . '.xml'; print "Backing up $backupfile ... \n"; backup_authorisedvalues($dbh,$backupfile); } if ($backup eq 'borrowerattributes') { $backupfile = $MIGGITDIR . 'borrower_attributes' . '.' . $timestamp . '.xml'; print "Restoring from $restorefile ... \n"; backup_borrower_attributes($dbh,$backupfile); } if ($backup eq 'calendar') { $backupfile = $MIGGITDIR . 'calendar' . '.' . $timestamp . '.xml'; print "Restoring from $restorefile ... \n"; backup_calendar($dbh,$backupfile); } if ($backup eq 'circrules') { $backupfile = $MIGGITDIR . 'circrules' . '.' . $timestamp . '.xml'; print "Restoring from $restorefile ... \n"; backup_circrules($dbh,$backupfile); } if ($backup eq 'itemtypes') { $backupfile = $MIGGITDIR . 'itemtypes' . '.' . $timestamp . '.xml'; print "Restoring from $restorefile ... \n"; backup_itemtypes($dbh,$backupfile); } if ($backup eq 'libraries') { $backupfile = $MIGGITDIR . 'libraries' . '.' . $timestamp . '.xml'; print "Restoring from $restorefile ... \n"; backup_libraries($dbh,$backupfile); } if ($backup eq 'patrontypes') { $backupfile = $MIGGITDIR . 'patrontypes' . '.' . $timestamp . '.xml'; print "Restoring from $restorefile ... \n"; backup_patrontypes($dbh,$backupfile); } if ($backup eq 'preferences') { $backupfile = $MIGGITDIR . 'systempreferences' . '.' . $timestamp . '.xml'; print "Restoring from $restorefile ... \n"; backup_preferences($dbh,$backupfile); } if ($backup eq 'smsproviders') { $backupfile = $MIGGITDIR . 'smsproviders' . '.' . $timestamp . '.xml'; print "Restoring from $restorefile ... \n"; backup_smsproviders($dbh,$backupfile); } } sub backup_authorisedvalues { my $dbh = shift; my $backupfile = shift; open(my $fh, '>', $backupfile) or die "Could not open $backupfile!"; my $xml = XML::Writer->new(OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag('authorisedvalues'); my $query = "SELECT category,authorised_value,lib,lib_opac,imageurl FROM authorised_values WHERE category != 'ITEMTYPECAT'"; my $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('value'); $xml->dataElement('category', $row[0]); $xml->dataElement('authorised_value', $row[1]); $xml->dataElement('lib', $row[2]); $xml->dataElement('lib_opac', $row[3]); $xml->dataElement('imageurl', $row[4]); $xml->endTag('value'); } $xml->endTag('authorisedvalues'); $xml->end(); close $fh; return; } sub backup_borrower_attributes { my $dbh = shift; my $backupfile = shift; open(my $fh, '>', $backupfile) or die "Could not open $backupfile!"; my $xml = XML::Writer->new(OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag('document'); $xml->startTag('borrower_attribute_types'); my $query = "SELECT code,description,repeatable,opac_display,opac_editable,staff_searchable,authorised_value_category,display_checkout,category_code,class FROM borrower_attribute_types"; my $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('value'); $xml->dataElement('code', $row[0]); $xml->dataElement('description', $row[1]); $xml->dataElement('repeat', $row[2]); $xml->dataElement('opac_display', $row[3]); $xml->dataElement('staff_searchable', $row[4]); $xml->dataElement('auth_value_cat', $row[5]); $xml->dataElement('display_checkout', $row[6]); $xml->dataElement('category_code', $row[7]); $xml->dataElement('class', $row[8]); $xml->endTag('value'); } $xml->endTag('borrower_attribute_types'); $xml->startTag('authorised_value_categories'); $query = "SELECT category_name FROM authorised_value_categories WHERE category_name IN (select category_name from borrower_attribute_types)"; $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->dataElement('value', $row[0]); } $xml->endTag('authorised_value_categories'); $xml->startTag('authorised_values'); $query = "SELECT category,authorised_value,lib,lib_opac,imageurl FROM authorised_values where category in (select category_code from borrower_attribute_types);"; $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('value'); $xml->dataElement('category', $row[0]); $xml->dataElement('authvalue', $row[1]); $xml->dataElement('lib', $row[2]); $xml->dataElement('lib_opac', $row[3]); $xml->dataElement('image_url', $row[4]); $xml->endTag('value'); } $xml->endTag('authorised_values'); $xml->endTag('document'); $xml->end(); close $fh; return; } sub backup_calendar { my $dbh = shift; my $backupfile = shift; open(my $fh, '>', $backupfile) or die "Could not open $backupfile!"; my $xml = XML::Writer->new(OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag('holidays'); my $query = "SELECT branchcode,weekday,day,month,title,description FROM repeatable_holidays"; my $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('repeatable'); $xml->dataElement('branchcode',$row[0]); $xml->dataElement('weekday',$row[1]); $xml->dataElement('day',$row[2]); $xml->dataElement('month',$row[3]); $xml->dataElement('title',$row[4]); $xml->dataElement('description',$row[5]); $xml->endTag('repeatable'); } $query = "SELECT branchcode,day,month,year,isexception,title,description FROM special_holidays"; $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('special'); $xml->dataElement('branchcode',$row[0]); $xml->dataElement('day',$row[1]); $xml->dataElement('month',$row[2]); $xml->dataElement('year',$row[3]); $xml->dataElement('isexception',$row[4]); $xml->dataElement('title',$row[5]); $xml->dataElement('description',$row[6]); $xml->endTag('special'); } $xml->endTag('holidays'); $xml->end(); close $fh; return; } sub backup_circrules { my $dbh = shift; my $backupfile = shift; open(my $fh, '>', $backupfile) or die "Could not open $backupfile!"; my $xml = XML::Writer->new(OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag('document'); $xml->startTag('circ'); my $query = "SELECT branchcode, categorycode, itemtype, rule_name, rule_value FROM circulation_rules"; my $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('rule'); $xml->dataElement('branchcode', $row[0]); $xml->dataElement('categorycode', $row[1]); $xml->dataElement('itemtype', $row[2]); $xml->dataElement('rule_name', $row[3]); $xml->dataElement('rule_value', $row[4]); $xml->endTag('rule'); } $xml->endTag('circ'); $xml->startTag('issuing'); $query = "SELECT categorycode,itemtype,restrictedtype,rentaldiscount,reservecharge,fine,finedays,maxsuspensiondays,suspension_chargeperiod,firstremind,chargeperiod,chargeperiod_charge_at,accountsent,issuelength,lengthunit,hardduedate,hardduedatecompare,renewalsallowed,renewalperiod,norenewalbefore,auto_renew,no_auto_renewal_after,no_auto_renewal_after_hard_limit,reservesallowed,holds_per_record,holds_per_day,branchcode,overduefinescap,cap_fine_to_replacement_price,onshelfholds,opacitemholds,article_requests,note FROM issuingrules"; $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('rule'); $xml->dataElement('categorycode', $row[0]); $xml->dataElement('itemtype', $row[1]); $xml->dataElement('restrictedtype', $row[2]); $xml->dataElement('rentaldiscount', $row[3]); $xml->dataElement('reservecharge', $row[4]); $xml->dataElement('fine', $row[5]); $xml->dataElement('finedays', $row[6]); $xml->dataElement('maxsuspensiondays', $row[7]); $xml->dataElement('suspension_chargeperiod', $row[8]); $xml->dataElement('firstremind', $row[9]); $xml->dataElement('chargeperiod', $row[10]); $xml->dataElement('chargeperiod_charge_at', $row[11]); $xml->dataElement('accountsent', $row[12]); $xml->dataElement('issuelength', $row[13]); $xml->dataElement('lengthunit', $row[14]); $xml->dataElement('hardduedate', $row[15]); $xml->dataElement('hardduedatecompare', $row[16]); $xml->dataElement('renewalsallowed', $row[17]); $xml->dataElement('renewalperiod', $row[18]); $xml->dataElement('norenewalbefore', $row[19]); $xml->dataElement('auto_renew', $row[20]); $xml->dataElement('no_auto_renewal_after', $row[21]); $xml->dataElement('no_auto_renewal_after_hard_limit', $row[22]); $xml->dataElement('reservesallowed', $row[23]); $xml->dataElement('holds_per_record', $row[24]); $xml->dataElement('holds_per_day', $row[25]); $xml->dataElement('branchcode', $row[26]); $xml->dataElement('overduefinescap', $row[27]); $xml->dataElement('cap_fine_to_replacement_price', $row[28]); $xml->dataElement('onshelfholds', $row[29]); $xml->dataElement('opacitemholds', $row[30]); $xml->dataElement('article_requests', $row[31]); $xml->dataElement('note', $row[32]); $xml->endTag('rule'); } $xml->endTag('issuing'); $xml->endTag('document'); $xml->end(); close $fh; return; } sub backup_itemtypes { my $dbh = shift; my $backupfile = shift; open(my $fh, '>', $backupfile) or die "Could not open $backupfile!"; my $xml = XML::Writer->new(OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag('document'); $xml->startTag('itemtypes'); my $query = "SELECT itemtype, description, rentalcharge, rentalcharge_daily, rentalcharge_hourly, defaultreplacecost, processfee, notforloan, imageurl, summary, checkinmsg, checkinmsgtype, sip_media_type, hideinopac, searchcategory FROM itemtypes"; my $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('value'); $xml->dataElement('itemtype', $row[0]); $xml->dataElement('description', $row[1]); $xml->dataElement('rentalcharge', $row[2]); $xml->dataElement('rentalcharge_daily', $row[3]); $xml->dataElement('rentalcharge_hourly', $row[4]); $xml->dataElement('defaultreplacecost', $row[5]); $xml->dataElement('processfee', $row[6]); $xml->dataElement('notforloan', $row[7]); $xml->dataElement('imageurl', $row[8]); $xml->dataElement('summary', $row[9]); $xml->dataElement('checkinmsg', $row[10]); $xml->dataElement('checkinmsgtype', $row[11]); $xml->dataElement('sip_media_type', $row[12]); $xml->dataElement('hideinopac', $row[13]); $xml->dataElement('searchcategory', $row[14]); $xml->endTag('value'); } $xml->endTag('itemtypes'); $xml->startTag('authorised_values'); $query = "SELECT category,authorised_value,lib,lib_opac,imageurl FROM authorised_values where category = 'ITEMTYPECAT';"; $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('value'); $xml->dataElement('category', $row[0]); $xml->dataElement('authvalue', $row[1]); $xml->dataElement('lib', $row[2]); $xml->dataElement('lib_opac', $row[3]); $xml->dataElement('image_url', $row[4]); $xml->endTag('value'); } $xml->endTag('authorised_values'); $xml->startTag('localizations'); $query = "SELECT entity, code, lang, translation FROM localization WHERE entity = 'itemtypes';"; $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('value'); $xml->dataElement('entity', $row[0]); $xml->dataElement('code', $row[1]); $xml->dataElement('lang', $row[2]); $xml->dataElement('translation', $row[3]); $xml->endTag('value'); } $xml->endTag('localizations'); $xml->endTag('document'); $xml->end(); close $fh; return; } sub backup_libraries { my $dbh = shift; my $backupfile = shift; open(my $fh, '>', $backupfile) or die "Could not open $backupfile!"; my $xml = XML::Writer->new(OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag('libraries'); my $query = "SELECT id,parent_id,branchcode,title,description,ft_hide_patron_info,ft_search_groups_opac,ft_search_groups_staff FROM library_groups"; my $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('library_group'); $xml->dataElement('id',$row[0]); $xml->dataElement('parent_id',$row[1]); $xml->dataElement('branchcode',$row[2]); $xml->dataElement('title',$row[3]); $xml->dataElement('description',$row[4]); $xml->dataElement('ft_hide_patron_info',$row[5]); $xml->dataElement('ft_search_groups_opac',$row[6]); $xml->dataElement('ft_search_groups_staff',$row[7]); $xml->endTag('library_group'); } $query = "SELECT branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchzip,branchcity,branchstate,branchcountry,branchphone,branchfax,branchemail,branchreplyto,branchreturnpath,branchurl,issuing,branchip,branchprinter,branchnotes,opac_info,geolocation,marcorgcode,pickup_location FROM branches"; $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('library'); $xml->dataElement('branchcode',$row[0]); $xml->dataElement('branchname',$row[1]); $xml->dataElement('branchaddress1',$row[2]); $xml->dataElement('branchaddress2',$row[3]); $xml->dataElement('branchaddress3',$row[4]); $xml->dataElement('branchzip',$row[5]); $xml->dataElement('branchcity',$row[6]); $xml->dataElement('branchstate',$row[7]); $xml->dataElement('branchcountry',$row[8]); $xml->dataElement('branchphone',$row[9]); $xml->dataElement('branchfax',$row[10]); $xml->dataElement('branchemail',$row[11]); $xml->dataElement('branchreplyto',$row[12]); $xml->dataElement('branchreturnpath',$row[13]); $xml->dataElement('branchurl',$row[14]); $xml->dataElement('issuing',$row[15]); $xml->dataElement('branchip',$row[16]); $xml->dataElement('branchprinter',$row[17]); $xml->dataElement('branchnotes',$row[18]); $xml->dataElement('opac_info',$row[19]); $xml->dataElement('geolocation',$row[20]); $xml->dataElement('marcorgcode',$row[21]); $xml->dataElement('pickup_location',$row[22]); $xml->endTag('library'); } $xml->endTag('libraries'); $xml->end(); close $fh; return; } sub backup_patrontypes { my $dbh = shift; my $backupfile = shift; open(my $fh, '>', $backupfile) or die "Could not open $backupfile!"; my $xml = XML::Writer->new(OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag('patron'); my $query = "SELECT categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit,reservefee,hidelostitems,category_type,BlockExpiredPatronOpacActions,default_privacy,checkprevcheckout,reset_password,change_password FROM categories"; my $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('category'); $xml->dataElement('categorycode',$row[0]); $xml->dataElement('description',$row[1]); $xml->dataElement('enrolmentperiod',$row[2]); $xml->dataElement('enrolmentperioddate',$row[3]); $xml->dataElement('upperagelimit',$row[4]); $xml->dataElement('dateofbirthrequired',$row[5]); $xml->dataElement('finetype',$row[6]); $xml->dataElement('bulk',$row[7]); $xml->dataElement('enrolmentfee',$row[8]); $xml->dataElement('overduenoticerequired',$row[9]); $xml->dataElement('issuelimit',$row[10]); $xml->dataElement('reservefee',$row[11]); $xml->dataElement('hidelostitems',$row[12]); $xml->dataElement('category_type',$row[13]); $xml->dataElement('BlockExpiredPatronOpacActions',$row[14]); $xml->dataElement('default_privacy',$row[15]); $xml->dataElement('checkprevcheckout',$row[16]); $xml->dataElement('reset_password',$row[17]); $xml->dataElement('change_password',$row[18]); $xml->endTag('category'); } $xml->endTag('patron'); $xml->end(); close $fh; return; } sub backup_preferences { my $dbh = shift; my $backupfile = shift; open(my $fh, '>', $backupfile) or die "Could not open $backupfile!"; my $xml = XML::Writer->new(OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag('preferences'); my $query = "SELECT variable,value FROM systempreferences WHERE value != '' AND value IS NOT NULL"; my $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('pref'); $xml->dataElement('variable',$row[0]); $xml->dataElement('value',$row[1]); $xml->endTag('pref'); } $xml->endTag('preferences'); $xml->end(); close $fh; return; } sub backup_smsproviders { my $dbh = shift; my $backupfile = shift; open(my $fh, '>', $backupfile) or die "Could not open $backupfile!"; my $xml = XML::Writer->new(OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag('sms'); my $query = "SELECT name,domain FROM sms_providers"; my $sth = $dbh->prepare($query); $sth->execute(); while (my @row = $sth->fetchrow_array) { $xml->startTag('provider'); $xml->dataElement('name',$row[0]); $xml->dataElement('domain',$row[1]); $xml->endTag('provider'); } $xml->endTag('sms'); $xml->end(); close $fh; return; } sub create_timestamp { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; $mon = sprintf("%02s",$mon); $mday = sprintf("%02s",$mday); $hour = sprintf("%02s",$hour); $min = sprintf("%02s",$min); $sec = sprintf("%02s",$sec); my $str = "$year$mon$mday$hour$min$sec"; return $str; } sub abort { my $msg = shift; print STDERR "$0: $msg", "\n"; print_usage(); exit 1; } sub print_usage { print <<_USAGE_; mig export foo_a foo_b foo_c _USAGE_ }