-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_gen.sh
executable file
·50 lines (41 loc) · 1.27 KB
/
backup_gen.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
#!/usr/bin/env bash
set -euo pipefail
rederror="\033[1;31mERROR:\033[0m"
if [ $EUID -ne 0 ]; then
printf "$rederror This script must be run as root\n"
exit 1
fi
name=""
db=""
while [ -z $name ]; do
read -p " Site name: " name
if [ ! -d "/var/www/$name" ]; then
printf "$rederror The directory /var/www/$name/ does not exist! Please try again.\n"
name=""
fi
done
while [ -z "$db" ]; do
read -p "Database name: " db
if [ -n "$db" ]; then
if [ -z "$(mysql -e "show databases like '$db'")" ]; then
printf "$rederror The database '$db' does not exist! Please try again.\n"
db=""
fi
fi
done
mkdir -p /var/www/scripts/
if [ -f "/var/www/scripts/backup_$name.sh" ]; then
read -p \
"The script /var/www/scripts/backup_$name.sh already exists. Do you wish to overwrite it? (y/N) " \
overwrite
if [ "${overwrite,,}" != "y" ]; then
exit 1
fi
fi
cat backup_template.sh |
sed "s/site_name/$name/g" |
sed "s/db_name/$db/g" > "/var/www/scripts/backup_$name.sh"
chmod +x "/var/www/scripts/backup_$name.sh"
echo "The backup script has been created at /var/www/scripts/backup_$name.sh"
cp restore_template.sh /var/www/scripts/.
chmod -x /var/www/scripts/restore_template.sh