3

I normally SSH on multiple remote hosts which are VMs and re-create them every 5-6 days. I loose all my bash history in those VMs as soon as I destroy and re-create them.

Is there a way I can share the bash history and re-use it in future? (Something like Dropbox but only for bash_history file)

  • Check out this post on distributed eternal history using Dropbox – Jedi Jul 4 '16 at 2:57
  • Just a fist idea, shared folder and a link to a file in the host HDD from the ~/.bash_history ? No DropBox, no external, no need of internet connection but only the needs required for the shared folder. (ps> else you can always manually copy the file ...). Or you can take the file from the host via ssh with a script in cron.... – Hastur Jul 4 '16 at 3:32
7

Take a look at the project Bashhub. It's effectively exactly what you are asking for - a dropbox for bash history - but they have added tools to reference the history from all your machines, assuming they have access to the internet. There are valid concerns about sending your shell interactions to a third party (e.g. employer, privacy, working on sensitive material), but if those aren't a significant factor this seems like a great solution. It doesn't appear they have open sourced the server component of the code, unfortunately, which would be nice to mitigate the previously mentioned issues.

There are a bunch of less sophisticated techniques that you can use, particularly if each machine has ssh access to each other, or at least each can talk to one central server via ssh. Any solution that involves multiple machines that could be in use simultaneously updating a common history file (either in a single linked location or in distributed updates) will somewhat break the history expansion capabilities of bash - e.g. recalling the last command with !! could get you the wrong command. These are just some ideas that would need to be fleshed out some:

  • Configure an NFS share from a server, mount on each machine, sym link your .bash_history file from it
  • Use the bash PROMPT_COMMAND capability to append a remote file with your last command ( e.g in .bashrc PROMPT_COMMAND="append_history" then add the function append_history () { ( ssh you@yourserver "echo !! >> ~/common_bash_history" ) & } . I put the ssh command in a subshell and backgrounded it to remove the delay after entering a command. You would then have to periodically push the changes from the common_bash_history file to each machine (Only adding unique entries).

  • Periodic cron job that runs on one of the machines ('server') that pulls the histories from each, creates a master and pushes it out.

In my case Bashhub wouldn't work, for reasons mentioned above. I've been looking for a 'perfect' solution to this and haven't found it - what I have settled on for the time being is aggregating all of my histories from each machine into a master history file on one server. But I don't push the aggregated results back into each machine's history file; I use a separate file for this, which lets me add things like which machine it came from to it, and I set up new search and completion functions for the aggregate file (which I use for 'long term' history) and leaving bashs history expansion / completion intact (which I use for 'short term' history). If bashhub made the server software available as well as the client software it would be a highly polished alternative of the solution (hack) I've thrown together.

  • Bashhub creator here. @Argonauts, thanks for the plug and feedback. Work is beginning on an opensource version of the sever. Stay tuned as I hope to get it out to the community in the coming months. – RCCola Apr 17 '17 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.