-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysbackup.sh
225 lines (202 loc) · 6.95 KB
/
sysbackup.sh
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
#!/usr/bin/env bash
#
# Copyright (c) 2020 Garri Djavadyan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
TARGETS="/ /home /usr /var" # Filesystems to backup
EXCLUDE[1]="/mnt" # First exclude expression. See tar(1) for details.
BACKUP_DIR="/backup/server1" # Full path to directory contining system and mysql subdirs
BACKUP_DST="local" # Backup destination (local, remote)
COMPRESS_TYPE="gzip" # Valid values are gzip, bzip2, xz and gpg (for encryption)
# E-mail reports configuration
MAIL_CMD="/usr/bin/mail" # Full path to mail process
CONTACT="[email protected]" # E-mail address to send notification
# Remote backup configuration
REMOTE_USER="sysbackup" # Remote SSH user
REMOTE_HOST="192.168.1.1" # Remote host IP address or hostname
REMOTE_PORT="22" # SSH port used to connect to remote backup service
# GPG options
GPG_RCPT[1]="[email protected]" # First GPG recipient
#GPG_RCPT[2]="[email protected]"
# MySQL backup configuration
DB_OPTS="--single-transaction" # Options for mysqldump
DB_USER="username" # User with backup rights on database
DB_PASS="password" # Password for that user
DB_NAME[1]="first" # First database to backup
DB_NAME[2]="second" # Second, third, ... database, if required
# Postgresql backup config
PG_USER="postgres"
PG_DB_NAME[1]="first"
PG_DB_NAME[2]="second"
# The code below is not intended for direct modification.
# Change it only if you are sure it is needed.
print_usage() {
echo "Usage: $0 [--type=system|mysql|pgsql] [--notify=yes|no] [--dateformat=weekday|monthday|month]"
exit 1
}
# Check local or remote file size in MB
check_size() {
if [ ${BACKUP_DST} = "remote" ]; then
ssh -p ${REMOTE_PORT} ${REMOTE_USER}@${REMOTE_HOST} "du -m ${BACKUP_DIR}/system/${BACKUP_FILE}.${EXT}" | cut -f 1
else
du -m ${BACKUP_DIR}/system/${BACKUP_FILE}.${EXT} | cut -f 1
fi
}
send_email() {
[ -x ${MAIL_CMD} ] || { echo "Mail utility not installed. Skip email sending."; return 1; }
echo -e "Date: $(date)\nBackup Type: ${6}\nHost: ${1}\nFile: ${2}\nSize (MB): ${3}\nDuration (sec): ${4}\nDestination Host: ${5}\n\n---\nArchive created using SysBackup-${VERSION}" | ${MAIL_CMD} -s "SysBackup completed on ${1}" ${CONTACT}
echo "E-mail sent to ${CONTACT}."
}
main() {
if [ -f ${LOCK_FILE} ]; then
echo -e "SysBackup already started. Abort.\nIf you are sure it is not true, please remove file ${LOCK_FILE} manually."
exit 1
else
starttime=$(date +%s)
touch ${LOCK_FILE}
trap "rm -f ${LOCK_FILE}; echo -e '\nOperation Abored!'; exit 1" SIGINT SIGTERM
${BACKUP_TYPE} || { rm -f ${LOCK_FILE}; echo -e "SysBackup failed."; exit 1; }
[[ "${BACKUP_DST}" == "remote" ]] && destination="${REMOTE_HOST}" || destination="localhost"
duration=$(( $(date +%s) - $starttime ))
if [[ "${NOTIFY}" == "yes" ]]; then
if [[ "${BACKUP_TYPE}" == "system" ]]; then
send_email $(hostname) ${BACKUP_FILE}.${EXT} $(check_size) ${duration} ${destination} ${BACKUP_TYPE}
else
send_email $(hostname) unknown unknown ${duration} ${destination} ${BACKUP_TYPE}
fi
fi
rm -f ${LOCK_FILE}
echo "SysBackup completed without errors."
exit 0
fi
}
system() {
[ -n "$(which tar)" ] || { echo "Tar archiver is not installed."; return 1; }
if [ ${BACKUP_DST} = "remote" ]; then
tar -cvf - --one-file-system --xattrs ${EXCLUDE[@]} ${TARGETS} | ${COMPRESSOR} | \
ssh -p ${REMOTE_PORT} ${REMOTE_USER}@${REMOTE_HOST} "cat > ${BACKUP_DIR}/system/${BACKUP_FILE}.${EXT}"
else
tar -cvf - --one-file-system --xattrs ${EXCLUDE[@]} ${TARGETS} | \
${COMPRESSOR} > ${BACKUP_DIR}/system/${BACKUP_FILE}.${EXT}
fi
}
mysql() {
[ -n "$(which mysqldump)" ] || { echo "Mysqldump is not installed."; return 1; }
if [ ${BACKUP_DST} = "remote" ]; then
for i in ${DB_NAME[@]}; do
mysqldump ${DB_OPTS} -u ${DB_USER} -p${DB_PASS} ${i} | ${COMPRESSOR} | \
ssh -p ${REMOTE_PORT} ${REMOTE_USER}@${REMOTE_HOST} "cat > ${BACKUP_DIR}/mysql/${i}-${DATEFORMAT}.sql.${EXT}"
done
else
for i in ${DB_NAME[@]}; do
mysqldump ${DB_OPTS} -u ${DB_USER} -p${DB_PASS} ${i} | ${COMPRESSOR} > ${BACKUP_DIR}/mysql/${i}-${DATEFORMAT}.sql.${EXT}
done
fi
}
pgsql() {
[ -n "$(which pg_dump)" ] || { echo "pg_dump is not installed."; return 1; }
if [ ${BACKUP_DST} = "remote" ]; then
for i in ${PG_DB_NAME[@]}; do
sudo -iu ${PG_USER} pg_dump $i | ${COMPRESSOR} | \
ssh -p ${REMOTE_PORT} ${REMOTE_USER}@${REMOTE_HOST} "cat > ${BACKUP_DIR}/pgsql/${i}-${DATEFORMAT}.sql.${EXT}"
done
else
for i in ${DB_NAME[@]}; do
sudo -iu ${PG_USER} pg_dump $i | ${COMPRESSOR} > ${BACKUP_DIR}/pgsql/${i}-${DATEFORMAT}.sql.${EXT}
done
fi
}
# Parse CLI options
for CMD_ARG; do
case ${CMD_ARG} in
--type=system)
BACKUP_TYPE="system"
;;
--type=mysql)
BACKUP_TYPE="mysql"
;;
--type=pgsql)
BACKUP_TYPE="pgsql"
;;
--type=*)
echo "Specified backup type is not supported. Using default 'system'."
;;
--notify=yes)
NOTIFY="yes"
;;
--notify=no)
NOTIFY="no"
;;
--dateformat=weekday)
DATEFORMAT=$(date +%A)
;;
--dateformat=monthday)
DATEFORMAT=$(date +%d)
;;
--dateformat=month)
DATEFORMAT=$(date +%B)
;;
--dateformat=*)
echo "Specified date format is not supported. Using default 'monthday'."
;;
*)
print_usage
;;
esac
done
# Set defaults
VERSION="0.73"
HOME="/root/"
PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
LOCK_FILE=/tmp/sysbackup.lock
BACKUP_TYPE=${BACKUP_TYPE:-"system"}
NOTIFY=${NOTIFY:-"no"}
DATEFORMAT=${DATEFORMAT:-$(date +%d)}
BACKUP_FILE="sysbackup-${DATEFORMAT}.tar"
# Form a string of GPG recepients
if [[ ${#GPG_RCPT[@]} -gt 0 ]]; then
for (( i=1; i<=${#GPG_RCPT[@]}; i++)); do
GPG_RCPT[$i]="-r ${GPG_RCPT[$i]}"
done
fi
# Adapt command and file extension for compression type
case ${COMPRESS_TYPE} in
gpg)
COMPRESSOR="gpg ${GPG_RCPT[@]} -e"
EXT="gpg"
;;
bzip2)
COMPRESSOR="bzip2"
EXT="bz2"
;;
xz)
COMPRESSOR="xz"
EXT="xz"
;;
gzip)
COMPRESSOR="gzip"
EXT="gz"
;;
*)
echo "Specified compression is not supported. Valid types are: gzip, bzip2, xz and gpg."
exit 1
esac
# Aggregate --exclude expressions
if [[ ${#EXCLUDE[@]} -gt 0 ]]; then
for (( i=1; i<=${#EXCLUDE[@]}; i++)); do
EXCLUDE[$i]="--exclude=${EXCLUDE[$i]}"
done
fi
# Call main function
main