Shell history DB


As a Linux system administrator, I frequently need to write clever or complex bits of shell code on the command line that I might want to re-use. The standard bash history file in ~/.bash_history always seems to overflow or go missing when I move from system to system. The history-db project is my fix for this problem - when the command prompt is rendered, a bash $PROMPT_COMMAND reads the previous command out of history and uses curl to send it to a server (hopefully over https) where it is deduplicated and stored in a MySQL database.



The server is a very small PHP application that requires MySQL:

git clone
vim history-db-web/download.php # change the "get" and database password from the default
vim history-db-web/index.php # change the database password from the default
cp -a history-db/web /var/www/htdocs/history


The client is a single bash function:

post_history ()
    LAST_COMMAND=$(history 1 | sed -e 's/^ *[0-9]* *[0-9]*-[0-9]*-[0-9]* *[0-9]*:[0-9]*:[0-9]* *- *//g');
    curl -d hostname="$HOSTNAME" -d command="$LAST_COMMAND" https://yourserver.example.tld/history/

set your $PROMPT_COMMAND variable to include post_history and be sure to save this in your .bashrc


The prompt command runs pretty quickly and doesn’t noticably slow down your sessions. It does result in your prompt blocking when you can’t reach the server hosting your database - I’ve got some workarounds for “offline mode” in my Dotfiles, but if you frequently work offline, you should pick a different solution.

When you want to pull your sync your history back from the database, use the get_history command:

get_history ()
    if ! [ -f $HISTFILE ]; then
    echo -n "enter password: ";
    read -s HISTPASS;
    history -c;
    rm $HISTFILE;
    curl -s https://yourserver.example.tld/history/download.php?passwd=$HISTPASS > $HISTFILE;
    history -r

It will prompt you for a password interactively and pull the history database down.