Git as a Backup Tool
Table of Contents
1 Introduction
Using Git as a backup strategy works the best for short term backups (i.e. backing up files you work with on a daily/weekly basis) and for syncing files between multiple computers. It is a simple solution and comes with some limitations (discussed below). For long term backups you'll probably want to look into specialized software such as restic.
2 Setup
The setup discussed here will use a usb drive as the "remote" repository, but you could ofcourse also use a server.
Create the repository:
cd /mnt/backup-repo git init --bare --shared=all
Clone the repository:
cd ~/documents git clone /mnt/backup-repo
Commit changes:
git add -A git commit -m "2024-03-17"
Push changes to USB stick, make sure you always mount the usb to the same location or pushing will fail:
git push origin main
3 Limitations
- Binary data cannot use most of git's features (e.g. diff's).
- Git does not preserve file system meta data:
- file groups
- file owners
- file permissions, except executable bit
- The backup will always increase in size. If you run out of space
you'll need to delete the repo on the usb and the
.git
directory in the local repo and start form scratch. There are other ways to trim the remote repo size but those are out of scope for this tutorial. - Backups are not encrypted.
If these are deal breakers for you you might want to look into rsync or syncthing.
4 Advantages
- Git is used by practically every software project and therefore very reliable.
- Access to all of Git's features, mainly version control.
- If you know Git then you do not need to learn another piece of software.
- Fast recovery after you lose your data. Simply clone the repo and you are good to go.