Skip to content
Open
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
1 change: 1 addition & 0 deletions pillar/registry.sls
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sync:
/home/collect/scrapyd/dbs:
/home/collect/scrapyd/eggs:
/home/collect/scrapyd/jobs:
exclude: "*/requests.queue/*"
/home/collect/scrapyd/logs:

apache:
Expand Down
44 changes: 35 additions & 9 deletions salt/aws/files/sync-to-s3.sh
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

Expand All @@ -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"
Copy link
Copy Markdown
Member

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"

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
;;
*)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This final set -e does nothing, I intend for it to prevent human error keeping the flag active if the script was extended in the future.

16 changes: 5 additions & 11 deletions salt/aws/sync.sls
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, maybe do (I think items is safe on None?):

{%- 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
Loading