Skip to content

Instantly share code, notes, and snippets.

@discarn8
Last active May 2, 2018 07:32
Show Gist options
  • Save discarn8/c3e97d7235c14936687e46a139800b3c to your computer and use it in GitHub Desktop.
Save discarn8/c3e97d7235c14936687e46a139800b3c to your computer and use it in GitHub Desktop.
bash_history_backup.sh
#!/bin/sh
# This script creates daily backups of the root and user's bash history file. This backup
# is non-destructive.
# Typical usage in a bash profile:
#
# Forked from https://lukas.zapletalovi.com/2013/03/never-lost-your-bash-history-again.html
#
# How many lines do you want to save?
KEEP=1000
#Name of the computer - useful when archiving these files
pcname=#Name of the computer
#Name of the user you want to include the history for
uNAMEu1=#username1
#uNAMEu2=#username2
#To get root history, include this
uNAMEr=root
#The destination sub-directory you want your files written to
bDIR=history
#Will create /home/student1/history/
DESTu1=/home/$uNAMEu1/$bDIR/
#DESTu2=/home/$uNAMEu2/$bDIR/
#Will create /root/history/
DESTr=/$uNAMEr/$bDIR/
#Will create /home/student1/history/20180502_SQL-SERVER.student1.history
BACKUPu1=$DESTu1$(date +%y%m%d)_$pcname.$uNAMEu1.history
#BACKUPu2=$DESTu2$(date +%y%m%d)_$pcname.$uNAMEu2.history
#Will create /root/history/20180502_SQL-SERVER.root.history
BACKUPr=$DESTr$(date +%y%m%d)_$pcname.$uNAMEr.history
#this is the name of the file that we want to backup
BASH_HIST=.bash_history
#Create the destination directory if it does not exist
if [ ! -d $DESTu1 ]; then
mkdir -p $DESTu1
fi
#if [ ! -d $DESTu2 ]; then
# there is not already a backup directory,create it
# mkdir -p $DESTu2
#fi
if [ ! -d $DESTr ]; then
# there is not already a backup directory,create it
mkdir -p $DESTr
fi
#Create the files
tail -n$KEEP /home/$uNAMEu1/$BASH_HIST > $BACKUPu1
#tail -n$KEEP /home/$uNAMEu2/$BASH_HIST > $BACKUPu2
tail -n$KEEP /$uNAMEr/$BASH_HIST > $BACKUPr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment