Jedrzej Pawel Maczan
[18.04.2021] Privacy Regained - Self-hosted local git with automatic backups on Linux
If you consider hosting your own local git instance, because i.e. you don't want to store code outside your machine, but you want to sleep well and not worry about data loss, then you might find this article useful.
I will guide you through few steps to set it up in minutes. Where you see placeholder like
YOUR_USERNAME, put your own data in accordance with context.
Tools: rsync, git, anacron, nano (or any text editor)
Devices: computer, USB drive or external HDD/SSD drive
0. Mount USB drive - your drive will be mounted under path like
/media/user/writable/. In order to check list of mounted drives, run
1. Create copying script - let's assume you will write NodeJS code inside your git repository. In order to prevent copying unnecessary dependencies, we will exclude
node_modules from copying for every subfolder. To create a copying script, run
nano self-hosted-git-backup.sh and paste:
rsync -avr --exclude='*/node_modules' /home/YOUR_USERNAME/Documents/self-hosted-git/ /media/YOUR_USERNAME/writable/self-hosted-git
We are using
rsync for copying files from drive to USB drive.
2. Make script executable - to give executable permission to script, run
chmod +x self-hosted-git-backup.sh
3. Create local git repository - in our example, repository will be placed under path
/home/YOUR_USERNAME/Documents/self-hosted-git/, so just create a folder called
Documents folder. To do it, invoke
mkdir self-hosted-git. Then, navigate inside a folder and initialize local repository with
4. Create backup script. In order to create it, you need to create a folder inside
sudo mkdir /etc/self-hosted-git-backup.daily. Navigate inside this folder and create a script using
sudo nano self-hosted-git-backup. Inside a file, paste:
It will invoke our copying script.
5. Schedule backups. In this example, we will be fine with daily backups. Open backups configuration file using
sudo nano /etc/anacrontab. Navigate to bottom of the file and paste:
1 0 self-hosted-git-backup.daily run-parts --report /etc/self-hosted-git-backup.daily
(Optional) 6. Verify correctness of the backup process. You can force invoke backup in order to check if all is set up correctly. Simply run
sudo anacron -f -d self-hosted-git-backup.daily
And you did it - you created automatic backups for your private git repository. You can modify this process to be more customized for your needs.