forked from ramirojoaquin/vestacp-borg-incremental-backups
-
Notifications
You must be signed in to change notification settings - Fork 1
/
restore-web-rsync.sh
executable file
·136 lines (110 loc) · 3.63 KB
/
restore-web-rsync.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
#!/bin/bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
source $CURRENT_DIR/config.ini
# This script will restore a web domain from incremental backup
USAGE="./restore-web.sh 2018-03-25 username domain.com [database]"
# Assign arguments
TIME=$1
USER=$2
WEB=$3
# Set script start time
START_TIME=`date +%s`
# Temp dir setup
TEMP_DIR=$CURRENT_DIR/tmp
mkdir -p $TEMP_DIR
# Set user repository
USER_REPO=$REPO_USERS_DIR/$USER
USER_DIR="/home/$USER"
##### Validations #####
if [[ -z $1 || -z $2 ]] || [[ $1 != "list" && -z $3 ]]; then
echo "!!!!! This script needs at least 3 arguments. Backup date, user name and web domain. Dadabase is optional"
echo "---"
echo "Usage example:"
echo $USAGE
echo "or"
echo "./restore-web.sh list username domain.com"
exit 1
fi
if [ ! -d "$HOME_DIR/$USER" ]; then
echo "!!!!! User $USER does not exist"
echo "---"
echo "Available users:"
ls $HOME_DIR
echo "---"
echo "Usage example:"
echo $USAGE
exit 1
fi
if [ ! -d "$HOME_DIR/$USER/web/$WEB" ]; then
echo "!!!!! The web domain $WEB does not exist under user $USER."
echo "---"
echo "User $USER has the following available web domains:"
ls $HOME_DIR/$USER/web
echo "---"
echo "Usage example:"
echo $USAGE
exit 1
fi
if ! borg list $USER_REPO | grep -q $TIME; then
echo "!!!!! Backup archive $TIME not found, the following are available:"
borg list $USER_REPO
echo "Usage example:"
echo $USAGE
exit 1
fi
echo "########## BACKUP ARCHIVE $TIME FOUND, PROCEEDING WITH RESTORE ##########"
read -p "Are you sure you want to restore web $WEB owned by $USER with $TIME backup version? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]]
echo
echo "########## PROCESS CANCELED ##########"
exit 1
fi
# монтируем папку с бекапом, если не примонтирована
echo "----- Mount backup dir"
mkdir -p $USER_DIR/backups/$TIME
isMounted=`mount | grep borgfs | grep $USER_DIR/backups/$TIME`
if [ -z "$isMounted" ] ; then
if ! borg mount $USER_REPO::$TIME $USER_DIR/backups/$TIME ; then
echo "Borg mount failed. Try use restore-web.sh"
exit 2
fi
fi
# Set dir paths
WEB_DIR=$HOME_DIR/$USER/web/$WEB/$PUBLIC_HTML_DIR_NAME
BACKUP_WEB_DIR=$USER_DIR/backups/$TIME$HOME_DIR/$USER/web/$WEB/$PUBLIC_HTML_DIR_NAME
echo $BACKUP_WEB_DIR
echo "-- Rsync files start"
rsync -v -a --numeric-ids --delete $BACKUP_WEB_DIR/ $WEB_DIR/
echo "-- Rsync files finish"
echo "-- Fixing permissions"
chown -R $USER:$USER $WEB_DIR/
# Check if database argument is present and proceed with database restore
if [ $4 ]; then
DB=$4
v-list-databases $USER | cut -d " " -f1 | awk '{if(NR>2)print}' | while read DATABASE ; do
if [ "$DB" == "$DATABASE" ]; then
echo "-- Restoring database $DB from backup $USER_REPO::$TIME"
DB_DIR=$USER_DIR/backups/$TIME$HOME_DIR/$USER/$DB_DUMP_DIR_NAME
# Check that the files have been restored correctly
DB_FILE=$DB_DIR/$DB.sql.gz
mv -f $WEB_DIR/.htaccess $WEB_DIR/.htaccess.backup
echo "Deny From All" > $WEB_DIR/.htaccess
$CURRENT_DIR/inc/db-restore.sh $DB $DB_FILE $DB_DIR
mv -f $WEB_DIR/.htaccess.backup $WEB_DIR/.htaccess
else
echo "!!!!! Database $DB not found under selected user. User $USER has the following databases:"
v-list-databases $USER | cut -d " " -f1 | awk '{if(NR>2)print}'
fi
done
fi
echo "----- Umount backup dir"
borg umount $USER_DIR/backups/$TIME
echo
echo "$(date +'%F %T') ########## WEB $WEB OWNED BY $USER RESTORE COMPLETED ##########"
END_TIME=`date +%s`
RUN_TIME=$((END_TIME-START_TIME))
echo "-- Execution time: $(date -u -d @${RUN_TIME} +'%T')"
echo