1 #!/bin/bash 2 # 3 # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 USAGE="Usage: ./es_create_alias.sh \ 8 [-s <server>] [-p <port>] [-i <index>] [-a <alias>] 9 Example usage: ./es_create_alias.sh -s 172.25.61.45 -p 9200 -i \ 10 test_index -a test_index_alias" 11 12 HELP="${USAGE}\n\n\ 13 Create a new alias so that we can refer to an index via an alternate name.\n\ 14 This is useful so we can remap an index without any downtime. \n\ 15 16 Options:\n\ 17 -s IP of server running elasticsearch\n\ 18 -p Port of server running elasticsearch\n\ 19 -a A new name that we can refer to the index as \n\ 20 -i elasticsearch index, i.e. atlantis4.mtv, cautotest, localhost, etc.\n" 21 22 SERVER= 23 PORT= 24 ALIAS= 25 INDEX= 26 while getopts ":s:p:a:i:" opt; do 27 case $opt in 28 s) 29 SERVER=$OPTARG 30 ;; 31 p) 32 PORT=$OPTARG 33 ;; 34 a) 35 ALIAS=$OPTARG 36 ;; 37 i) 38 INDEX=$OPTARG 39 ;; 40 \?) 41 echo "Invalid option: -$OPTARG" >&2 42 echo "${USAGE}" >&2 43 exit 1 44 ;; 45 :) 46 echo "Option -$OPTARG requires an argument." >&2 47 echo "${USAGE}" >&2 48 exit 1 49 ;; 50 esac 51 done 52 53 echo "Creating alias ${ALIAS} for index ${INDEX} for 54 ES server at: ${SERVER}:${PORT}..." 55 56 57 curl -XPOST ${SERVER}:${PORT}/_aliases -d ' 58 { 59 "actions": [ 60 { "add": { 61 "alias": '"\"${ALIAS}\""', 62 "index": '"\"${INDEX}\""' 63 }} 64 ] 65 }' 66 67 echo ''