summaryrefslogtreecommitdiffstats
path: root/.local
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2013-08-26 20:54:17 +0200
committerJan Pokorný <jpokorny@redhat.com>2013-08-26 20:58:35 +0200
commit616d0be491dc15a41b09ec870287a9747cc2e7ee (patch)
tree5503e94307d34f74e996baa719ad5e456907cd7d /.local
parentc446d20cb3897b4902de2968a3d99d1de07790f6 (diff)
downloaddotfiles-616d0be491dc15a41b09ec870287a9747cc2e7ee.tar.gz
dotfiles-616d0be491dc15a41b09ec870287a9747cc2e7ee.tar.xz
dotfiles-616d0be491dc15a41b09ec870287a9747cc2e7ee.zip
Add bash history rotation on montly basis
Credit: http://lukas.zapletalovi.com/2013/03/never-lost-your-bash-history-again.html Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to '.local')
-rw-r--r--.local/bin/history-backup32
1 files changed, 32 insertions, 0 deletions
diff --git a/.local/bin/history-backup b/.local/bin/history-backup
new file mode 100644
index 0000000..b591e12
--- /dev/null
+++ b/.local/bin/history-backup
@@ -0,0 +1,32 @@
+#!/bin/sh
+# [Source:
+# http://lukas.zapletalovi.com/2013/03/never-lost-your-bash-history-again.html]
+#
+# This script creates monthly backups of the bash history file. Make sure you have
+# HISTSIZE set to large number (more than number of commands you can type in every
+# month). It keeps last 200 commands when it "rotates" history file every month.
+# Typical usage in a bash profile:
+#
+# HISTSIZE=90000
+# source ~/bin/history-backup
+#
+# And to search whole history use:
+# grep xyz -h --color ~/.bash_history.*
+#
+
+KEEP=200
+BASH_HIST=~/.bash_history
+BACKUP=$BASH_HIST.$(date +%y%m)
+
+if [ -s "$BASH_HIST" -a "$BASH_HIST" -nt "$BACKUP" ]; then
+ # history file is newer then backup
+ if [[ -f $BACKUP ]]; then
+ # there is already a backup
+ cp -f $BASH_HIST $BACKUP
+ else
+ # create new backup, leave last few commands and reinitialize
+ mv -f $BASH_HIST $BACKUP
+ tail -n$KEEP $BACKUP > $BASH_HIST
+ history -r
+ fi
+fi