forked from ramirojoaquin/vestacp-borg-incremental-backups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore-archived-user.sh
executable file
·133 lines (109 loc) · 3.71 KB
/
restore-archived-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
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/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 the offline archive.
# If the user does not exist in the system, it will be created.
# If the user already exist, it will be overwrited.
USAGE="restore-user.sh user"
# Assign arguments
USER=$1
# Set script start time
START_TIME=`date +%s`
# Temp dir setup
TEMP_DIR=$CURRENT_DIR/tmp
mkdir -p $TEMP_DIR
# Set dir paths
USER_DIR=$HOME_DIR/$USER
VESTA_USER_DIR=$VESTA_DIR/data/users/$USER
ARCHIVE_USER_DIR=$ARCHIVE_DIR/$USER
ARCHIVE_VESTA_USER_DIR=$ARCHIVE_USER_DIR/vesta/$USER
##### Validations #####
if [[ -z $1 ]]; then
echo "!!!!! This script needs 1 argument: User name"
echo "---"
echo "Usage example:"
echo $USAGE
exit 1
fi
# Check if user archive exist
if [ ! -f "$ARCHIVE_USER_DIR.tar.gz" ]; then
echo "!!!!! User $USER has no offline archive file in $ARCHIVE_DIR. Aborting..."
exit 1
fi
echo "########## OFFLINE ARCHIVE FOR USER $USER FOUND ##########"
# Check if user exist in the system
if [ -d "$VESTA_USER_DIR" ]; then
echo "!!!!! User $USER already exist in the system."
read -p "Are you sure you want to overwrite the user $USER with offline archived version stored in $ARCHIVE_USER_DIR.tar.gz? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]]
echo
echo "########## PROCESS CANCELED ##########"
exit 1
fi
else
# Ask for confirmation
read -p "Are you sure you want to create the user $USER using offline archived version stored in $ARCHIVE_USER_DIR.tar.gz? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]]
echo
echo "########## PROCESS CANCELED ##########"
exit 1
fi
fi
echo
echo "########## Extracting offline archive file $ARCHIVE_USER_DIR.tar.gz ##########"
cd $ARCHIVE_DIR
tar -pxzf $USER.tar.gz
# Archive content validation
if [ ! -d "$ARCHIVE_VESTA_USER_DIR" ]; then
echo "!!!!! User $USER vesta config files are not present in the offline archive. Aborting..."
exit 1
fi
if [ -z "$(ls -A $ARCHIVE_VESTA_USER_DIR)" ]; then
echo "!!!!! Restored Vesta user config dir from offline archive is empty, Aborting..."
exit 1
fi
echo "########## BACKUP OFFLINE ARCHIVE FOR USER $USER FOUND, PROCEEDING WITH RESTORE ##########"
echo "-- Restoring vesta config files for user $USER from $ARCHIVE_VESTA_USER_DIR to $VESTA_USER_DIR"
mkdir -p $VESTA_USER_DIR
rsync -za --delete $ARCHIVE_VESTA_USER_DIR/ $VESTA_USER_DIR/
echo "-- Vesta rebuild user"
v-rebuild-user $USER
# First we remove vesta folder from archive
if [ -d "$ARCHIVE_USER_DIR/vesta" ]; then
rm -rf $ARCHIVE_USER_DIR/vesta
fi
echo "-- Restoring user files from $ARCHIVE_USER_DIR to $USER_DIR"
rsync -za --delete --omit-dir-times $ARCHIVE_USER_DIR/ $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 | cut -d " " -f1 | awk '{if(NR>2)print}' | while read DB ; do
DB_DIR=$HOME_DIR/$USER/$DB_DUMP_DIR_NAME
DB_FILE=$DB_DIR/$DB.sql.gz
# Check if there is a backup for the db
if test -f "$DB_FILE"
then
echo "-- $DB found in offline archive"
$CURRENT_DIR/inc/db-restore.sh $DB $DB_FILE
else
echo "$DB_FILE not found offline archive in $DB_DIR"
fi
done
echo "-- Vesta rebuild user"
v-rebuild-user $USER
echo "----- Cleaning extracted offline archive dir"
if [ -d "$ARCHIVE_USER_DIR" ]; then
rm -rf $ARCHIVE_USER_DIR
fi
echo
echo "$(date +'%F %T') #################### USER $USER RESTORE FROM OFFLINE ARCHIVE $ARCHIVE_USER_DIR.tar.gz COMPLETED ####################"
END_TIME=`date +%s`
RUN_TIME=$((END_TIME-START_TIME))
echo "-- Execution time: $(date -u -d @${RUN_TIME} +'%T')"
echo