Created
November 10, 2022 14:10
-
-
Save gioisco/51b97e404fec23cd30263169251138bb to your computer and use it in GitHub Desktop.
An automatic backup of bashrc on load bash and after 10 minutes from the last edit of .bashrc
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
# START AUTOBACKUP .bashrc | |
how_old_is_file_from_now() { | |
if [[ ! -r $1 ]]; then | |
echo 0 | |
else | |
echo "$(($(date +%s) - $(date -r "$1" +%s)))" | |
fi | |
} | |
bak_have_same_modify_date() { | |
if [[ ! -r "$1".bak ]]; then | |
# echo "File .bak does not exist" | |
return 0 | |
fi | |
if [[ "$(date -r "$1" +%s)" == "$(date -r "$1".bak +%s)" ]]; then | |
return 0 | |
fi | |
return 1 | |
} | |
bashrc="$HOME/.bashrc" | |
if [[ -r "${bashrc}" ]] && ! bak_have_same_modify_date "$bashrc"; then | |
how_old_is_bashrc=$(how_old_is_file_from_now "$bashrc") | |
if [[ ${how_old_is_bashrc} -gt 600 ]]; then | |
echo "Backup bashrc" | |
cp -a "${bashrc}" "${bashrc}.bak" | |
fi | |
fi | |
# END AUTOBACKUP .bashrc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment