commit 22b5f7cdd7b183b9d8d0cd7d52964547e185a6da Author: OriolFilter Date: Sun Jan 16 17:29:42 2022 +0100 upload diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..e6ff14b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM alpine:3.15.0 +RUN apk add --no-cache openssh git bash +ADD ./credential_helper.sh /scripts/ +ADD ./main.sh /scripts/ +RUN chmod +x /scripts/main.sh /scripts/credential_helper.sh +WORKDIR /GIT_DIR + +ENV __CREDENTIAL_HELPER_SCRIPT_PATH="/scripts/credential_helper.sh" + +ENV GIT_REMOTE_BRANCH='master' +ENV GIT_PASSWORD='' +ENV GIT_USERNAME='' +ENV GIT_REPO_URL='' +ENV GIT_FORCE_PUSH='' +#ENV SKIP_SSL_CHECK='' + +ENV GIT_COMMIT_MESSAGE='.' +ENV GIT_COMMIT_AUTHOR='' +ENV GIT_COMMIT_EMAIL='' + +ENTRYPOINT [ "/bin/bash", "-l", "-c" ] +CMD ["/scripts/main.sh"] + diff --git a/README.md b/README.md new file mode 100755 index 0000000..05e305c --- /dev/null +++ b/README.md @@ -0,0 +1,82 @@ +## Usage + +1. Mount the folders/files you want to push into a repository **in** the **/GIT_DIR** directory. +2. Finally, set the environment variables required. +3. Optional, configure crontab to have periodical push in case you want to use it as a backup (ie. factorio server public world backups). + +#### Notes + +In case of wanting to push the content of a directory, just mount it **as** **/GIT_DIR** + +### usage? + +Use a **env** file with a simple env config + +```text +GIT_REPO_URL=https://gitserver.url/user/repo_name.git +GIT_USERNAME=username +GIT_PASSWORD=TOKEN (or password...) +GIT_REMOTE_BRANCH=dev +GIT_FORCE_PUSH=yes +``` + +```shell +_time=`date "+%Y-%M-%d"` && \ +docker run -v "$(pwd)/folder_to_push:/GIT_DIR" -e GIT_COMMIT_MESSAGE="Commit done at $_time" --env-file env.file -it testing/git_backup:latest +``` + +In this scenario we are storing the required information to push in a **env** file + +## Git ignore + +Is as simple to create a **.gitignore** file and place it in the folder that you want to back up. + +#### In case of **.gitignore** being inside the folder to push +```shell +# .gitignore +ignored_file +``` + +```shell +docker run -v "$(pwd)/folder_to_push:/GIT_DIR" -e GIT_COMMIT_MESSAGE="Commit done at $_time" --env-file env.file -it testing/git_backup:latest +``` + +#### In case of having to push multiple directories/files + +```shell +docker run -v "/path_to/file_to_push1:/GIT_DIR/file_to_push1" "/path_to/file_to_push2:/GIT_DIR/file_to_push2" "/path_to/gitignore:/GIT_DIR/.gitignore" -e GIT_COMMIT_MESSAGE="Commit done at $_time" --env-file env.file -it testing/git_backup:latest +``` + +## Environment + +### Configurable + + +|ENV|DEFAULT value|DESCRIPTION| +|---|---|---| +|GIT_REPO_URL||Url from the repository| +|GIT_USERNAME||Username or email from the git server| +|GIT_PASSWORD||Token (or password, which is not recommended) from the git server| +|GIT_FORCE_PUSH||Used to force the push, to enable it add any content| +|GIT_REMOTE_BRANCH|master|Branch to push| +|GIT_COMMIT_MESSAGE|.|Message used during the commit| +|GIT_COMMIT_AUTHOR||On empty will use "Name"| +|GIT_COMMIT_EMAIL||On empty will commit as "<>"| +|---|---|---| +|SKIP_SSL_CHECK|---|disabled| +|GIT_TAG|---|disabled| +|GIT_TAG_MESSAGE|---|disabled| +|TAR_CONTENT|---|disabled| + +*ยน **---** means that it's empty + +### Not Configurable +##### This exists as a documentation + +|ENV|DEFAULT|DESCRIPTION| +|---|---|---| +|__CREDENTIAL_HELPER_SCRIPT_PATH | /scripts/credential_helper.sh"| Points to the script used to automate git authentication (credential manager) | + +## Docker compose + +... \ No newline at end of file diff --git a/credential_helper.sh b/credential_helper.sh new file mode 100755 index 0000000..9eb4b8f --- /dev/null +++ b/credential_helper.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo username="$GIT_USERNAME" +echo password="$GIT_PASSWORD" \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..d83cd51 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,10 @@ +services: + backup: + image: testing/git_backup + build: + context: ./ + dockerfile: Dockerfile + volumes: + - ./tmpf:/GIT_DIR:rw + env_file: + - ./env.file \ No newline at end of file diff --git a/env.file b/env.file new file mode 100755 index 0000000..c43bb62 --- /dev/null +++ b/env.file @@ -0,0 +1,8 @@ +GIT_REPO_URL=git.server.url +GIT_USERNAME=username +GIT_PASSWORD=token_passw +GIT_REMOTE_BRANCH=dev +GIT_FORCE_PUSH=yes + +GIT_COMMIT_AUTHOR= +GIT_COMMIT_EMAIL= \ No newline at end of file diff --git a/main.sh b/main.sh new file mode 100755 index 0000000..25f4d30 --- /dev/null +++ b/main.sh @@ -0,0 +1,84 @@ +#!/bin/bash +#!/usr/bin/env bash +# https://gist.github.com/mihow/9c7f559807069a03e302605691f85572 + +######################### +#### Script env +## +_FORCE_PUSH="" + +env +######################### +#### Validate username and password exists +## + +if [ -z "${GIT_USERNAME}" ]; + then + echo "Please set up a username in the environment" + exit 127 +fi + +if [ -z "${GIT_PASSWORD}" ]; + then + echo "Please set up a password in the environment" + exit 127 +fi + +if [ -z "${GIT_REPO_URL}" ]; + then + echo "Please set up a url to push the environment in the environment" + exit 127 +fi + +#echo "${GIT_REPO_URL:-not found}" +# Remove .git +rm -rfv ./.git + +## TMP +# Create file to keep updating repo +date > README.md + +# Tar files in folder (Unless specified otherwise in the config) +#cp -rv /BACKUP_DIR /__BACKUP_DIR || exit 127 +#cd /__BACKUP_DIR || exit 127 +# Git init +git init + +# Git add +git add . + + +#??? +git config user.email "${GIT_COMMIT_EMAIL:-.}" + +# Git commit +echo "email $GIT_COMMIT_EMAIL" +echo "author $GIT_COMMIT_AUTHOR" +git commit --author="${GIT_COMMIT_AUTHOR:-Name} ${GIT_COMMIT_EMAIL:-<>}" -m "${GIT_COMMIT_MESSAGE}" + +# Git add remote tag +git remote add origin "${GIT_REPO_URL}" + +## Git tag +#if [ -n "${GIT_TAG}" ] +# then +# +# git tag -a "${GIT_TAG}" -m "${GIT_TAG_MESSAGE:-$GIT_TAG_NAME}" +# _FORCE_PUSH='-f' +#fi + +# Set up credential helper script to use environment variables +chmod +x "$__CREDENTIAL_HELPER_SCRIPT_PATH" +git config credential.helper "/bin/bash ${__CREDENTIAL_HELPER_SCRIPT_PATH}" + +# Git push + + +if [ -n "${GIT_FORCE_PUSH}" ] + then + _FORCE_PUSH='-f' +fi + + +git push ${_FORCE_PUSH} origin "HEAD:${GIT_REMOTE_BRANCH}" +#bash \ No newline at end of file