-
Notifications
You must be signed in to change notification settings - Fork 15
/
restore-mail.sh
executable file
·100 lines (81 loc) · 2.47 KB
/
restore-mail.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
#!/bin/bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
source $CURRENT_DIR/config.ini
# This script will restore a mail domain from incremental backup.
USAGE="restore-mail.sh 2018-03-25 user domain.com"
# Assign arguments
TIME=$1
USER=$2
DOMAIN=$3
# Set script start time
START_TIME=`date +%s`
# Set user repository
USER_REPO=$REPO_USERS_DIR/$USER
##### Validations #####
if [[ -z $1 || -z $2 || -z $3 ]]; then
echo "!!!!! This script needs at least 3 arguments. Backup date, user name and mail domain."
echo "---"
echo "Usage example:"
echo $USAGE
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/mail/$DOMAIN" ]; then
echo "!!!!! The mail domain $DOMAIN does not exist under user $USER."
echo "---"
echo "User $USER has the following available mail domains:"
ls $HOME_DIR/$USER/mail
echo "---"
echo "Usage example:"
echo $USAGE
exit 1
fi
if [ ! -d "$USER_REPO/data" ]; then
echo "!!!!! User $USER has no backup repository or no backup has been executed yet. Aborting..."
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 mail domain $DOMAIN 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
# Set dir paths
DOMAIN_DIR=$HOME_DIR/$USER/mail/$DOMAIN
BACKUP_DOMAIN_DIR="${DOMAIN_DIR:1}" # Paths inside borg repo are relative
if ! borg list $USER_REPO::$TIME | grep -q $BACKUP_DOMAIN_DIR; then
echo "!!!!! $DOMAIN mail domain is not present in backup archive $TIME. Aborting..."
exit 1
fi
echo "-- Restoring mail domain files from backup $USER_REPO::$TIME to $BACKUP_DOMAIN_DIR"
cd /
rm -fr $BACKUP_DOMAIN_DIR
borg extract --list $USER_REPO::$TIME $BACKUP_DOMAIN_DIR
echo "-- Fixing permissions"
chown -R $USER:mail $DOMAIN_DIR/
echo
echo "$(date +'%F %T') ########## MAIL DOMAIN $DOMAIN 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