Last active
February 15, 2023 14:26
-
-
Save Bachsau/6d58bf0b9673ca564a0cf4e32a9af21f to your computer and use it in GitHub Desktop.
Bash history backup script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you value your history, call this from your personal crontab.