Skip to content

Instantly share code, notes, and snippets.

@calkan
Last active January 5, 2021 02:09
Show Gist options
  • Save calkan/eaad0bc4458da16a72dd to your computer and use it in GitHub Desktop.
Save calkan/eaad0bc4458da16a72dd to your computer and use it in GitHub Desktop.
Michael Hoffman's crazy bash_history backer upper on git
1 - Create a *private* GitHub/Bitbucket or similar git repo. Here I assume the repo is:
https://github.com/calkan/bash_history.git
2 - Create .history directory and initialize it for the repo:
mkdir $HOME/.history
cd $HOME/.history
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/calkan/bash_history.git
git push -u origin master
3 - Make it stop asking for password (no need to worry, still others can't reach it unless they have access to your .history)
chmod 700 $HOME/.history
git config credential.helper store
git push -u origin master
4 - Add the following lines to your $HOME/.bashrc - Michael's crazy BASH trick:
HOSTNAME="$(hostname)" HOSTNAME_SHORT="${HOSTNAME%%.*}"
mkdir -p ${HOME}/.history/$(date -u +%Y/%m)
HISTSIZE=""
HISTFILESIZE=""
HISTFILE="${HOME}/.history/$(date -u +%Y/%m/%d.%H.%M.%S)_${HOSTNAME_SHORT}_$$"
5 - Add GitHub support to $HOME/.bashrc below the lines written above:
touch $HISTFILE
# add HISTFILE
git --git-dir $HOME/.history/.git/ --work-tree $HOME/.history/ add $HISTFILE
# let's be paranoid and save the history frequently. This is to prevent history loss due to drops in network connection
PROMPT_COMMAND="history -a"
6 - Activate your new environment variables:
source $HOME/.bashrc
6 - Add the following lines to your $HOME/.bash_logout
# write history file
history -a
# pull it, just in case if you logged out of another session, git will complain:
git --git-dir $HOME/.history/.git/ --work-tree $HOME/.history/ pull
# commit & push
git --git-dir $HOME/.history/.git/ --work-tree $HOME/.history/ commit -am "add $HISTFILE"
git --git-dir $HOME/.history/.git/ --work-tree $HOME/.history/ push
Logging out takes a second or two while committing to GitHub. It may be a good idea to find a way to disable this for cluster jobs, not sure if .bash_logout is invoked while terminating grid jobs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment