Skip to content

Commit

Permalink
Merge pull request #46 from jan-brinkmann/Enable-pre-and-post-commands
Browse files Browse the repository at this point in the history
Enable pre and post commands
  • Loading branch information
jareware authored Jan 10, 2022
2 parents 9bd613d + ce07692 commit 612e633
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ Variable | Default | Notes
`BACKUP_CRON_EXPRESSION` | `@daily` | Standard debian-flavored `cron` expression for when the backup should run. Use e.g. `0 4 * * *` to back up at 4 AM every night. See the [man page](http://man7.org/linux/man-pages/man8/cron.8.html) or [crontab.guru](https://crontab.guru/) for more.
`BACKUP_FILENAME` | `backup-%Y-%m-%dT%H-%M-%S.tar.gz` | File name template for the backup file. Is passed through `date` for formatting. See the [man page](http://man7.org/linux/man-pages/man1/date.1.html) for more.
`BACKUP_ARCHIVE` | `/archive` | When this path is available within the container (i.e. you've mounted a Docker volume there), a finished backup file will get archived there after each run.
`PRE_COMMAND` | | Commands that is executed before the backup is transferred to `/archive`.
`POST_COMMAND` | | Commands that is executed after the backup has been transferred to `/archive`.
`BACKUP_UID` | `root (0)` | After backup file has been moved to archive location the file user ownership is changed to this UID.
`BACKUP_GID` | `$BACKUP_UID` | After backup file has been moved to archive location the file group ownership is changed to this GID.
`BACKUP_WAIT_SECONDS` | `0` | The backup script will sleep this many seconds between re-starting stopped containers, and proceeding with archiving/uploading the backup. This can be useful if you don't want the load/network spike of a large upload immediately after the load/network spike of container startup.
Expand Down
8 changes: 8 additions & 0 deletions src/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,18 @@ fi

if [ -d "$BACKUP_ARCHIVE" ]; then
info "Archiving backup"
if [ ! -z "$PRE_COMMAND" ]; then
echo "Pre command: $PRE_COMMAND"
$PRE_COMMAND
fi
mv -v "$BACKUP_FILENAME" "$BACKUP_ARCHIVE/$BACKUP_FILENAME"
if (($BACKUP_UID > 0)); then
chown -v $BACKUP_UID:$BACKUP_GID "$BACKUP_ARCHIVE/$BACKUP_FILENAME"
fi
if [ ! -z "$POST_COMMAND" ]; then
echo "Post command: $POST_COMMAND"
$POST_COMMAND
fi
fi

if [ -f "$BACKUP_FILENAME" ]; then
Expand Down
2 changes: 2 additions & 0 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ BACKUP_CRON_EXPRESSION="${BACKUP_CRON_EXPRESSION:-@daily}"
AWS_S3_BUCKET_NAME="${AWS_S3_BUCKET_NAME:-}"
AWS_GLACIER_VAULT_NAME="${AWS_GLACIER_VAULT_NAME:-}"
AWS_EXTRA_ARGS="${AWS_EXTRA_ARGS:-}"
PRE_COMMAND="${PRE_COMMAND:-}"
POST_COMMAND="${POST_COMMAND:-}"
SCP_HOST="${SCP_HOST:-}"
SCP_USER="${SCP_USER:-}"
SCP_DIRECTORY="${SCP_DIRECTORY:-}"
Expand Down
23 changes: 23 additions & 0 deletions test/pre-post-command/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: "3"

services:

dashboard:
image: grafana/grafana:7.4.5
volumes:
- grafana-data:/var/lib/grafana # This is where Grafana keeps its data

backup:
build: ../..
environment:
# Commands that is executed before the backup is transferred:
PRE_COMMAND: "ls -la /archive"
# Command that is executed after the backup has been transferred:
# "Delete all files in /archive that are older than seven days."
POST_COMMAND: "rm $$(find /archive/* -mtime +7)"
volumes:
- grafana-data:/backup/grafana-data:ro # Mount the Grafana data volume (as read-only)
- /home/pi/backups:/archive # Mount the directory where the backups are being stored

volumes:
grafana-data:

0 comments on commit 612e633

Please sign in to comment.