Debian packages script library

From Koha Wiki
Jump to navigation Jump to search

A Koha installation made with the Debian packages provides a number of useful scripts/commands. Even more interesting things can be done by combining these commands into new scripts. This page is meant for sharing such scripts.

Template

Developers: Please use the following template to add your scripts to the wiki.

== Script title ==
* '''Developer:''' Name of script developer
* '''Creation Date:''' the date you entered the script (YYYY-MM-DD)
* '''Purpose:''' Purpose of the script
* '''Status:''' Completed / In progress

<syntaxhighlight lang="bash">
Some script code
</syntaxhighlight>

Scripts do not have to be written in Bash of course, and you can specify the language with the lang attribute on the syntaxhighlight tag, and values from the list of Supported languages.

Count the number of rows in a table

  • Developer: Magnus Enger
  • Creation Date: 2015-12-18
  • Purpose: Takes one argument: the name of a table in the Koha database. Counts the number of rows in that table for each Koha instance, and sums up all the numbers at the end. Can be used to count the number of items, biblios or borrowers. Or to check the number of sessions in the database.
  • Status: Completed
#!/bin/bash

# Check that the user is root
if [ "$(whoami)" != "root" ]; then
  echo "Sorry, you are not root."
  exit 1
fi

die() {
    echo "$@" 1>&2
    exit 1
}

[ "$#" = 1 ] || die "Usage: $0 tablename"

sum=0
for name in $(koha-list --enabled); do
  
  echo -n "$name: "
  
  # Output from this next line is two lines: 
  # - First line is just the string "value"
  # - Second line is the actual value we are looking for
  # The sed command gives us just the second line
  count=$( echo "SELECT COUNT(*) AS count FROM $1" | sudo koha-mysql $name | sed -n '2 p' )
  echo $count
  sum=$(( sum + count ))

done

echo "--------------"
echo "SUM: $sum"

List the value of a given syspref for all instances

  • Developer: Magnus Enger
  • Creation Date: 2013-10-30
  • Purpose: Say you have 20 instances on a server and you want to check that they all have memcached set as SessionStorage. You give this script "SessionStorage" as its argument and then it gives you the value of that syspref for every instance.
  • Status: Completed
#!/bin/bash

# Check that the user is root
if [ "$(whoami)" != "root" ]; then
  echo "Sorry, you are not root."
  exit 1
fi

die() {
    echo "$@" 1>&2
    exit 1
}

[ "$#" = 1 ] || die "Usage: $0 sysprefname"

for name in $(koha-list); do

  echo -n "$name: "

  # Output from this next line is two lines:
  # - First line is just the string "value"
  # - Second line is the actual value we are looking for
  # The sed command gives us just the second line
  echo "SELECT value FROM systempreferences WHERE variable = '$1'" | sudo koha-mysql $name | sed -n '2 p'

done

Gist

List the value of a given syspref for all instances - oneliner

  • Developer: Robin Sheat
  • Creation Date: 2014-02-18
  • Purpose: Same as the script above, but as a oneliner.
  • Status: Completed
for i in $( sudo koha-list --enabled ) ; do echo 'select * from systempreferences where variable="ShowRecommendations";' | sudo koha-mysql $i ; done

Restart all Zebras

  • Developer: Magnus Enger
  • Creation Date: 2014-02-14
  • Purpose: Restart all the Zebra instances on a server
  • Status: Completed
#!/bin/bash

# Check that the user is root
if [ "$(whoami)" != "root" ]; then
  echo "Sorry, you are not root."
  exit 1
fi

for name in $(koha-list --enabled)
do
    koha-restart-zebra "$name"
done

This script is deprecated since Bug 8566 (koha-foreach) is fixed.

Bulk export patrons images

#!/bin/bash
# set -euo pipefail     # unofficial bash strict mode
# IFS=$'\n\t'
#
# Also here: https://gist.github.com/pabloab/347dc307a9650a5c4309c8fd6296c906

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"