From 98ed1b9769b823c731e1f102eb35c35600a68bb3 Mon Sep 17 00:00:00 2001 From: paulmataruso Date: Wed, 23 Oct 2024 02:02:53 +0000 Subject: [PATCH] Upload files to "/" --- Dockerfile | 10 ++++++++++ README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ command.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 command.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..87bbc94 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM istepanov/cron +MAINTAINER Ilya Stepanov + +RUN apk add --no-cache git ca-certificates + +RUN mkdir -p /target + +ENV GIT_BRANCH 'master' +ENV GIT_COMMIT_MESSAGE 'Automatic backup' +ENV TARGET_FOLDER '/target' diff --git a/README.md b/README.md new file mode 100644 index 0000000..a431a0c --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +istepanov/backup-to-git +======================= + +[![Docker Stars](https://img.shields.io/docker/stars/istepanov/backup-to-git.svg)](https://hub.docker.com/r/istepanov/backup-to-git/) +[![Docker Pulls](https://img.shields.io/docker/pulls/istepanov/backup-to-git.svg)](https://hub.docker.com/r/istepanov/backup-to-git/) +[![Docker Build](https://img.shields.io/docker/automated/istepanov/backup-to-git.svg)](https://hub.docker.com/r/istepanov/backup-to-git/) +[![Layers](https://images.microbadger.com/badges/image/istepanov/backup-to-git.svg)](https://microbadger.com/images/istepanov/backup-to-git) + +Docker container that periodically backups files to a remote git repository. + +### Usage + + docker run -d [OPTIONS] istepanov/backup-to-git [no-cron] + +### Required parameters: + +* `-e GIT_NAME='Backup User'`: git user name. +* `-e GIT_EMAIL='backup_user@example.com'`: git user email. +* `-e GIT_URL=https://username:password@example.com/path`: git remote repo. +* `-v /path/to/target/folder:/target:ro`: mount target local folder to container's data folder. Content of this folder will be pushed to git repo. + +### Optional parameters: + +* `-e 'CRON_SCHEDULE=0 1 * * *'`: specifies when cron job starts ([details](http://en.wikipedia.org/wiki/Cron)). Default is `0 1 * * *` (runs every day at 1:00 am). +* `-e GIT_BRANCH=branch_name`: git branch to put commits to (default is 'master'). +* `-e GIT_COMMIT_MESSAGE='Custom message'`: Custom commit message (default is 'Automatic backup'). +* `-e TARGET_FOLDER=/target`: target folder inside the container (default is '/target'). + +### Commands: + +* `no-cron`: if specified, run container once and exit (no cron scheduling). + +### Examples: + +Backup changes to git every minute: + + docker run -d \ + -v GIT_NAME: 'Backup User' \ + -v GIT_EMAIL: 'backup_user@example.com' \ + -v GIT_URL: https://username:password@bitbucket.org/username/repo.git + -v CRON_SCHEDULE: '* * * * *' + -v /home/user/data:/target:ro + istepanov/backup-to-git diff --git a/command.sh b/command.sh new file mode 100644 index 0000000..fe37dda --- /dev/null +++ b/command.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +set -e + +# required env vars +: ${GIT_NAME:?"GIT_NAME env variable is required"} +: ${GIT_EMAIL:?"GIT_EMAIL env variable is required"} +: ${GIT_URL:?"GIT_URL env variable is required"} + +export GIT_DIR="/var/git" +export GIT_WORK_TREE="$TARGET_FOLDER" + +echo "Job started: $(date)" +echo "$GIT_WORK_TREE" + +if [ ! -d "$GIT_DIR" ]; then + mkdir -p "$GIT_DIR" + git init + git config user.name "$GIT_NAME" + git config user.email "$GIT_EMAIL" + git remote add origin "$GIT_URL" + + mkdir -p "$GIT_DIR/info" + echo '' > "$GIT_DIR/info/exclude" + while IFS=';' read -ra IGNORE_PATTERNS; do + for i in "${IGNORE_PATTERNS[@]}"; do + echo "$i" >> "$GIT_DIR/info/exclude" + done + done <<< "$GIT_IGNORE" + + git fetch --all + git symbolic-ref HEAD refs/remotes/origin/$GIT_BRANCH + git reset + git checkout -b $GIT_BRANCH + + # to get rid of ignored files that are already commited to the repo, + # remove all files for index (but not from working directory!), + # then re-add everything back to index + git rm -r --cached . > /dev/null || true +fi + +git add -A + +git commit -m "$GIT_COMMIT_MESSAGE" +git push origin $GIT_BRANCH + +echo "Job finished: $(date)"