Mobileread
Fully automatic calibre installer/updater for linux
#1  eschwartz 04-07-2014, 04:27 PM
Growing out of this discussion: Official Calibre PPA?, here is a simple bash script to update calibre on linux.
Note: This can also perform the first-time install, since the version check returns the same failure for out-of-date as it does if there is no calibre installed.

Thanks to aleyx for working on the version checking!

EDIT: The script has been updated to fix a few problems. Also, you can now install the script and add the systemd timer or cron job (defaults to systemd timer, falls back on cron) by running the following one-liner in a terminal:
Code
sudo -v && wget -nv -O- https://github.com/eli-schwartz/calibre-installer/raw/master/linux/calibre-installer.sh | sudo bash -
Everything is managed through my GitHub repository



What this is doing, the manual way



New code:
Spoiler Warning below






Code
#!/bin/bash
# Unset git development source
export CALIBRE_DEVELOP_FROM=
# by default, switch is off
force_upgrade=0
# Functions
calibre_is_installed()
{ command -v calibre >/dev/null 2>&1
}
usage()
{	cat <<- _EOF_	Usage: calibre-upgrade.sh [OPTIONS]	Upgrades calibre installation. Automatically checks if the current version is up to date.	Must be run as root.	OPTIONS -f, --force Force an update. This is only useful if binaries were updated for a critical error. :shrug: -h, --help Displays this help message.
_EOF_
}
do_upgrade()
{ if calibre_is_installed; then # shutdown calibre as each logged-in user. for i in $(users | tr ' ' '\n' | sort -u); do sudo -u $i calibre --shutdown-running-calibre done killall -q -v calibre-server && echo -e "Restart when upgrade is finished. ;)\n\n" || echo -e "No running calibre servers.\n\n" fi wget -nv -O- https://github.com/kovidgoyal/calibre/raw/master/setup/linux-installer.py | python -c "import sys; main=lambda x,y:sys.stderr.write('Download failed\n'); exec(sys.stdin.read()); main()"
}
# Options
while [ "$1" != "" ]; do case $1 in -h|--help) usage exit ;; -f|--force) shift force_upgrade=1 ;; *) echo "calibre-upgrade.sh: unrecognized option '$1'" echo "Try 'calibre-upgrade.sh --help' for more information." exit 1 esac shift
done
# Main
## Check that we are running as root
if [[ $EUID -ne 0 ]]; then echo -e "You can only install calibre if you have root permission." exit 1
fi
if calibre_is_installed; then calibre-debug -c "import urllib as u; from calibre.constants import numeric_version; raise SystemExit(int(numeric_version < (tuple(map(int, u.urlopen('http://code.calibre-ebook.com/latest').read().split('.'))))))" UP_TO_DATE=$?
else echo -e "Calibre is not installed, installing...\n\n" UP_TO_DATE=1
fi
if [ "$UP_TO_DATE" = 0 ]; then echo "Calibre is up-to-date" if [ "$force_upgrade" = 1 ]; then echo "" echo "Forcing upgrade anyway -- are you sure you want to continue? [y/n]" read answer if [[ "$answer" = "y" || "$answer" = "yes" ]]; then do_upgrade else echo "Exiting..." exit 1 fi fi
else calibre_is_installed && echo -e "Calibre is out-of-date. Upgrading...\n\n" do_upgrade
fi


Old code:
Spoiler Warning below






Code
#!/bin/bash
calibre-debug -c "import urllib as u; from calibre.constants import numeric_version; raise SystemExit(int(numeric_version < (tuple(map(int, u.urlopen('http://calibre-ebook.com/downloads/latest_version').read().split('.'))))))"
UP_TO_DATE=$?
if [ $UP_TO_DATE = 0 ]; then	echo "Calibre is up-to-date"
else	calibre --shutdown-running-calibre	killall calibre-server	sudo -v; wget -nv -O- https://github.com/kovidgoyal/calibre/raw/master/setup/linux-installer.py | sudo python -c "import sys; main=lambda:sys.stderr.write('Download failed\n'); exec(sys.stdin.read()); main()"
fi


Explanation:
Spoiler Warning below






Very simply:
  1. we start off with a one-liner which runs calibre-debug to determine the currently installed version, and compares it to the version declared at http://calibre-ebook.com/downloads/latest_version
  2. if it returns negative, calibre is already up to date, and it tells you so. Why bother updating to the current version?
  3. if not, we:
    1. close any running instances of calibre
    2. kill calibre-server, if running
    3. run Kovid's one-liner install code. If you use a non-default install directory, you can edit this!
      Just drop in Kovid's other code.


Save this as "calibre-upgrade.sh" (or whatever you want) and stick it in "/usr/bin/", to add it to your path. ("$HOME/bin/" works too, but this is for a system install, so this is nicer. )

You can now update calibre by running
Code
sudo calibre-upgrade.sh
but to make this fully automatic, simply add:
Code
0 6 * * 5 /usr/bin/calibre-upgrade.sh > /dev/null 2>&1
or if you want the output emailed to you by cron, add
Code
0 6 * * 5 /usr/bin/calibre-upgrade.sh
to your crontab (with the command "sudo crontab -e") and the updater will run every Friday at 6:00 AM. Presumably, you aren't awake at that time, but you can fill in whatever time you want.

Attached is a zip of the updater script (in a bin directory, and marked as executable), which can be extracted to "/usr/" (or $HOME) for your convenience.
[gz] calibre-upgrade.sh.tar.gz (1.3 KB, 529 views)
Reply 

#2  aleyx 04-07-2014, 05:19 PM
You're welcome ^_^

Some ideas:

After the update one-liner, it may be useful to test for the success (or failure) of the update, then run a separate .sh containing the calibre-server command(s) for your particular libraries. That way you don't have to relaunch the servers manually. You can also use that separate .sh at boot time.

Maybe also sending a mail with a little blurb (whether there was an update, whether it succeeded...)

Also, between the killall and the oneliner, a backup.sh would be nice. Although anyone savvy enough to use this script should already have a nigthly backup in place, so maybe not?

For those who are a little wary about messing with their crontab, just linking calibre-update.sh into /etc/cron.daily (or /etc/cron.weekly) can be an adequate solution.

N.
Reply 

#3  eschwartz 04-07-2014, 05:49 PM
All kinds of frills can be used, of course. But the basics are there.

Regarding mail blurbs, just using
Code
0 6 * * 5 /usr/bin/calibre-upgrade.sh
without sending stdout and stderr to /dev/null should mail you the output anyway.
Reply 

#4  aleyx 04-07-2014, 06:01 PM
That's... a good point, actually.
Reply 

#5  R71986 05-11-2014, 10:04 AM
In order to automate it have I understood the cron setup correctly?
sudo ln -s /usr/bin/calibre-upgrade.sh /etc/cron.weekly/calibre-upgrade

(note i have removed the sh from the object link)

I am not clear when cron.weekly will run. Is that a system operation not controlled by a cron script?
Reply 

#6  eschwartz 05-11-2014, 12:00 PM
Quote R71986
In order to automate it have I understood the cron setup correctly?
sudo ln -s /usr/bin/calibre-upgrade.sh /etc/cron.weekly/calibre-upgrade

(note i have removed the sh from the object link)

I am not clear when cron.weekly will run. Is that a system operation not controlled by a cron script?
That uses anacron instead of cron, and should do basically the same thing. But you will have to edit "/etc/anacrontab" or "/etc/cron.d/anacron" (not sure which, since I just stuck with crontab and have no idea how anacron works) I think, to change anacron's schedule to run weekly jobs on Friday, or the job won't run until anacron's default time. I think it defaults to running weekly jobs on Sunday, so you'd be two days behind.

The purpose of anacron is for running jobs on startup if the jobs were missed because the computer was off. You may not need that.
I'd just put the command to run calibre-upgrade as a crontab job, like in the instructions. It's simpler/easier to control.
Reply 

#7  BetterRed 05-11-2014, 09:23 PM
Quote eschwartz
I just stuck with crontab and have no idea how anacron works
Dare I suggest anachronistically, or anarchically

BR
Reply 

#8  eschwartz 05-11-2014, 09:38 PM
Quote BetterRed
Dare I suggest anachronistically, or anarchically

BR
You mean it is secretly controlled by Something Else Entirely, for instance, perhaps the hidden Phase Of The Moon Generator?
Reply 

#9  BetterRed 05-11-2014, 09:53 PM
No... by the Ghosts of Godwin and Early Burke
Reply 

#10  eschwartz 07-30-2014, 02:53 AM
I have created a github repository here: https://github.com/eli-schwartz/calibre-installer which contains an updated calibre-upgrade.sh as well as a script à la calibre's installer, which allows for a one-liner installation of the upgrade script. (Was that statement confusing enough? )

Run the following command in a terminal:

Code
sudo -v && wget -nv -O- https://github.com/eli-schwartz/calibre-installer/raw/master/linux/calibre-installer.sh | sudo sh -
It will:
  1. Download calibre-upgrade.sh to /usr/bin/calibre-upgrade.sh
  2. chmod it so it is executable.
  3. Add the cron job to root's crontab. (If cron is not installed*, this step will fail and report that to you.)

If anyone has any improvement suggestions, I'd be delighted to hear them.

* Because on my system it isn't -- who knew? Apparently, systemd is the wave of the future, and Arch Linux wants you to use systemd timers instead of crontabs. When I get around to it (or rather, figure out how they work ), I may write one of these timers. Meanwhile, if you are on Arch too, you can manually install a cron daemon or write your own stinking timer. (If anyone does, please share. </beg> )
Reply 

  Next »  Last »  (1/5)
Today's Posts | Search this Thread | Login | Register