https://github.com/ni554n/git-local-backup
CLI tool to backup local files that are not pushed to the remote (yet)
https://github.com/ni554n/git-local-backup
automation backup cli git git-tool go
Last synced: 2 months ago
JSON representation
CLI tool to backup local files that are not pushed to the remote (yet)
- Host: GitHub
- URL: https://github.com/ni554n/git-local-backup
- Owner: ni554n
- Created: 2024-09-18T01:19:58.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-28T23:38:41.000Z (12 months ago)
- Last Synced: 2025-03-01T00:36:06.473Z (12 months ago)
- Topics: automation, backup, cli, git, git-tool, go
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Git Local Backup
A CLI tool for copying local files from Git projects to a cloud drive or a backup disk for safekeeping.
```
- Projects - Backup
- project-1 --> - project-1
- .git - main.go
- main.go [modified]
- utils.go [deleted]
- README.md [origin]
- project-2 --> - project-2
- .git - .env
- .gitignore - index.js
- .env [forced] - README.md
- index.js [untracked]
- package.json [origin]
- README.md [added]
```
It copies only the files that have been modified since the last backup, including:
- Committed files that are not yet pushed to the remote repository
- Working and staged files that are not yet committed
- Files that are not yet tracked by `git add`
- Any .gitignored file included via `--force-include` flag
> … basically every unpushed file that can be lost during an incident.
## Why?
Most modern editors now have built-in local history feature, so pushing unfinished changes solely
for backup purposes seems counter-productive to me.
To reduce the risk of accidental data loss in between remote pushing,
this tool can provide a layer of protection.
Plus, there's a lot of important files that cannot be committed to VCS,
such as `.env` containing private keys that should be backed up locally with a tool like this.
## Prerequisites
- Git is installed and added to the `PATH` so that it's accessible from anywhere
- Have some sort of a backup solution in place
- Optionally, subscribe to update notifications by selecting `Watch > Custom > Releases` on GitHub
## Usage
Download the [latest release](https://github.com/ni554n/git-local-backup/releases/latest) and
extract (`tar -xvzf`) the binary to a suitable path.
Here's all the options you can configure:
| Flag | Description |
| --- | --- |
| `--projects-path` | Path to the projects directory (required) |
| `--backup-path` | Path to an empty backup directory (required)
Otherwise, existing files may be removed from that directory. |
| `--remote-branch` | Remote name (default: `origin`) |
| `--force-include` | Always include a git ignored file or directory like `.git`.
Specify it multiple times to include multiple items. |
| `--dry-run` | Preview changes without modifying the backup directory |
### Test drive the command
Assuming all your Git projects are in `~/Projects` and you want to backup to `~/OneDrive/Backup/Projects`:
```sh
/path/to/git-local-backup --projects-path "~/Projects" --backup-path "~/OneDrive/Backup/Projects" --dry-run
```
If you also want to back up Git internals like stashes, or other gitignored files such as `.env`:
```sh
/path/to/git-local-backup --projects-path "~/Projects" --backup-path "~/OneDrive/Backup/Projects" --force-include ".git" --force-include ".env" --dry-run
```
If you are satisfied with the output, remove the `--dry-run` flag, and
schedule the command to run periodically using the instructions below.
Linux (Crontab)
Run `crontab -e` and add the following line:
```txt
*/15 * * * * /path/to/git-local-backup "~/Projects" --backup-path "~/OneDrive/Backup/Projects"
```
MacOS (Launchd)
1. Create this plist file in the `~/Library/LaunchAgents/` and configure it your command:
`git-local-backup.plist`
```xml
Label
Git Local Backup
ProgramArguments
/path/to/git-local-backup --projects-path "~/Projects" --backup-path "~/OneDrive/Backup/Projects"
StartInterval
900
RunAtLoad
```
2. Load it via `launchctl load ~/Library/LaunchAgents/git-local-backup.plist`
3. Start it via `launchctl start git-local-backup`
4. Check the status via `launchctl list | grep git-local-backup`. A status of zero means a successful run.
Windows (Task Scheduler)
1. Create the following file and configure it with your command. You can also modify it later during import.
`Git Local Backup.xml`
```xml
ni554n
https://github.com/ni554n/git-local-backup
\ni554n\Git Local Backup
PT15M
false
true
S4U
LeastPrivilege
StopExisting
false
true
true
false
false
true
false
true
true
false
false
false
true
false
PT0S
7
\path\to\the\git-local-backup.exe
--projects-dir "~/Projects" --backup-dir "~/OneDrive/Backup/Projects"
```
2. Open `Task Scheduler` and import this task via `Action > Import Task` from the top menu bar
3. Check both `Run whether user is logged on or not` and `Do not store password` option.
Otherwise, a terminal window will pop up on each run.
4. To test it, manually run the task from `ni554n` folder
5. Refresh the task list and check the `Last Run Result` column to see if it's a successful run
## Information
**Author:** [Nissan Ahmed](https://anissan.com) ([@ni554n](https://twitter.com/ni554n))
**Donate:** [PayPal](https://paypal.me/ni554n)