Skip to content

Commit 2fea4e2

Browse files
committed
Refactor Code
1 parent f9e5ca8 commit 2fea4e2

File tree

1 file changed

+134
-133
lines changed

1 file changed

+134
-133
lines changed

backup.sh

Lines changed: 134 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
#################################################################
88
# Simple backup script for GNU/Linux servers
99
# Main features:
10-
# - Backup custom files and directories
11-
# - Backup MySQL/PostgreSQL/MongoDB databases
12-
# - Copy/SCP/FTP to another server or mounted media
13-
# - Upload to MEGA.nz cloud
14-
# - Send a notification to your email
15-
# - Logging all the activities
16-
# - Encrypts backup file using GPG
10+
# - Backup custom files and directories
11+
# - Backup MySQL/PostgreSQL/MongoDB databases
12+
# - Copy/SCP/FTP to another server or mounted media
13+
# - Upload to MEGA.nz cloud
14+
# - Send a notification to your email
15+
# - Logging all the activities
16+
# - Encrypts backup file using GPG
1717
#
1818
# Edit the configuration and run:
19-
# $ sudo bash backup.sh
19+
# $ sudo bash backup.sh
2020
#
2121
# Please help to simplify and develop new features
2222
# Narbeh - http://narbeh.org - narbeh.aj@gmail.com
@@ -101,9 +101,11 @@ mongo_database=""
101101
mongo_collection=""
102102

103103
# Docker Mariadb/Mysql dump config
104+
# pattern of backup most be like containerID:::user:::password:::database
105+
# This script can backup multiple container with this pattern
104106

105107
docker_mysql_backup="no"
106-
docker_mysql_containers="" #pattern of backup most be like containerID:::user:::password:::database and this script can backup multiple container with this pattern
108+
docker_mysql_containers=""
107109

108110
#################################################################
109111
#################################################################
@@ -114,10 +116,10 @@ docker_mysql_containers="" #pattern of backup most be like containerID:::user:::
114116
################
115117

116118
case $1 in
117-
"--fresh" )
118-
rm /var/backup_lock 2> /dev/null;;
119-
*)
120-
:;;
119+
"--fresh" )
120+
rm /var/backup_lock 2> /dev/null;;
121+
*)
122+
:;;
121123
esac
122124

123125
# Main variables
@@ -130,10 +132,10 @@ date_now=$(date +"%Y-%m-%d %H:%M:%S")
130132
# Checking lock file
131133
test -r /var/backup_lock
132134
if [ $? -eq 0 ];then
133-
echo -e "\n ${color}--- $date_now There is another backup process. \n${nc}"
134-
echo "$date_now There is another backup process." >> $log_file
135-
echo -e "\n ${color}--- $date_now If not, run the script with --fresh argument. \n${nc}"
136-
exit
135+
echo -e "\n ${color}--- $date_now There is another backup process. \n${nc}"
136+
echo "$date_now There is another backup process." >> $log_file
137+
echo -e "\n ${color}--- $date_now If not, run the script with --fresh argument. \n${nc}"
138+
exit
137139
fi
138140

139141
touch /var/backup_lock 2> /dev/null
@@ -147,22 +149,22 @@ sleep 1
147149
# Backing up the files
148150
if [ $backup_files_enable = "yes" ]
149151
then
150-
echo -e "\n ${color}--- $date_now Backing up files \n${nc}"
151-
echo "$date_now Backing up files" >> $log_file
152-
mkdir $backup_path/Backup/$path_date/custom_files | tee -a $log_file
153-
for backup_custom_files in $backup_files
154-
do
155-
echo "--> $backup_custom_files" | tee -a $log_file
156-
cp $backup_files $backup_path/Backup/$path_date/custom_files/ 2>> $log_file
157-
done
158-
echo
152+
echo -e "\n ${color}--- $date_now Backing up files \n${nc}"
153+
echo "$date_now Backing up files" >> $log_file
154+
mkdir $backup_path/Backup/$path_date/custom_files | tee -a $log_file
155+
for backup_custom_files in $backup_files
156+
do
157+
echo "--> $backup_custom_files" | tee -a $log_file
158+
cp $backup_files $backup_path/Backup/$path_date/custom_files/ 2>> $log_file
159+
done
160+
echo
159161
fi
160162

161163
if [ $iptables_backup = "yes" ]
162164
then
163165
echo -e "\n ${color}--- $date_now Backing up iptables rules \n${nc}"
164166
echo "$date_now Backing up iptables rules" >> $log_file
165-
iptables-save >> $backup_path/Backup/$path_date/custom_files/iptables-save
167+
iptables-save >> $backup_path/Backup/$path_date/custom_files/iptables-save
166168
echo
167169
fi
168170

@@ -172,66 +174,66 @@ sleep 1
172174
# Backing up the directories
173175
if [ $backup_dir_enable = "yes" ]
174176
then
175-
echo -e "\n ${color}--- $date_now Backing up directories \n${nc}"
176-
echo "$date_now Backing up directories" >> $log_file
177-
for backup_dirs in $backup_directories
178-
do
177+
echo -e "\n ${color}--- $date_now Backing up directories \n${nc}"
178+
echo "$date_now Backing up directories" >> $log_file
179+
for backup_dirs in $backup_directories
180+
do
179181
echo "--> $backup_dirs" | tee -a $log_file
180-
dir_name=`echo $backup_dirs | cut -d / -f2- | sed 's/\//-/g'`
182+
dir_name=`echo $backup_dirs | cut -d / -f2- | sed 's/\//-/g'`
181183
if [[ -d ${backup_dirs}/.git ]]; then
182184
tar -cjf $backup_path/Backup/$path_date/$dir_name.tar.bz2 -X ${backup_dirs}/.gitignore $backup_dirs/ > /dev/null 2> /dev/null
183185
else
184186
tar -cjf $backup_path/Backup/$path_date/$dir_name.tar.bz2 $backup_dirs/ > /dev/null 2> /dev/null
185187
fi
186-
done
187-
echo
188+
done
189+
echo
188190
fi
189191

190192
sleep 1
191193

192194
# MySQL backup
193195
if [ $mysql_backup = "yes" ]
194196
then
195-
echo -e "\n ${color}--- $date_now MySQL backup enabled, backing up: \n${nc}"
196-
echo "$date_now MySQL backup enabled, backing up" >> $log_file
197-
# Using ionice for MySQL dump
198-
ionice -c 3 mysqldump -u $mysql_user -p$mysql_pass --events --all-databases | gzip -9 > $backup_path/Backup/$path_date/MySQL_Full_Dump_$path_date.sql.gz | tee -a $log_file
199-
if [ $? -eq 0 ]
200-
then
201-
echo -e "\n ${color}--- $date_now MySQL backup completed. \n${nc}"
202-
echo "$date_now Backing up files" >> $log_file
203-
fi
197+
echo -e "\n ${color}--- $date_now MySQL backup enabled, backing up: \n${nc}"
198+
echo "$date_now MySQL backup enabled, backing up" >> $log_file
199+
# Using ionice for MySQL dump
200+
ionice -c 3 mysqldump -u $mysql_user -p$mysql_pass --events --all-databases | gzip -9 > $backup_path/Backup/$path_date/MySQL_Full_Dump_$path_date.sql.gz | tee -a $log_file
201+
if [ $? -eq 0 ]
202+
then
203+
echo -e "\n ${color}--- $date_now MySQL backup completed. \n${nc}"
204+
echo "$date_now Backing up files" >> $log_file
205+
fi
204206
fi
205207

206208
sleep 1
207209

208210
# PostgreSQL backup
209211
if [ $postgres_backup = "yes" ]
210212
then
211-
# Creating ~/.pgpass for PostgreSQL password
212-
# PostgreSQL does not support inline password
213-
# Know better solution? Let me know.
214-
USERNAME=`whoami`
215-
if [ $USERNAME = "root" ]
216-
then
217-
cp /root/.pgpass /root/.pgpass_BACKUP_$(date +"%Y-%m-%d-%H-%M-%S") > /dev/null 2> /dev/null
218-
echo "$postgres_host:$postgres_port:$postgres_database:$postgres_user:$postgres_pass" > /root/.pgpass
219-
chmod 600 /root/.pgpass
220-
else
221-
cp /home/$USERNAME/.pgpass /home/$USERNAME/.pgpass_BACKUP_$(date +"%Y-%m-%d-%H-%M-%S") > /dev/null 2> /dev/null
222-
echo "$postgres_host:$postgres_port:$postgres_database:$postgres_user:$postgres_pass" > /home/$USERNAME/.pgpass
223-
chmod 600 /home/$USERNAME/.pgpass
224-
fi
225-
226-
echo -e "\n ${color}--- $date_now PostgreSQL backup enabled, backing up: \n${nc}"
227-
echo "$date_now PostgreSQL backup enabled, backing up" >> $log_file
228-
# Using ionice for PostgreSQL dump
229-
ionice -c 3 pg_dump -p $postgres_port -h $postgres_host -Fc -U $postgres_user $postgres_database > ${backup_path}/Backup/${path_date}/Postgres_Full_Dump_${path_date}.dump | tee -a $log_file
230-
if [ $? -eq 0 ]
231-
then
232-
echo -e "\n ${color}--- $date_now PostgreSQL backup completed. \n${nc}"
233-
echo "$date_now PostgreSQL backup completed" >> $log_file
234-
fi
213+
# Creating ~/.pgpass for PostgreSQL password
214+
# PostgreSQL does not support inline password
215+
# Know better solution? Let me know.
216+
USERNAME=`whoami`
217+
if [ $USERNAME = "root" ]
218+
then
219+
cp /root/.pgpass /root/.pgpass_BACKUP_$(date +"%Y-%m-%d-%H-%M-%S") > /dev/null 2> /dev/null
220+
echo "$postgres_host:$postgres_port:$postgres_database:$postgres_user:$postgres_pass" > /root/.pgpass
221+
chmod 600 /root/.pgpass
222+
else
223+
cp /home/$USERNAME/.pgpass /home/$USERNAME/.pgpass_BACKUP_$(date +"%Y-%m-%d-%H-%M-%S") > /dev/null 2> /dev/null
224+
echo "$postgres_host:$postgres_port:$postgres_database:$postgres_user:$postgres_pass" > /home/$USERNAME/.pgpass
225+
chmod 600 /home/$USERNAME/.pgpass
226+
fi
227+
228+
echo -e "\n ${color}--- $date_now PostgreSQL backup enabled, backing up: \n${nc}"
229+
echo "$date_now PostgreSQL backup enabled, backing up" >> $log_file
230+
# Using ionice for PostgreSQL dump
231+
ionice -c 3 pg_dump -p $postgres_port -h $postgres_host -Fc -U $postgres_user $postgres_database > ${backup_path}/Backup/${path_date}/Postgres_Full_Dump_${path_date}.dump | tee -a $log_file
232+
if [ $? -eq 0 ]
233+
then
234+
echo -e "\n ${color}--- $date_now PostgreSQL backup completed. \n${nc}"
235+
echo "$date_now PostgreSQL backup completed" >> $log_file
236+
fi
235237
fi
236238

237239
sleep 1
@@ -253,27 +255,26 @@ fi
253255
sleep 1
254256

255257
# Docker Backup
256-
257258
# Mariadb or Mysql backup
258259

259260
if [ $docker_mysql_backup = "yes" ]
260261
then
261-
echo -e "\n ${color}--- $date_now Docker Mariadb/MySQL backup enabled, backing up: \n${nc}"
262-
echo "$date_now Docker MySQL backup enabled, backing up" >> $log_file
263-
for docker_mysql_container in $docker_mysql_containers
264-
do
265-
docker_mysql_container_id=`echo $ocker_mysql_container | awk -F":::" '{print $1}'`
266-
docker_mysql_container_name=`docker ps --filter "id=$docker_mysql_container_id" | awk '{print $11}'`
267-
docker_mysql_user=`echo $ocker_mysql_container | awk -F":::" '{print $2}'`
268-
docker_mysql_pass=`echo $ocker_mysql_container | awk -F":::" '{print $3}'`
269-
docker_mysql_database=`echo $ocker_mysql_container | awk -F":::" '{print $4}'`
270-
docker exec $docker_mysql_container_id /usr/bin/mysqldump -u $docker_mysql_user --password=$docker_mysql_pass $docker_mysql_database | gzip -9 > $backup_path/Backup/$path_date/Docker_MySQL_${docker_mysql_container_name}_Dump_$path_date.sql.gz | tee -a $log_file
271-
if [ $? -eq 0 ]
272-
then
273-
echo -e "\n ${color}--- $date_now Docker Mariadb/MySQL backup completed. \n${nc}"
274-
echo "$date_now Backing up files" >> $log_file
275-
fi
276-
done
262+
echo -e "\n ${color}--- $date_now Docker Mariadb/MySQL backup enabled, backing up: \n${nc}"
263+
echo "$date_now Docker MySQL backup enabled, backing up" >> $log_file
264+
for docker_mysql_container in $docker_mysql_containers
265+
do
266+
docker_mysql_container_id=`echo $ocker_mysql_container | awk -F":::" '{print $1}'`
267+
docker_mysql_container_name=`docker ps --filter "id=$docker_mysql_container_id" | awk '{print $11}'`
268+
docker_mysql_user=`echo $ocker_mysql_container | awk -F":::" '{print $2}'`
269+
docker_mysql_pass=`echo $ocker_mysql_container | awk -F":::" '{print $3}'`
270+
docker_mysql_database=`echo $ocker_mysql_container | awk -F":::" '{print $4}'`
271+
docker exec $docker_mysql_container_id /usr/bin/mysqldump -u $docker_mysql_user --password=$docker_mysql_pass $docker_mysql_database | gzip -9 > $backup_path/Backup/$path_date/Docker_MySQL_${docker_mysql_container_name}_Dump_$path_date.sql.gz | tee -a $log_file
272+
if [ $? -eq 0 ]
273+
then
274+
echo -e "\n ${color}--- $date_now Docker Mariadb/MySQL backup completed. \n${nc}"
275+
echo "$date_now Docker Mariadb/MySQL backup completed" >> $log_file
276+
fi
277+
done
277278
fi
278279

279280

@@ -306,79 +307,79 @@ sleep 1
306307
# Copy to other storage
307308
if [ $external_copy = "yes" ]
308309
then
309-
for cp_paths in $external_storage
310-
do
311-
echo -e "\n ${color}--- $date_now Copy backup archive to $cp_paths: \n${nc}"
312-
echo "$date_now Copy backup archive to $cp_paths" >> $log_file
313-
cp $backup_path/$final_archive $cp_paths/
314-
if [ $? -eq 0 ]
315-
then
316-
echo -e "Copied to $cp_paths. \n"
317-
echo "$date_now Copied to $cp_paths" >> $log_file
318-
else
319-
echo -e " ${color_fail} Copy to $cp_paths failed. ${nc} \n"
320-
echo "$date_now Copy to $cp_paths failed. Please investigate." >> $log_file
321-
fi
322-
done
310+
for cp_paths in $external_storage
311+
do
312+
echo -e "\n ${color}--- $date_now Copy backup archive to $cp_paths: \n${nc}"
313+
echo "$date_now Copy backup archive to $cp_paths" >> $log_file
314+
cp $backup_path/$final_archive $cp_paths/
315+
if [ $? -eq 0 ]
316+
then
317+
echo -e "Copied to $cp_paths. \n"
318+
echo "$date_now Copied to $cp_paths" >> $log_file
319+
else
320+
echo -e " ${color_fail} Copy to $cp_paths failed. ${nc} \n"
321+
echo "$date_now Copy to $cp_paths failed. Please investigate." >> $log_file
322+
fi
323+
done
323324
fi
324325

325326
sleep 1
326327

327328
# SCP to other server
328329
if [ $scp_enable = "yes" ]
329330
then
330-
echo -e "\n ${color}--- $date_now SCP backup archive to $scp_server: \n${nc}"
331-
echo "$date_now SCP backup archive to $scp_server" >> $log_file
332-
scp -P $scp_port $backup_path/$final_archive '$scp_username'@'$scp_server':$scp_path
333-
echo "$date_now SCP done" | tee -a $log_file
331+
echo -e "\n ${color}--- $date_now SCP backup archive to $scp_server: \n${nc}"
332+
echo "$date_now SCP backup archive to $scp_server" >> $log_file
333+
scp -P $scp_port $backup_path/$final_archive '$scp_username'@'$scp_server':$scp_path
334+
echo "$date_now SCP done" | tee -a $log_file
334335
fi
335336

336337
sleep 1
337338

338339
# Upload to FTP server
339340
if [ $ftp_enable = "yes" ]
340341
then
341-
if [ `which curl` ]
342-
then
343-
echo -e "\n ${color}--- $date_now Uploading backup archive to FTP server $ftp_server \n${nc}"
344-
echo "$date_now Uploading backup archive to FTP server $ftp_server" >> $log_file
345-
curl --connect-timeout 30 -S -T $backup_path/$final_archive ftp://$ftp_server/$ftp_path --user $ftp_username:$ftp_password | tee -a $log_file
346-
if [ $? -eq 0 ]
347-
then
348-
echo "$date_now FTP Upload Done" | tee -a $log_file
349-
else
350-
echo -e "\n ${color_fail}--- $date_now FTP upload failed. \n${nc}"
351-
echo "$date_now FTP upload failed. Please investigate." >> $log_file
352-
fi
353-
else
354-
echo -e " ${color_fail}--- $date_now You have been enabled FTP upload. ${nc}"
355-
echo -e " ${color_fail}--- $date_now You need to install curl package. ${nc}"
356-
echo -e " ${color_fail}--- $date_now FTP upload failed. ${nc}"
357-
echo "$date_now FTP upload failed. Install 'curl' package." >> $log_file
358-
fi
342+
if [ `which curl` ]
343+
then
344+
echo -e "\n ${color}--- $date_now Uploading backup archive to FTP server $ftp_server \n${nc}"
345+
echo "$date_now Uploading backup archive to FTP server $ftp_server" >> $log_file
346+
curl --connect-timeout 30 -S -T $backup_path/$final_archive ftp://$ftp_server/$ftp_path --user $ftp_username:$ftp_password | tee -a $log_file
347+
if [ $? -eq 0 ]
348+
then
349+
echo "$date_now FTP Upload Done" | tee -a $log_file
350+
else
351+
echo -e "\n ${color_fail}--- $date_now FTP upload failed. \n${nc}"
352+
echo "$date_now FTP upload failed. Please investigate." >> $log_file
353+
fi
354+
else
355+
echo -e " ${color_fail}--- $date_now You have been enabled FTP upload. ${nc}"
356+
echo -e " ${color_fail}--- $date_now You need to install curl package. ${nc}"
357+
echo -e " ${color_fail}--- $date_now FTP upload failed. ${nc}"
358+
echo "$date_now FTP upload failed. Install 'curl' package." >> $log_file
359+
fi
359360
fi
360361

361362
# Upload archive file to MEGA.nz
362363
if [ $mega_enable = "yes" ]
363364
then
364-
if [ `which megaput` ]
365-
then
366-
echo -e "\n ${color}--- $date_now Uploading backup archive to MEGA.nz \n${nc}"
367-
echo "$date_now Uploading backup archive to MEGA.nz" >> $log_file
368-
megaput --reload --path $mega_path -u $mega_email -p $mega_pass $backup_path/$final_archive
369-
echo "$date_now MEGA Upload Done. Path: $mega_path" | tee -a $log_file
370-
else
371-
echo -e " ${color_fail}--- $date_now You have been enabled MEGA upload. ${nc}"
372-
echo -e " ${color_fail}--- $date_now You need to install megatools from http://megatools.megous.com ${nc}"
373-
echo -e " ${color_fail}--- $date_now MEGA upload failed. ${nc}"
374-
echo "$date_now Uploading to MEGA.nz failed. Install 'megatools' from http://megatools.megous.com" >> $log_file
375-
fi
365+
if [ `which megaput` ]
366+
then
367+
echo -e "\n ${color}--- $date_now Uploading backup archive to MEGA.nz \n${nc}"
368+
echo "$date_now Uploading backup archive to MEGA.nz" >> $log_file
369+
megaput --reload --path $mega_path -u $mega_email -p $mega_pass $backup_path/$final_archive
370+
echo "$date_now MEGA Upload Done. Path: $mega_path" | tee -a $log_file
371+
else
372+
echo -e " ${color_fail}--- $date_now You have been enabled MEGA upload. ${nc}"
373+
echo -e " ${color_fail}--- $date_now You need to install megatools from http://megatools.megous.com ${nc}"
374+
echo -e " ${color_fail}--- $date_now MEGA upload failed. ${nc}"
375+
echo "$date_now Uploading to MEGA.nz failed. Install 'megatools' from http://megatools.megous.com" >> $log_file
376+
fi
376377
fi
377378

378379
# Send a simple email notification
379380
if [ $send_email = "yes" ]
380381
then
381-
echo -e "Backup completed $date_now\nBackup path: $backup_path/$final_archive" | mail -s "Backup Result" $email_to >> $log_file 2>&1
382+
echo -e "Backup completed $date_now\nBackup path: $backup_path/$final_archive" | mail -s "Backup Result" $email_to >> $log_file 2>&1
382383
fi
383384

384385
echo -e "\n"

0 commit comments

Comments
 (0)