-
Notifications
You must be signed in to change notification settings - Fork 15
/
restore-user.sh
executable file
·120 lines (97 loc) · 3.27 KB
/
restore-user.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
#!/bin/bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
source $CURRENT_DIR/config.ini
# This script will restore the given user from incremental backup.
USAGE="restore-user.sh 2018-03-25 user"
# Assign arguments
TIME=$1
USER=$2
# Set script start time
START_TIME=`date +%s`
# Set user repository
USER_REPO=$REPO_USERS_DIR/$USER
##### Validations #####
if [[ -z $1 || -z $2 ]]; then
echo "!!!!! This script needs 2 arguments. Backup date and user name"
echo "---"
echo "Usage example:"
echo $USAGE
exit 1
fi
# Check if user repo exist
if [ ! -d "$USER_REPO/data" ]; then
echo "!!!!! User $USER has no backup repository or no backup has been executed yet. Aborting..."
exit 1
fi
# Check if backup archive date exist in user repo
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
# Check if vesta repo exist
if [ ! -d "$REPO_VESTA/data" ]; then
echo "!!!!! Vesta has no backup repository or no backup has been executed yet. Aborting..."
exit 1
fi
# Check if backup archive date exist in vesta repo
if ! borg list $REPO_VESTA | grep -q $TIME; then
echo "!!!!! Backup archive $TIME not found in Vesta repo, the following are available:"
borg list $REPO_VESTA
echo "Usage example:"
echo $USAGE
exit 1
fi
echo "########## BACKUP ARCHIVE $TIME FOR USER $USER FOUND, PROCEEDING WITH RESTORE ##########"
read -p "Are you sure you want to restore user $USER with $TIME backup version? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]]
echo
echo "########## PROCESS CANCELED ##########"
exit 1
fi
# Set dir paths
USER_DIR=$HOME_DIR/$USER
VESTA_USER_DIR=$VESTA_DIR/data/users/$USER
BACKUP_USER_DIR="${USER_DIR:1}"
BACKUP_VESTA_USER_DIR="${VESTA_USER_DIR:1}"
cd /
if ! borg list $REPO_VESTA::$TIME | grep -q $BACKUP_VESTA_USER_DIR; then
echo "!!!!! Vesta user config files for $USER are not present in backup archive $TIME. Aborting..."
exit 1
fi
echo "----- Restoring Vesta user files from backup $REPO_VESTA::$TIME to $VESTA_USER_DIR"
rm -fr $BACKUP_VESTA_USER_DIR
borg extract --list $REPO_VESTA::$TIME $BACKUP_VESTA_USER_DIR
echo "-- Vesta rebuild user"
v-rebuild-user $USER
if ! borg list $USER_REPO::$TIME | grep -q $BACKUP_USER_DIR; then
echo "!!!!! User $USER files are not present in backup archive $TIME. Aborting..."
exit 1
fi
echo "----- Restoring user files from backup $USER_REPO::$TIME"
rm -fr $BACKUP_USER_DIR
borg extract --list $USER_REPO::$TIME $BACKUP_USER_DIR
echo "-- Fixing web permissions"
chown -R $USER:$USER $USER_DIR/web
echo "----- Checking if there are databases to restore"
v-list-databases $USER | grep -w mysql | cut -d " " -f1 | while read DB ; do
echo "-- Restoring $DB"
yes | $CURRENT_DIR/restore-db.sh $TIME $USER $DB
done
v-list-databases $USER | grep -w pgsql | cut -d " " -f1 | while read DB ; do
echo "-- Restoring $DB"
yes | $CURRENT_DIR/restore-db.sh $TIME $USER $DB
done
echo "-- Vesta rebuild user"
v-rebuild-user $USER
echo
echo "$(date +'%F %T') #################### USER $USER RESTORE COMPLETED ####################"
END_TIME=`date +%s`
RUN_TIME=$((END_TIME-START_TIME))
echo "-- Execution time: $(date -u -d @${RUN_TIME} +'%T')"
echo