-
Notifications
You must be signed in to change notification settings - Fork 4
refactor sync-to-s3.sh per directory #623
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Backup multiple directories and upload to AWS S3 | ||
| # Sync a single directory into S3. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
|
|
@@ -24,16 +24,42 @@ if [ ! -x "$AWS_CLI" ]; then | |
| exit 3 | ||
| fi | ||
|
|
||
| if [ -z "$SYNC_DIRECTORIES" ]; then | ||
| echo "Error: SYNC_DIRECTORIES isn't set or is empty" | ||
| if [ -z "$S3_SYNC_BUCKET" ]; then | ||
| echo "Error: S3_SYNC_BUCKET isn't set or is empty" | ||
| exit 4 | ||
| fi | ||
|
|
||
| for DIRECTORY in "${SYNC_DIRECTORIES[@]}"; do | ||
| SAFENAME="${DIRECTORY/#\//}" | ||
| SAFENAME="${SAFENAME/%\//}" | ||
| AWS_ARGS=(--only-show-errors --delete) | ||
|
|
||
| set +e | ||
| $AWS_CLI s3 sync "$DIRECTORY" "s3://$S3_SYNC_BUCKET/$SAFENAME/" --only-show-errors --delete 2>&1 | grep -v "You did not provide the number of bytes specified by the Content-Length HTTP header" | ||
| set -e | ||
| # Parse script arguments | ||
| while [[ $# -gt 0 ]]; do | ||
| case $1 in | ||
| --exclude) | ||
| if [ -z "$2" ]; then | ||
| echo "Missing path." | ||
| exit 1 | ||
| fi | ||
| AWS_ARGS+=(--exclude "\"$2\"") | ||
| shift 2 | ||
| ;; | ||
| -*) | ||
| echo "Unknown option" | ||
| exit 1 | ||
| ;; | ||
| *) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a bit paranoid, as we control the Pillar data, and should only be setting real args. I think we can pass through args directly. |
||
| DIRECTORY=$1 | ||
| SAFENAME=${DIRECTORY/#\//} | ||
| SAFENAME="${SAFENAME/%\//}" | ||
| shift | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| if [ -e "$DIRECTORY" ]; then | ||
| echo "Error: DIRECTORY isn't set or doesn't exist." | ||
| exit 5 | ||
| fi | ||
|
|
||
| set +e | ||
| $AWS_CLI s3 sync "${AWS_ARGS[@]}" "$DIRECTORY" "s3://$S3_SYNC_BUCKET/$SAFENAME/" | ||
| set -e | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This final |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,16 +16,10 @@ include: | |
| file.managed: | ||
| - contents: | | ||
| MAILTO=root | ||
| 15 03,15 * * * root /home/sysadmin-tools/bin/sync-to-s3.sh | ||
| {%- for directory, entry in pillar.sync.directories|items %} | ||
| {%- set minute = (loop.index0 * 5) % 60 %} | ||
| {{minute}} 03,15 * * * root /home/sysadmin-tools/bin/sync-to-s3.sh {{ directory }} | ||
| {%- if entry and 'exclude' in entry %} --exclude "{{ entry.exclude }}"{% endif %} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checking: This puts the cron job onto two lines. Does cron read onto the second line?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, maybe do (I think {%- for option, value in entry|items %} --{{ option }} "{{ value }}"{% endfor %} |
||
| {%- endfor %} | ||
| - require: | ||
| - file: /home/sysadmin-tools/bin/sync-to-s3.sh | ||
| set SYNC_DIRECTORIES setting: | ||
| file.keyvalue: | ||
| - name: /home/sysadmin-tools/aws-settings.local | ||
| - key: SYNC_DIRECTORIES | ||
| - value: '( "{{ pillar.sync.directories|join('" "') }}" )' | ||
| - append_if_not_found: True | ||
| - require: | ||
| - file: /home/sysadmin-tools/bin | ||
| - sls: aws | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we not need this anymore?
2>&1 | grep -v "You did not provide the number of bytes specified by the Content-Length HTTP header"