Skip to content

Instantly share code, notes, and snippets.

@pabloab
Created September 30, 2018 02:06
Show Gist options
  • Save pabloab/347dc307a9650a5c4309c8fd6296c906 to your computer and use it in GitHub Desktop.
Save pabloab/347dc307a9650a5c4309c8fd6296c906 to your computer and use it in GitHub Desktop.
Bash script to export patron images from a Koha instance and generate DATALINK.TXT to import into another instance.
#!/bin/bash
# set -euo pipefail # unofficial bash strict mode
# IFS=$'\n\t'
echo "This script bulk export patron images from a Koha instance and generate DATALINK.TXT to import into another instance."
# @TODO
# -
# [PARAMS]
queryIDs="
SELECT borrowers.borrowernumber AS borrowernumber,
cardnumber,
REPLACE(mimetype,'image/','')
FROM borrowers
JOIN patronimage ON (borrowers.borrowernumber = patronimage.borrowernumber)
WHERE borrowers.borrowernumber IS NOT NULL
AND cardnumber IS NOT NULL;"
datalink_filename="DATALINK.TXT" # or “IDLINK.TXT”
counter=0
# limit=3
if [ $# -ne 1 ]; then
echo >&2 -e "Usage: sudo $0 {instancename} "
exit 0
fi
if [ "$(whoami)" != "root" ]; then
echo "Sorry, you are not root. Try with sudo $0 {instancename}"
exit 1
fi
instancename="$1";
rm --force "$datalink_filename"
koha-mysql $instancename --skip-column-names -e "$queryIDs" | while read borrowernumber cardnumber extension; do
queryImages="SELECT imagefile FROM patronimage WHERE borrowernumber=$borrowernumber"
filename="$cardnumber.$extension"
rm -rf $filename
koha-mysql $instancename --skip-column-names --raw -e "$queryImages" > $filename
echo "$cardnumber, $filename" | tee -a "$datalink_filename"
(( counter++ ))
# if [ $counter -gt $limit ]; then
# exit
# fi
done
# zip "$(date +%Y-%m-%d_%T).zip $datalink_filename *.png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment