Skip to content

Instantly share code, notes, and snippets.

@Bachsau

Bachsau/backup-history.sh

Last active Jul 18, 2020
Embed
What would you like to do?
Bash history backup script
#!/bin/bash --login
# Make a backup of the user's Bash history
if [ -z "${HISTFILE}" ]; then
cd "${HOME}"
hfile='.bash_history'
else
cd "${HISTFILE%/*}"
hfile="${HISTFILE##*/}"
fi;
if [ -f "${hfile}" ]; then
hsize=$(stat -c '%s' "${hfile}")
bfile="${hfile}~"
elif [ -e "${hfile}" ]; then
echo "${PWD}/${hfile} is not a regular file. Aborting." >&2
exit 1
else
echo "${hfile} not found in ${PWD}. Aborting." >&2
exit 1
fi;
if [ -f "${bfile}" ]; then
bsize=$(stat -c '%s' "${bfile}")
elif [ -e "${bfile}" ]; then
echo "${PWD}/${bfile} is not a regular file. Aborting." >&2
exit 1
else
bsize=0
fi;
if [ ${bsize} -gt ${hsize} ]; then
echo "Backup is larger than current history. Aborting." >&2
exit 1
fi;
cp -fp "${hfile}" "${bfile}"
@Bachsau

This comment has been minimized.

Copy link
Owner Author

@Bachsau Bachsau commented Jul 17, 2020

If you value your history, call this from your personal crontab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.