Commit | Line | Data |
---|---|---|
0155bbac JD |
1 | #!/bin/bash |
2 | ||
3 | usage() | |
4 | { | |
5 | cat <<EOF | |
6 | USAGE: | |
7 | ./deployfile.sh -f filename -p /path/to/destination/ [ -u remoteuser ] [ -b ] host1 [ host2 ... ] | |
8 | ||
9 | OPTIONS: | |
10 | -f Name of file to be deployed | |
11 | -p Path on remote server where file should be deployed to | |
12 | -u Username to use when logging into remote server; if not provided, defaults to current user | |
13 | -b Backup flag (optional); creates a backup instead of overwriting file on remote server | |
14 | -h Print this message and exit | |
15 | EOF | |
16 | } | |
17 | ||
18 | while getopts ":f:p:u:bh" opt | |
19 | do | |
20 | case $opt in | |
21 | f ) FILE=$OPTARG;; | |
22 | p ) DEST=$OPTARG;; | |
23 | u ) USER=$OPTARG;; | |
24 | b ) BACKUP="-b --suffix=.`date +%Y%m%d%H%M`";; | |
25 | h ) usage && exit 0;; | |
26 | esac | |
27 | done | |
28 | shift $(($OPTIND - 1)) | |
29 | ||
30 | if [[ -z "$FILE" ]] || [[ -z "$DEST" ]] | [[ $# -lt 1 ]] | |
31 | then | |
32 | usage | |
33 | exit 1 | |
34 | else | |
35 | ||
36 | if [[ -z "$BACKUP" ]] | |
37 | then | |
38 | SSH="ssh" | |
39 | else | |
40 | SSH="ssh -l${USER}" | |
41 | fi | |
42 | ||
43 | for HOST in "$@" | |
44 | do | |
45 | RSYNC_DEST="${HOST}:${DEST}" | |
46 | #echo -e "$HOST:\n\tFILE=$FILE\n\tDEST=$DEST\n\tUSER=$USER\n\tBACKUP=$BACKUP\n\tSSH=$SSH\n\tRSYNC_DEST=$RSYNC_DEST\n" | |
47 | rsync -e"${SSH}" $BACKUP -avz $FILE $RSYNC_DEST | |
48 | done | |
49 | fi |