If you manage a Linux based machine you have likely run into problems where you accidentally edited a configuration file in /etc or even deleted it and had regrets. Any system administrator knows you should back up a configuration file before editing it. How often do you do that though?
etckeeper is a small set of scripts that will create a version control repository in /etc (where most systemwide configuration files are stored) and automatically check-in changes including metadata like file permissions.
etckeeper supports Git, Mercurial, Darcs, and Bzr repos, I use the Git which is the default option.
etckeeper will automatically check-in and commit changes to /etc when installing and uninstalling a package via your system package manager. When making changes by hand, you can have etckeeper check them in and commit them with a single command.
sudo apt install etckeeper
When etckeeper is installed, it will create a repository automatically by silently issuing the sudo etckeeper init command.
If you prefer to use something other than git, you can issue the command sudo etckeeper uninit and change the configuration file to choose a different version control. You will need to re-issue sudo etckeeper init to initialize the repository.
Once etckeeper is installed, it will automatically track files changed when using the system package manager to uninstall or install packages. etckeeper will also automatically commit changes on a daily basis if you haven't done so manually. Although I recommend manually committing changes as you make them.
To manually commit changes to /etc you just have to issue the following command.
sudo etckeeper commit "Update DNS server list"
This will automatically check-in the changed files and commit them with the message "Update DNS server list". The more frequently you use sudo etckeeper commit the more granular and specific your commits will be. Just like with programming, you want to keep your commits to the least amount changes to solve a particular problem.
If you are updating the hostname, changing DNS server, setting some sysctl parameters, these should be unique commits unless they are all required to solve one problem.
If you just type sudo etckeeper commit without a message, it will open your default text editor to allow you to create a longer commit message.
Depending on the version control software chosen for etckeeper (defaults to Git) you just use the normal commands you would use.
For example, let's say we change the hostname in /etc/hostname , and we have committed the changes using `sudo etckeeper commit "Set hostname to nginxproxy". Later you want to see what has changed you can use the following git command:
sudo git diff HEAD^ HEAD -- /etc/hostname
If you do not know how to use version control, I highly recommend you learn. If of course you are in need of such things.