Upload files to "/"

This commit is contained in:
2024-10-23 02:02:53 +00:00
parent fb7f4ba6ce
commit 98ed1b9769
3 changed files with 100 additions and 0 deletions

10
Dockerfile Normal file
View File

@@ -0,0 +1,10 @@
FROM istepanov/cron
MAINTAINER Ilya Stepanov <dev@ilyastepanov.com>
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'

43
README.md Normal file
View File

@@ -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

47
command.sh Normal file
View File

@@ -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)"