Skip to content

Instantly share code, notes, and snippets.

@wica128
Created December 6, 2016 13:59
Show Gist options
  • Save wica128/eca2eb9a8fc98ebf57eacf70c60bd0b2 to your computer and use it in GitHub Desktop.
Save wica128/eca2eb9a8fc98ebf57eacf70c60bd0b2 to your computer and use it in GitHub Desktop.
#!/bin/bash
mkrespjson=$( cat <<EOF
{
"type": "fs",
"settings": {
"location": "/backups/snapshots/%s",
"compress": true
}
}
EOF
)
mksnapshotjson=$( cat <<EOF
{
"indices": "%s",
"ignore_unavailable": true,
"include_global_state": false
}
EOF
)
SNDIR="/backups/snapshots"
DATE=$(date -d "2 day ago" +%Y.%m.%d)
# Fix some settings
curl -XPUT 'http://localhost:9200/_settings' -d '
{
"index" : {
"number_of_replicas" : 0
}
}'
for x in apache syslog
do
INDEX="${x}-${DATE}"
mkdir $SNDIR/${INDEX}
chown -R elasticsearch:elasticsearch /backups/snapshots/${INDEX}
# Create repo
curl -XPUT "http://localhost:9200/_snapshot/${INDEX}" -d "$(printf "$mkrespjson" "$INDEX")"
# Create Snapshot
curl -XPUT "http://localhost:9200/_snapshot/${INDEX}/snapshot_1?wait_for_completion=true" -d "$( printf "$mksnapshotjson" "$INDEX")"
# Tar it
tar cjpf /backups/tar/${INDEX}.tbz2 $SNDIR/${INDEX}
rm -rf $SNDIR/${INDEX}
# Delete Index
curl -XDELETE "http://localhost:9200/${INDEX}/"
done
# Clean up
DATE=$(date -d "4 day ago" +%Y.%m.%d)
rm /backups/tar/*$(date -d "4 day ago" +%Y.%m.%d)*.tbz2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment