Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable pre and post commands #46

Merged
merged 5 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
Comment on lines +231 to +232
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`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`.
`PRE_COMMAND` | | Command that is executed before the backup is transferred to `/archive`.
`POST_COMMAND` | | Command that is executed after the backup has been transferred to `/archive`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typos - thank you!

`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)"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautifully concise 👏

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: