forked from MinehubDE/linux-beginner-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
137 lines (110 loc) · 4.02 KB
/
backup.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
#!/bin/bash
printf "\033c"
if [ "$(id -u)" != "0" ]; then
echo -e "\n---------------------------------------------------------------------------------\n"
echo -e "PLEASE EXECUTE THIS SCRIPT AS ROOT OR SUDO!\n"
echo -e "YOU CAN ALWAYS CANCEL THE SCRIPT WITH 'leave'!\n"
echo "sudo ${0}"
echo -e "\n---------------------------------------------------------------------------------"
exit 1
fi
#----------------------------------------------------------
# Source Backup Folder
#----------------------------------------------------------
echo -e "Please enter the source backup folder, which get archived for example '/home/backup'.\n"
read -r SRCDIR
echo ""
while :; do
if [ -d "$SRCDIR" ]; then
break
else
if [ "$SRCDIR" == "l" ] || [ "$SRCDIR" == "exit" ] || [ "$SRCDIR" == "q" ] || [ "$SRCDIR" == "leave" ] || [ "$SRCDIR" == "quit" ]; then
echo "You have cancelled the script. So this script will exit now."
exit 1
else
echo "Error: Not a valid path!"
echo -e "Please enter the source backup folder, which get archived for example '/home/backup'.\n"
read -r SRCDIR
echo ""
fi
fi
done
#----------------------------------------------------------
# Destionation Backup Folder
#----------------------------------------------------------
echo -e "Please enter the destination backup folder, which get archived for example '/home/backup'.\n"
read -r DESDIR
echo ""
while :; do
if ! [ "$DESDIR" = "$SRCDIR" ]; then
if [ -d "$DESDIR" ]; then
break
else
echo "Error: Not a valid path!"
echo -e "Please enter the destination backup folder, which get archived for example '/home/backup'.\n"
read -r DESDIR
echo ""
fi
elif [ "$DESDIR" == "l" ] || [ "$DESDIR" == "exit" ] || [ "$DESDIR" == "q" ] || [ "$DESDIR" == "leave" ] || [ "$DESDIR" == "quit" ]; then
echo "You have cancelled the script. So this script will exit now."
exit 1
else
echo -e "Error: You have the same path!"
echo -e "Please enter the destination backup folder, which get archived for example '/home/backup'.\n"
read -r DESDIR
echo ""
fi
done
#----------------------------------------------------------
# Filename
#----------------------------------------------------------
read -r -p "Please enter the desired name of the backup that comes before the time: " FPART
echo ""
VALIDATE='[^a-zA-Z]'
while :; do
if [[ $FPART =~ $VALIDATE ]]; then
if [ "$FPART" == "l" ] || [ "$FPART" == "exit" ] || [ "$FPART" == "q" ] || [ "$FPART" == "leave" ] || [ "$FPART" == "quit" ]; then
echo "You have cancelled the script. The script will exit now."
exit 1
else
echo "Error: Not a valid filename!"
read -r -p "Please enter the name of the backup that comes before the time: " FPART
echo ""
fi
else
break
fi
done
#----------------------------------------------------------
# Backup time
#----------------------------------------------------------
echo -e "Do you want that the script will be executed automatically?\n"
read -r -p "Please insert 'YES' if you want it, otherwise it would be skipped! " ANSWER
echo ""
if [ "$ANSWER" == "YES" ] || [ "$ANSWER" == "y" ] || [ "$ANSWER" == "Y" ] || [ "$ANSWER" == "yes" ]; then
echo -e "Enter time in hour (0 - 23) when the backup should be daily generated.\n"
read -r CRONTAB
re='^[0-9]+$'
while :; do
if ! [[ $CRONTAB =~ $re ]] ; then
if [ "$CRONTAB" == "l" ] || [ "$CRONTAB" == "exit" ] || [ "$CRONTAB" == "q" ] || [ "$CRONTAB" == "leave" ] || [ "$CRONTAB" == "quit" ]; then
echo "You have cancelled the script. So this script will exit now."
exit 1
else
echo "Error: Not a number!"
echo -e "Enter time in hour (0 - 23) when the backup should be daily generated.\n"
read -r CRONTAB
echo ""
fi
else
( crontab -l | grep -v -F 'tar' ; echo "00 0$CRONTAB * * * /bin/bash tar -cpzf $DESDIR/$FPART-date +%d-%m-%y-%H-%M.tar.gz -P $SRCDIR" ) | crontab -
echo -e "Setup done!"
echo -e "The backup process was completed successfully."
break
fi
done
else
echo "Your answer was \"$ANSWER\" and not YES. Therefore this step will be skipped..."
echo -e "Setup done!"
echo -e "The backup process was completed successfully."
fi