diff --git a/.gitignore b/.gitignore index f20cf4a..5b49202 100644 --- a/.gitignore +++ b/.gitignore @@ -37,5 +37,6 @@ yarn-error.log* !/.projenrc.js !/.versionrc.json !/LICENSE +!/docker-compose.yml !/package.json !version.json \ No newline at end of file diff --git a/.projenrc.js b/.projenrc.js index df5858f..c3b53fe 100644 --- a/.projenrc.js +++ b/.projenrc.js @@ -1,4 +1,4 @@ -const { NodeProject, ProjectType, SampleReadme } = require('projen'); +const { NodeProject, ProjectType, DockerCompose } = require('projen'); const project = new NodeProject({ authorAddress: 'damadden88@googlemail.de', @@ -9,4 +9,23 @@ const project = new NodeProject({ }); +new DockerCompose(project, { + version: '3.9', + services: { + influxdbs3backup: { + imageBuild: { + context: '.', + }, + environment: { + DATABASE: 'mydatabase', + DATABASE_HOST: '1.2.3.4', + S3_BUCKET: 'mybackupbucket', + AWS_ACCESS_KEY_ID: 'AKIAIOSFODNN7EXAMPLE', + AWS_SECRET_ACCESS_KEY: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', + AWS_DEFAULT_REGION: 'us-west-2', + }, + } +}, +}); + project.synth(); diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..58d376e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "folders": [], + "settings": {}, + "editor.formatOnSave": true, + "prettier.singleQuote": true, + "prettier.trailingComma": "all", +} diff --git a/Dockerfile b/Dockerfile index 7195280..0dfdb3c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ FROM influxdb:1.8.3 -RUN apt update -y && apt install awscli -y +RUN apt update -y && apt install awscli cron -y -COPY influxdb-to-s3.sh /usr/bin/influxdb-to-s3 +COPY influxdb-to-s3.sh /usr/bin/influxdb-to-s3.sh -ENTRYPOINT ["/usr/bin/influxdb-to-s3"] -CMD ["cron", "0 1 * * *"] +ENTRYPOINT ["/usr/bin/influxdb-to-s3.sh"] +CMD ["startcron"] \ No newline at end of file diff --git a/README.md b/README.md index f82b567..56df0ab 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ ... # Thanks To: - ... \ No newline at end of file +* Jacob Tomlinson https://github.com/jacobtomlinson/docker-influxdb-to-s3 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..16146e0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ + +version: '3.3' + +services: + influxdbs3backup: + build: + context: . + environment: + DATABASE: mydatabase + DATABASE_HOST: localhost + S3_BUCKET: mybackupbucket + AWS_ACCESS_KEY_ID: AKIAIOSFODNN7EXAMPLE + AWS_SECRET_ACCESS_KEY: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY + AWS_DEFAULT_REGION: us-west-2 diff --git a/influxdb-to-s3.sh b/influxdb-to-s3.sh index 54e971b..c803d82 100755 --- a/influxdb-to-s3.sh +++ b/influxdb-to-s3.sh @@ -2,6 +2,7 @@ set -e +export S3_BUCKET=${S3_BUCKET} # Check and set missing environment vars : ${S3_BUCKET:?"S3_BUCKET env variable is required"} if [[ -z ${S3_KEY_PREFIX} ]]; then @@ -18,13 +19,26 @@ export DATABASE_HOST=${DATABASE_HOST:-localhost} export DATABASE_PORT=${DATABASE_PORT:-8088} export DATABASE_META_DIR=${DATABASE_META_DIR:-/var/lib/influxdb/meta} export DATABASE_DATA_DIR=${DATABASE_DATA_DIR:-/var/lib/influxdb/data} +export CRON=${CRON:-"* * * * *"} export DATETIME=$(date "+%Y%m%d%H%M%S") # Add this script to the crontab and start crond -cron() { +startcron() { + echo "export S3_BUCKET=$S3_BUCKET" >> $HOME/.profile echo "Starting backup cron job with frequency '$1'" - echo "$1 $0 backup" > /var/spool/cron/crontabs/root - crond -f + + echo "$1 . $HOME/.profile; $0 backup >> /var/log/cron.log 2>&1" > /etc/cron.d/influxdbbackup + + cat /etc/cron.d/influxdbbackup + + # Apply cron job + crontab /etc/cron.d/influxdbbackup + + # Create the log file to be able to run tail + touch /var/log/cron.log + + # cat /var/spool/cron/crontabs/root + cron && tail -f /var/log/cron.log } # Dump the database to a file and push it to S3 @@ -107,8 +121,8 @@ restore() { # Handle command line arguments case "$1" in - "cron") - cron "$2" + "startcron") + startcron "$CRON" ;; "backup") backup @@ -118,5 +132,5 @@ case "$1" in ;; *) echo "Invalid command '$@'" - echo "Usage: $0 {backup|restore|cron }" + echo "Usage: $0 {backup|restore|startcron}" esac