Skip to content

Instantly share code, notes, and snippets.

@gioisco
Created November 10, 2022 14:10
Show Gist options
  • Save gioisco/51b97e404fec23cd30263169251138bb to your computer and use it in GitHub Desktop.
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
# 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