# Makefile for restic backups

# Hard-coded settings
RESTIC_REPOSITORY := /mnt/data/OneDrive/backup/us
RESTIC_PASSWORD   := Shan33779488
RESTIC_BIN        := /usr/bin/restic

# Sources to back up
SOURCES := /root/docker/ /var/lib/docker/volumes/

# Common flags
BACKUP_FLAGS := --verbose
CHECK_FLAGS  := --read-data-subset=10%
FORGET_FLAGS := --keep-daily 7 --keep-weekly 5 --keep-monthly 12 --prune

.PHONY: backup check forget-prune init print-env cron-install cron-remove

print-env:
	@echo "Repository: $(RESTIC_REPOSITORY)"; echo "Sources: $(SOURCES)"; echo "Restic: $(RESTIC_BIN)"; echo "Backup flags: $(BACKUP_FLAGS)"  # [web:70]

init:
	RESTIC_REPOSITORY=$(RESTIC_REPOSITORY) RESTIC_PASSWORD=$(RESTIC_PASSWORD) $(RESTIC_BIN) init || true  # [web:57]

backup:
	RESTIC_REPOSITORY=$(RESTIC_REPOSITORY) RESTIC_PASSWORD=$(RESTIC_PASSWORD) $(RESTIC_BIN) backup $(SOURCES) $(BACKUP_FLAGS)  # [web:57]

check:
	RESTIC_REPOSITORY=$(RESTIC_REPOSITORY) RESTIC_PASSWORD=$(RESTIC_PASSWORD) $(RESTIC_BIN) check $(CHECK_FLAGS)  # [web:57]

forget-prune:
	RESTIC_REPOSITORY=$(RESTIC_REPOSITORY) RESTIC_PASSWORD=$(RESTIC_PASSWORD) $(RESTIC_BIN) forget $(FORGET_FLAGS)  # [web:57]

# Install a root crontab entry to run backup every 6 hours at minute 0, with logging
cron-install:
	@(crontab -l 2>/dev/null; echo '0 */6 * * * RESTIC_REPOSITORY=$(RESTIC_REPOSITORY) RESTIC_PASSWORD=$(RESTIC_PASSWORD) $(RESTIC_BIN) backup $(SOURCES) $(BACKUP_FLAGS) >> /var/log/restic-backup.log 2>&1') | crontab -  # [web:40]

cron-remove:
	@crontab -l | grep -v '$(RESTIC_BIN) backup' | crontab - || true  # [web:40]
