Use Git to back up Linux configuration files in minutes
When we use a Linux server, we sometimes need to back up the configuration file. The traditional backup method is to copy a file, rename it, and insert some characters at the end of the file.
However, if we use Git, we can easily manage the backup of configuration files. In this article, I will demonstrate how to use Git to implement backup and manage backup. My testing environments are CentOS 7 and RHEL 7.
1. Install Git
[Root @ localhost ~] # Yum install git
Check Git version
[Root @ localhost ~] # Git -- version
Git version 1.8.3.1
[Root @ localhost ~] #
Set initial parameters
Replace the username and email in the following command with your own.
[Root @ localhost network-scripts] # git config -- global user. name "your_user_name"
[Root @ localhost network-scripts] # git config -- global user. email "your_email"
Ii. initialize the Git Database
Because I want to back up the network configuration file, I only need to initialize the Git database in the directory of the network configuration file.
[Root @ localhost ~] # Cd/etc/sysconfig/network-scripts
[Root @ localhost network-scripts] # git init
Initialized empty Git repository in/etc/sysconfig/network-scripts/. git/
[Root @ localhost network-scripts] #
Enter ls-a in the command line. We can see that the ". git" folder is created.
3. Use the following command to back up data
[Root @ localhost network-scripts] # git add ifcfg-enp0s3
[Root @ localhost network-scripts] #
[Root @ localhost network-scripts] # git commit ifcfg-enp0s3
[Master (root-commit) 1269758] Changes on 26 Oct 2015
1 file changed, 16 insertions (+)
Create mode 100644 ifcfg-enp0s3
[Root @ localhost network-scripts] #
When we execute the second command, it requires you to enter a remark like "Changes on 26 Oct 2015" and save the file.
Run the following command to view git logs:
[Root @ localhost network-scripts] # git log
Commit 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d
Author: Pradeep <pradeep@linuxtechi.com> Date: Mon Oct 26 00:03:08 2015-0400
Changes on 26 Oct 2015
[Root @ localhost network-scripts] #
Note: Try to insert some useless characters in the ifcfg-enp0s3 File
4. Restore the network configuration file from the Git Database
[Root @ localhost network-scripts] # git reset -- hard 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d
HEAD is now at 1269758 Changes on 26 Oct 2015
[Root @ localhost network-scripts] #
Use the same git id as above. The git id is different for your installation.
Verify that the files recovered from the git database are correct.
GitHub Tutorials:
GitHub tutorials
Git tag management details
Git branch management
Git remote repository details
Git local Repository (Repository) Details
Git server setup and Client installation
Git Overview
Share practical GitHub tutorials
How to Build and use Git servers in Ubuntu
Git details: click here
Git: click here
This article permanently updates the link address: