Skip to content

Instantly share code, notes, and snippets.

@maxprehl
Last active February 4, 2022 13:26
Show Gist options
  • Save maxprehl/cedf3f73b06a97e5b8e08e4f77cad6aa to your computer and use it in GitHub Desktop.
Save maxprehl/cedf3f73b06a97e5b8e08e4f77cad6aa to your computer and use it in GitHub Desktop.
rsync makefile for general purpose sync and backups
# Makefile
#
# For syncing the current directory with a remote using rsync
#
# Author: maxprehl
#
# Refs:
# https://www.gnu.org/software/make/manual/html_node/index.html#Top
# https://www.man7.org/linux/man-pages/man1/rsync.1.html
# https://www.reddit.com/r/DataHoarder/comments/sixnp0/i_was_told_i_belong_here/
# Structure
# ---------
# Local Dir
# \--- your data here
#
# Remote Dir
# +--- current
# | \--- your data here
# +--- backup
# | \--- 2022-02-04__1643971072
# | \--- incremental backup tree (rsync --backup-dir)
# \--- rsync.log
# Usage
# -----
# Remote-local sync:
# make sync
#
# Sync dry run:
# make drysync
#
# Update remote to match local exactly (delete stuff):
# make delete
#
# Full list of options:
# get pull put push sync dry debug
# dry drypull drypush drysync drydel
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
date := $(shell date '+%Y-%m-%d_%s')
HOST := pop.p
ROOT := $$HOME/Sync
REMOTE_DIR := $(ROOT)/$(current_dir)
REMOTE_BAK := $(REMOTE_DIR)/backup/$(date)
REMOTE_LOG := $(REMOTE_DIR)/rsync.log
REMOTE := $(HOST):$(REMOTE_DIR)/current/
LOCAL := ./
# LOCAL_LOG :=
PARAMS := --verbose --compress --recursive --times --human-readable
PARAMS += --exclude-from='./.rsyncignore'
BACKUP := --backup --backup-dir=$(REMOTE_BAK)
LOGFILE := --remote-option=--log-file=$(REMOTE_LOG)
MAIN := $(PARAMS) $(BACKUP) $(LOGFILE)
.PHONY: dry drypull drypush drysync drydel drydelete get pull put push sync dry delete debug prep
dry drypull drypush drysync drydel: MAIN += --dry-run
dry : debug
drypull: pull
drypush: push
drysync: sync
drydel drydelete: delete
put push get pull : MAIN += --update
sync: get put
put push: .rsyncignore
rsync $(MAIN) $(LOCAL) $(REMOTE)
get pull: .rsyncignore
rsync $(MAIN) $(REMOTE) $(LOCAL)
delete: .rsyncignore
rsync $(MAIN) --delete $(LOCAL) $(REMOTE)
debug:
# mkfile_path=$(mkfile_path)
# current_dir=$(current_dir)
# REMOTE_BAK=$(REMOTE_BAK)
# REMOTE_LOG=$(REMOTE_LOG)
# REMOTE=$(REMOTE)
# LOCAL=$(LOCAL)
# MAIN=$(MAIN)
prep:
ssh $(HOST) "mkdir -p $(REMOTE_DIR) && mkdir -p $(REMOTE_DIR)/current && mkdir -p $(REMOTE_DIR)/backup && touch $(REMOTE_LOG)"
@maxprehl
Copy link
Author

maxprehl commented Feb 4, 2022

Replace HOST and ROOT with your own backup server info. Then copy this makefile in any folder that needs backing up.

Note: you do need to create the basic structure on the server for rsync to work. It cannot create it's own logfile, backup dir, or current dir.

@maxprehl
Copy link
Author

maxprehl commented Feb 4, 2022

Update: added a prep target that sets up the structure for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment