myesn

myEsn2E9

hi
github

Using Git to achieve file synchronization

Background Introduction#

I want to use a lightweight, free file synchronization software. After a brief search, I found that most of them are quite heavy. Suddenly, I thought of Git, which natively supports multi-person distributed collaboration and has Git servers.

The basic principle is to listen for changes in sub-files and subdirectories in the local directory, automatically commit after the changes, and trigger a hook after the repo receives the commit. This allows for callback operations, which means automatic pulling. By doing this, the two core points of automatic uploading and downloading of file synchronization are achieved.

Environment Preparation#

Source Code Download#

Click here to go to the source code download page, click the "Clone/Download" button, and then click "Download ZIP", as shown in the following image:
image

After downloading the source code zip file, extract it.

Then open a cmd and navigate to the directory of the source code, such as D:/git-file-sync, and enter npm i to download the required dependencies for running the source code.

Configuration Modification#

Open the file src/index.ts in the source code:

// Required configuration
const user = 'root';
const password = 'P@ssw0rd';
const repo = '192.168.0.228:8080/gitlab-instance-be4f27ff/webhook-tests.git';
const localPath = '/home/xiaoquantang/dev/tests/git-sync/webhook-tests'; // !!Local directory, make sure this is an empty directory

// Optional configuration
const pushDelayWhenChange = 1000; // How long to upload data after a file change, in milliseconds, recommended value is greater than 0
const httpServerPort = 3000; // Http server port

The required configurations are as follows:

  • user: Git login username
  • password: Git login password
  • repo: Git repository address. If there is none, please go to platforms like GitLab to create a repository. Note that it should not have the http:// or https:// prefix.
  • localPath: Local save directory. Note that this directory must be an empty directory, meaning there are no files in this directory.

The optional configurations are as follows:

  • pushDelayWhenChange: How long to upload changes to the Git repository after files or subdirectories in the directory have changed.
  • httpServerPort: The port of the Http server used to receive message pushes from the Git repository and implement file update functionality.

GitLab Configuration#

Enable Local Access#

Click on "Administrator", adjust the page, click on the bottom left "Settings > Network", then find "Outbound Requests" on the right, check the first option, and click "Save Changes".
image
image

Add a webhook to the repository#

Open the Git repository, click on the left "Settings > Webhooks", and perform the following operations on the right:

  1. Fill in the "URL" field with the address in the format of http://<LAN IP of the git-file-sync program host>:3000. The 3000 port here is the Http server port created by the git-file-sync program after running, and can be modified according to your needs.
  2. Check the "Push events" option under "Trigger Source".
  3. Uncheck the "Enable SSL verification" option under "SSL Verification".
  4. Click the "Add webhook" button.

Note: If you have git-file-sync running on 10 hosts, each host will create an HTTP server. Therefore, you need to configure webhooks for all 10 hosts. The effect is that if there is a push event in the repository, GitLab will push the event to all webhooks in a loop.
image

Start the file synchronization program#

Start a cmd window, then navigate to the git-file-sync source code directory, enter yarn to restore the dependencies, and finally enter yarn dev to start.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.