Skip to content

Commit 7e25add

Browse files
committed
shellcheck/harden/formatting
1 parent 88a8a4e commit 7e25add

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

fullsysbackup.sh

+31-29
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,41 @@ backuplocation="/home/user/fullsysbackup/"
77

88
# Enter full paths of folders to include in our backup archive. (You should change these)
99
backuppaths=(
10-
/home/user
11-
/root
12-
/etc
13-
/usr/share/coreruleset
14-
/usr/share/grc
15-
/var
10+
/home/user
11+
/root
12+
/etc
13+
/usr/share/coreruleset
14+
/usr/share/grc
15+
/var
1616
/lib/systemd/system
1717
)
1818
###### End Of Configuration ######
1919

2020
# sudo is required
2121
if [[ $UID != 0 ]]; then
22-
printf "\e[1mThis script needs sudo privileges. You will also be promped to enter mysql credentials for each database you export unless you have a .my.cnf file in place. See README.md file for more information.\e[0m\n"
23-
printf "sudo $0 $*"
24-
exit 1
22+
printf "\e[1m%s\e[0m\n" "This script needs sudo privileges. You will also be prompted to enter MySQL credentials for each database you export unless you have a .my.cnf file in place. See README.md file for more information."
23+
printf "%s\n" "sudo $0 $*"
24+
exit 1
2525
fi
2626

2727
printf "%b" "\e[1mChecking for required package: pv\e[0m\n"
2828
sleep 1
2929

3030
FILE=/usr/bin/pv
3131
if [ -f "$FILE" ]; then
32-
printf "%b" "\e[1m$FILE exists.\e[0m\n"
33-
else
34-
printf "%b" "\e[31m$FILE does not exist. You may install it with "sudo apt install pv" or your distros package manager. - Script will now exit.\e[0m\n" && exit 0
32+
printf "%b" "\e[1m$FILE exists.\e[0m\n"
33+
else
34+
printf "%b" "\e[31m$FILE does not exist. You may install it with "sudo apt install pv" or your distros package manager. - Script will now exit.\e[0m\n" && exit 0
3535
fi
3636

3737
printf "%b" "\e[1mChecking for required package: gzip\e[0m\n"
3838
sleep 1
3939

4040
FILE1=/bin/gzip
4141
if [ -f "$FILE1" ]; then
42-
printf "%b" "\e[1m$FILE1 exists.\e[0m\n"
43-
else
44-
printf "%b" "\e[31m$FILE1 does not exist. You may install it with "sudo apt install gzip" or your distros package manager. - Script will now exit.\e[0m\n" && exit 0
42+
printf "%b" "\e[1m$FILE1 exists.\e[0m\n"
43+
else
44+
printf "%b" "\e[31m$FILE1 does not exist. You may install it with "sudo apt install gzip" or your distros package manager. - Script will now exit.\e[0m\n" && exit 0
4545
fi
4646

4747
printf "%b" "\e[1mBacking up to: $backuplocation\e[0m\n"
@@ -51,27 +51,29 @@ mkdir -p $backuplocation
5151

5252
printf "%b" "\e[1mDumping Databases... Your mysql root password is required\e[0m\n"
5353

54-
mysql -N -e 'show databases' -u root -p |grep -v 'mysql\|information\|performance'|
55-
while read -r dbname;
56-
do
57-
if mysqldump --verbose --complete-insert --routines --triggers --single-transaction -u root -p "$dbname" > $backuplocation/"$dbname"-"${today}".sql; [[ $? -eq 0 ]] && gzip --force $backuplocation/"$dbname-${today}".sql; then
58-
printf "%b" "\e[31mDumping Database: \e[1m $dbname \e[32mdone.\e[0m\n"
59-
else
60-
printf "%b" "\e[31mCould not dump \e[1m $dbname \e[31mdatabase\e[0m\n" && exit 0
61-
fi
62-
done
54+
mysql -N -e 'show databases' -u root -p | grep -v 'mysql\|information\|performance' |
55+
while read -r dbname; do
56+
if
57+
mysqldump --verbose --complete-insert --routines --triggers --single-transaction -u root -p "$dbname" >$backuplocation/"$dbname"-"${today}".sql
58+
[[ $? -eq 0 ]] && gzip --force $backuplocation/"$dbname-${today}".sql
59+
then
60+
printf "%b" "\e[31mDumping Database: \e[1m $dbname \e[32mdone.\e[0m\n"
61+
else
62+
printf "%b" "\e[31mCould not dump \e[1m $dbname \e[31mdatabase\e[0m\n" && exit 0
63+
fi
64+
done
6365

6466
sleep 1
65-
printf "%b" "\e[1mBacking up system and user files...\e[0m\n"
67+
printf "%b" "\e[1mBacking up system and user files...\e[0m\n"
6668

6769
# TODO: dialog output
6870
#if (tar -cf - "${backuppaths[@]}" --exclude="$backuplocation" | pv -s $(du -cb "${backuppaths[@]}" | tail -1 | awk '{print $1}') | gzip --force > archive.tar.gz) 2>&1 | dialog --gauge "Backing up your files..." 7 70; then
6971
#
7072

71-
if tar -cf - --exclude="$backuplocation*" "${backuppaths[@]}" | pv -s $(du -cb "${backuppaths[@]}" | tail -1 | awk '{print $1}') | gzip --force > $backuplocation/backup-files-"$today".tar.gz ; then
72-
printf "%b" "\e[31mSystem and user files backup: \e[32mdone.\e[0m\n"
73-
else
74-
printf "%b" "\e[31mError: Could not backup files.\e[0m\n" && exit 0
73+
if tar -cf - --exclude="$backuplocation*" "${backuppaths[@]}" | pv -s $(du -cb "${backuppaths[@]}" | tail -1 | awk '{print $1}') | gzip --force >"$backuplocation"/backup-files-"$today".tar.gz; then
74+
printf "%b" "\e[31mSystem and user files backup: \e[32mdone.\e[0m\n"
75+
else
76+
printf "%b" "\e[31mError: Could not backup files.\e[0m\n" && exit 0
7577
fi
7678

7779
printf "%b" "\e[32mDone!\e[0m"

0 commit comments

Comments
 (0)