Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to back up Linux configuration files using Git

Shulou Source: shulou.com Published: 2022-06-01 04:33:36 10月05日 Update

This article is about how to use Git to back up Linux configuration files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

When we use a Linux server, we sometimes need to back up our configuration files. The traditional backup method is to copy the file, rename it, and insert some characters at the end of the file.

However, if we use Git, we can easily manage backups of configuration files. In this article, I will demonstrate how to use Git to implement backups and manage backups. The environments I tested were CentOS 7 and RHEL 7.

First, install Git

[root@localhost ~] # yum install git

Check the Git version

[root@localhost] # git-- version git version 1.8.3.1 [root@localhost] #

Set initial parameters

Replace the user name 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"

Initialize the Git database now

Because I am going 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] #

Type ls-an on the command line, and we can see that the ".git" folder is created.

Third, use the following command for backup

[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 asks you to enter a comment like "Changes on 26 Oct 2015" and then save the file.

Use the following command to view the git log

[root@localhost network-scripts] # git log commit 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d Author: Pradeep 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.

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] #

Using the same git id as above, the id of git is different depending on your installation.

Verify that the files recovered from the git database are correct.

Thank you for reading! This is the end of this article on "how to use Git to back up Linux configuration files". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: File backup configuration command data database article network different content character more face management input good practical same above traditional Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MariaDB Shulou Information Linux MySQL