Skip to content

Instantly share code, notes, and snippets.

@zuf
Created June 26, 2023 16:15
Show Gist options
  • Save zuf/e4ea27b10b1260b42df8830e326cf5f3 to your computer and use it in GitHub Desktop.
Save zuf/e4ea27b10b1260b42df8830e326cf5f3 to your computer and use it in GitHub Desktop.
Simple bash history auto backup
# copy to your .bashrc to keey bash history backups at ~/bak/bash/history/
# backup bash history when start new bash session and there no bak files for last 5 minutes
LAST_HIST_BAKS=$(find $HOME/bak/bash/history/ -type f -mmin -5)
if [ -z "$LAST_HIST_BAKS" ]; then
mkdir -p $HOME/bak/bash/history/
zstd < $HOME/.bash_history > $HOME/bak/bash/history/bash_history.$(date +%Y-%m-%d_%H-%M-%S.%N).zst
# remove old (older then 30 days) bak files
find ~/bak/bash/history/ -type f -mtime '+30' | xargs --no-run-if-empty rm
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment