Skip to content

Commit

Permalink
feat: add cron
Browse files Browse the repository at this point in the history
  • Loading branch information
mmuller88 committed Jan 15, 2021
1 parent 690cc61 commit 61ad99d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ yarn-error.log*
!/.projenrc.js
!/.versionrc.json
!/LICENSE
!/docker-compose.yml
!/package.json
!version.json
21 changes: 20 additions & 1 deletion .projenrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { NodeProject, ProjectType, SampleReadme } = require('projen');
const { NodeProject, ProjectType, DockerCompose } = require('projen');

const project = new NodeProject({
authorAddress: '[email protected]',
Expand All @@ -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();
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"folders": [],
"settings": {},
"editor.formatOnSave": true,
"prettier.singleQuote": true,
"prettier.trailingComma": "all",
}
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
...

# Thanks To:
...
* Jacob Tomlinson https://github.com/jacobtomlinson/docker-influxdb-to-s3
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
26 changes: 20 additions & 6 deletions influxdb-to-s3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -107,8 +121,8 @@ restore() {

# Handle command line arguments
case "$1" in
"cron")
cron "$2"
"startcron")
startcron "$CRON"
;;
"backup")
backup
Expand All @@ -118,5 +132,5 @@ case "$1" in
;;
*)
echo "Invalid command '$@'"
echo "Usage: $0 {backup|restore|cron <pattern>}"
echo "Usage: $0 {backup|restore|startcron}"
esac

0 comments on commit 61ad99d

Please sign in to comment.