aboutsummaryrefslogtreecommitdiff
path: root/dbdssn.sh
blob: 1395a81e0b5deacf9815d768453e41f58e7b5419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/bin/sh
# -*- ENCODING: UTF-8 -*-
# Copyright (C) 2018 by ziggys
# Copyright (C) 2017-2018 by xiku (xiku@p4g.club)
# License BSD 2.0 (3-Clause)

# scritpinfo
SCRIPTNAME="${0##*/}"
SCRIPTNAME="${SCRIPTNAME%%.sh}"
VERSION='2.0.3'                        # Fix syntax
AUTHOR="ziggys"
CYEAR="$(date +%Y)"
ITIME="$(date +%s)"

# usage
usage () {
test "${#}" = 0 || echo "${@}"
echo "${SCRIPTNAME} version ${VERSION}"
echo "  Copyright (C) 2017-${CYEAR} by ${AUTHOR} and 2017-${CYEAR} xiku (xiku@p4g.club)"
echo
echo "${SCRIPTNAME} comes with ABSOLUTELY NO WARRANTY. This is free software, and you"
echo "  are welcome to redistribute it under certain conditions. See the BSD 2.0"
echo "  license for details"
echo
echo "${SCRIPTNAME} is a shellscript intended to ease and automatize database backup"
echo "  processes asociated to mysql (mariadb) and postgresql instances with"
echo "  an approach on security."
echo 
echo "Usage:  ${0} [OPTIONS] <args> ..."
echo "Usage:  ${0} [-[sS]ron] -f /path/to/inputfile"
echo
echo "Options"
echo "   -f     <path>      Path to the inputfile for variables reading"
echo "                      See 'inputfile' examples to learn about"
echo "   -s     SHA         Obtain sha256sum for further verification"
echo "   -S     SIGN        Sign with gpg and obtain sha256sum"
echo "   -r     RSYNC       Use rsync to sync files to diferent path or host"
echo "   -n     NOTIFY      Use sendxmpp to notify"
echo "   -o     SILENT      No output will be shown, script runs silent"
echo "   -h     HELP        Print this help"
echo
exit "${?}"
}

# spinner
spinner () {
  while kill -0 "${PID}" 2> /dev/random ; do
    for i in '-' '/' '|' '\'; do
      printf "\\b%s" "${i}";
      sleep 0.2;
    done;
  done
}

# database dump (and gzip)
dump_sql () {
  case "${ENGINE}" in
    mysql)
      mysqldump -u "${USER}" --password="${PASSWORD}" \
        "${DATABASE}" ${DUMPFLAGS:=--quick} \
        > "${DUMPSQLDUMP}" 2> "${DUMPSQLERR}" & PID="${!}";
      spinner
      ;;
    postgresql)
      pg_dump ${DUMPFLAGS:=-F t} -d "postgresql://${PGURL}" \
        > "${DUMPSQLDUMP}" 2> "${DUMPSQLERR}" & PID="${!}";
      spinner
      ;;
    *)
      printf "%s is not supported engine\\n" "${ENGINE}"; exit 3
      ;;
  esac

  test ! -s "${DUMPSQLDUMP}" \
    && cat "${DUMPSQLERR}" >> "${DBDSSNOUT}" \
    && cat "${DBDSSNOUT}" \
    && exit 2 \
    || printf "%s dumped succefully...\\n" "${DATABASE}" >> "${DBDSSNOUT}"
 
  gzip -f -c "${DUMPSQLDUMP}" > "${TMPDIR}/${OUTPUTFILE}"
  rsync -azh --remove-source-files "${TMPDIR}/${OUTPUTFILE}" "${OUTPUTDIR}" \
    >/dev/random 2> "${DBDRERR}"

  test "${?}" -lt 1 \
    && printf "\\n%s saved into...\\n %s\\n\\n" \
      "${OUTPUTFILE}" "${OUTPUTDIR}" >> "${DBDSSNOUT}" \
    || cat "${DBDRERR}" >> "${DBDSSNOUT}"
}

# shasum
sha_sum () {
  cd "${OUTPUTDIR}" || printf "bad directory" >/dev/random
  sha256sum "${OUTPUTFILE}" > "${OUTPUTFILE}-sha256.sum" 2> "${SHASUMERR}"
  
  test -f "${OUTPUTFILE}-sha256.sum" \
    && printf "obtain sha256sum for %s\\n" "${OUTPUTFILE}" >> "${DBDSSNOUT}" \
    && printf " " & sha256sum -c "${OUTPUTFILE}-sha256.sum" >> "${DBDSSNOUT}" \
    && printf "\\n" >> "${DBDSSNOUT}" \
    || cat "${SHASUMERR}" >> "${DBDSSNOUT}"
}

# sign
sign () {
  SFKEYID="$(gpg -K | grep "${SKEYID}")"

  test ! -z "${SFKEYID}" \
    && gpg_do \
    || printf "gpg failed: invalid key %s" "${SKEYID}" >> "${DBDSSNOUT}"
}

gpg_do () {
  test -z "${PASSPHRASE}" \
    && gpg2 --batch --armor -u "${SKEYID}" -o "${OUTPUTFILE}-gpg.sig" \
      -b "${OUTPUTFILE}" 2> "${GPGDOERR}" \
    || gpg2 --batch --armor --pinentry-mode loopback -u "${SKEYID}" \
      --passphrase "${PASSPHRASE}" -o "${OUTPUTFILE}-gpg.sig" \
      -b "${OUTPUTFILE}" 2> "${GPGDOERR}"

  test -f "${OUTPUTFILE}-gpg.sig" \
    && printf "%s correctly signed using...\\n fingerprint %s\\n\\n" \
      "${OUTPUTFILE}" "${SKEYID}" >> "${DBDSSNOUT}" \
    || cat "${GPGDOERR}" >> "${DBDSSNOUT}"
}

# listfiles
list_files () {
  cd "${OUTPUTDIR}" || printf "bad directory" >/dev/random
  printf "Obtaining list of files...\\n" >> "${DBDSSNOUT}"

  ls -lh "${OUTPUTFILE}"* | awk '{print " " $5 "\t" $9}' >> "${DBDSSNOUT}" \
    || printf "no files where found" >> "${DBDSSNOUT}" 

  printf "\\n" >> "${DBDSSNOUT}"
}

# sync
sync_remote () {
  rsync ${RSYNCFLAGS} -e "${RRSSH}" "${OUTPUTDIR}/" "${REMOTEPATH}" \
    > /dev/random 2> "${SYNCREMOTEERR}"

  test "${?}" -lt 1 \
    && printf "%s succefully synced to\\n %s   using...\\n %s\\n" \
      "${OUTPUTDIR}" "${REMOTEPATH}" "${RSYNCFLAGS}" >> "${DBDSSNOUT}" \
    || cat "${SYNCREMOTEERR}" >> "${DBDSSNOUT}"
}

# notify
notify () {
  NFILE="/tmp/dbdssn/notify.out"
  printf "%s\\n" "${NDATE}" > "${NFILE}"
  printf "%s\\n" "${NMESSAGE}" >> "${NFILE}"

  test "${TTIME}" -gt 0 \
    && printf "dumping took %s seconds\\n" "${TTIME}" >> "${NFILE}" \
    || printf "script executed in less than 1 second\\n" >> "${NFILE}" \

  cat "${DBDSSNOUT}" >> "${NFILE}"

  /usr/local/bin/sendxmpp \
    -u "${NJID}" -p "${NJIDPASS}" -r "${NRESOURCE}" ${NRECIPIENT} ${NFLAGS} \
    < "${NFILE}" 2> "${NOTIFYERR}"

  test "${?}" -lt 1 \
    && printf "Notification succefully sended to\\n %s  from  %s\\n" \
      "${NRECIPIENT}" "${NJID}" \
    || cat "${NOTIFYERR}"
}

# read options from commandline
test "${#}" -lt 1 && usage "${@}"

while getopts "f:sSrnoh" opt; do
  case "${opt}" in
    f) INPUTFILE="${OPTARG}" ;;
    s) SHADO="notnull" ;;
    S) SHADO="notnull" ; SIGN="notnull" ;;
    r) SYNCREMOTE="notnull" ;;
    n) NOTIFY="notnull" ;;
    o) SILENT="notnull" ;;
    \?) printf "Invalid: -%s" "${OPTARG}" 1>&2; exit 3 ;;
    :) printf "Invalid: %s requires an argument" "${OPTARG}" 1>&2 ; exit 3 ;;
    h) usage "${@}" ;;
  esac
done
shift "$((OPTIND -1))"

# do the usual checks
#test -d /tmp/dbdssn || mkdir -p /tmp/dbdssn
#rm -rf /tmp/dbdssn/*

# read variables from inputfile
TMPDIR="$(mktemp -d)"
INPUT="$(mktemp -p "${TMPDIR}")"

sed '/^#/ d; s/;[ \t].*//g; s/;$//g; s/[ \t]*$//g' "${INPUTFILE}" > "${INPUT}"

ENGINE="$(sed '/ENGINE=/ !d; s/ENGINE=//' "${INPUT}")"
USER="$(sed '/USER=/ !d; s/USER=//' "${INPUT}")"
PASSWORD="$(sed '/PASSWORD=/ !d; s/PASSWORD=//' ${INPUT})"
DATABASE="$(sed '/DATABASE=/ !d; s/DATABASE=//' "${INPUT}")"
DUMPFLAGS="$(sed '/DUMPFLAGS=/ !d; s/DUMPFLAGS=//' "${INPUT}")"
HOST="$(sed '/HOST=/ !d; s/HOST=//' "${INPUT}")"
PGDUMPPORT="$(sed '/PGDUMPPORT=/ !d; s/PGDUMPPORT=//' "${INPUT}")"
OUTPUTDIR="$(sed '/OUTPUTDIR=/ !d; s/OUTPUTDIR=//' "${INPUT}")"
SKEYID="$(sed '/SKEYID=/ !d; s/SKEYID=//' "${INPUT}")"
RSYNCFLAGS="$(sed '/RSYNCFLAGS=/ !d; s/RSYNCFLAGS=//' "${INPUT}")"
RRSSH="$(sed '/RRSSH=/ !d; s/RRSSH=//' "${INPUT}")"
REMOTEPATH="$(sed '/REMOTEPATH=/ !d; s/REMOTEPATH=//' "${INPUT}")"
NJID="$(sed '/NJID=/ !d; s/NJID=//' "${INPUT}")"
NJIDPASS="$(sed '/NJIDPASS=/ !d; s/NJIDPASS=//' "${INPUT}")"
NRESOURCE="$(sed '/NRESOURCE=/ !d; s/NRESOURCE=//' "${INPUT}")"
NFLAGS="$(sed '/NFLAGS=/ !d; s/NFLAGS=//' "${INPUT}")"
NRECIPIENT="$(sed '/NRECIPIENT=/ !d; s/NRECIPIENT=//' "${INPUT}")"
NMESSAGE="$(sed '/NMESSAGE=/ !d; s/NMESSAGE=//' "${INPUT}")"

# general purpose variables
DATE="$(date "+%Y%m%d_%H%M%S")"
NDATE="$(date "+%d-%m %H:%M")"
HOSTNAME="$(hostname)"
DBDSSNOUT="$(mktemp -p "${TMPDIR}")"
DUMPSQLDUMP="$(mktemp -p "${TMPDIR}")"
DUMPSQLERR="$(mktemp -p "${TMPDIR}")"
DBDRERR="$(mktemp -p "${TMPDIR}")"
SHASUMERR="$(mktemp -p "${TMPDIR}")"
GPGDOERR="$(mktemp -p "${TMPDIR}")"
SYNCREMOTEERR="$(mktemp -p "${TMPDIR}")"
NOTIFYERR="$(mktemp -p "${TMPDIR}")"
PGURL="${USER}:${PASSWORD}@${HOST:=localhost}:${PGDUMPPORT:=5432}/${DATABASE}"
OUTPUTFILE="${DATABASE}-${DATE}.dump.gz"

# variables default value
test -d "${OUTPUTDIR}" || mkdir -p "${OUTPUTDIR}"
: "${OUTPUTDIR:=${PWD}}"
: "${ENGINE:=postgresql}"
: "${RSYNCFLAGS:=-avzh --force}"
: "${NRESOURCE:=dbdssn}"
: "${NFLAGS:=-t}"
: "${NMESSAGE:=dbdssn executed in ${HOSTNAME} by ${USER}}"

# start an empty stdout file
printf "\\n" > "${DBDSSNOUT}"

# dump
dump_sql

# sign, list and sync
test ! -z "${SHADO}" && sha_sum
test ! -z "${SIGN}" && sign
list_files
test ! -z "${SYNCREMOTE}" && sync_remote

test ! -z "${SILENT}" && printf "silent" > /dev/random || cat "${DBDSSNOUT}"

# calculate exec time
ETIME="$(date +%s)"
TTIME="$(( (ETIME-ITIME) ))"

# notify
test ! -z "${NOTIFY}" && notify

# clean and exit
rm -rf "${TMPDIR}"
exit 0