-
Notifications
You must be signed in to change notification settings - Fork 0
/
history-backup
executable file
·39 lines (37 loc) · 1.43 KB
/
history-backup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env sh
# by 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:
# #!/bin/sh
# 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=2000
HISTORY_FILE=${HISTFILE:-${HOME}/.bash_history}
BACKUP=${HOME}/bash_history_backup/$(basename ${HISTORY_FILE}.$(date +%Y-%U))
BASE_DIR=~/
cd $BASE_DIR
mkdir -p bash_history_backup
if [ -s "${HISTORY_FILE}" -a "${HISTORY_FILE}" -nt "${BACKUP}" ]; then
# history file is newer then backup
if [[ -f ${BACKUP} ]]; then
# there is already a backup
cp -f ${HISTORY_FILE} ${BACKUP}
else
# create new backup, leave last few commands and reinitialize
mv -f ${HISTORY_FILE} ${BACKUP}
tail -n${KEEP} ${BACKUP} > ${HISTORY_FILE}
history -r
echo "Created new history backup. Kept last ${KEEP} commands in history"
fi
fi