forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv-backup-user-restic
More file actions
executable file
·117 lines (100 loc) · 4.09 KB
/
v-backup-user-restic
File metadata and controls
executable file
·117 lines (100 loc) · 4.09 KB
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
#!/bin/bash
# info: backup system user with all its objects to restic backup
# options: USER NOTIFY
#
# example: v-backup-user-restic admin yes
#
# Backup user with all its objects to restic backup. If the repo doesn't exists a new one will be created.
#
#----------------------------------------------------------#
# Variables & Functions #
#----------------------------------------------------------#
# Argument definition
user=$1
notify=${2-no}
# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# shellcheck source=/usr/local/hestia/func/domain.sh
source $HESTIA/func/domain.sh
# shellcheck source=/usr/local/hestia/func/db.sh
source $HESTIA/func/db.sh
# shellcheck source=/usr/local/hestia/func/backup.sh
source $HESTIA/func/backup.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '1' "$#" 'USER'
is_format_valid 'user'
is_system_enabled "$BACKUP_SYSTEM" 'BACKUP_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_incremental_backup_enabled
# Perform verification if read-only mode is enabled
check_hestia_demo_mode
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Set notification email and subject
subj="$user → backup failed"
email=$(grep CONTACT "$HESTIA/data/users/$ROOT_USER/user.conf" | cut -f 2 -d \')
source_conf $HESTIA/conf/restic.conf
if [ ! -f "$USER_DATA/restic.conf" ]; then
password=$(generate_password '' '32')
echo "$password" > $USER_DATA/restic.conf
restic --repo "$REPO$user" --password-file $USER_DATA/restic.conf init
if [ $? -ne 0 ]; then
check_result $E_CONNECT "Unable to create restic repo"
fi
else
# Check if repo exists and is accessible with restic key
restic --repo "$REPO$user" --password-file $USER_DATA/restic.conf --json snapshots > /dev/null
if [ $? -ne 0 ]; then
# Send an email
echo "Unable to open restic backup. It might not exists or key is incorrect" | $SENDMAIL -s "$subj" "$email" "yes"
check_result $E_CONNECT "Unable to access restic repo"
fi
fi
# create backup of the user.conf an database
$BIN/v-backup-user-config $user
export GOMAXPROCS=2
export RESTIC_READ_CONCURRENCY=2
ionice -c2 -n7 nice -n 19 restic --repo "$REPO$user" --limit-upload 10240 --read-concurrency 2 --password-file $USER_DATA/restic.conf backup /home/$user
if [ $? -ne 0 ]; then
echo "Unable to create the backup" | $SENDMAIL -s "$subj" "$email" "yes"
check_result $E_BACKUP "Unable to backup user"
fi
if [[ -n "$SNAPSHOTS" && "$SNAPSHOTS" -ge 0 ]]; then
restic_prune="$restic_prune --keep-last $SNAPSHOTS"
fi
if [[ -n "$KEEP_DAILY" && "$KEEP_DAILY" -ge 0 ]]; then
restic_prune="$restic_prune --keep-daily $KEEP_DAILY"
fi
if [[ -n "$KEEP_WEEKLY" && "$KEEP_WEEKLY" -ge 0 ]]; then
restic_prune="$restic_prune --keep-weekly $KEEP_WEEKLY"
fi
if [[ -n "$KEEP_MONTLY" && "$KEEP_MONTLY" -ge 0 ]]; then
restic_prune="$restic_prune --keep-monthly $KEEP_MONTLY"
fi
if [[ -n "$KEEP_YEARLY" && "$KEEP_YEARLY" -ge 0 ]]; then
restic_prune="$restic_prune --keep-yearly $KEEP_YEARLY"
fi
restic --repo "$REPO$user" --password-file $USER_DATA/restic.conf forget $restic_prune --prune
# Send notification
if [ -e "$BACKUP/$user.log" ] && [ "$notify" = "yes" ]; then
subj="$user → backup has been completed"
email=$(get_user_value '$CONTACT')
cat $BACKUP/$user.log | $SENDMAIL -s "$subj" "$email" "$notify"
$BIN/v-add-user-notification "$user" "Snapshot created successfully" "Snap shot of user successfully created"
fi
# Deleting task from queue
sed -i "/v-backup-user-restic $user /d" $HESTIA/data/queue/backup.pipe
# Logging
$BIN/v-log-action "$user" "Info" "Backup" "Backup created."
$BIN/v-log-action "system" "Info" "Backup" "Backup created (User: $user)."
log_event "$OK" "$ARGUMENTS"
exit