-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplesk-backup
executable file
·93 lines (68 loc) · 3.28 KB
/
plesk-backup
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
#!/bin/bash
# Script Name:plesk-backup
# Author: Matt McKinnon
# Date: 04 May 2018
# Description:
# This script will backup your plesk hosting files.
# Send an email report of plesk hosting files that have been backed up.
# Rotate backups for 7 days
#
MAIL="[email protected]"
MAILTO="[email protected]"
MAILFROM="[email protected]"
THISSERVER=$(hostname -f)
SMTP="mail.comprofix.com"
SUBJECT="$(hostname -f) Hosting Files Backup Completed $BAKDATE"
BAKDATE=$(date +%Y%m%d)
BACKUPDIR='/BACKUP'
VHOSTS='/var/www/vhosts/'
LOGFOLDER=/var/log/
LOGFILE=$LOGFOLDER/backuplog-`date +%d-%m-%Y.log`
rotate_backups() {
find $BACKUPDIR -type f -mtime +1 -exec rm -fr {} \;
}
startlogging() {
echo $DASHES2 >> $LOGFILE
echo "$0 started running at $(date)" >> $LOGFILE
echo $DASHES >> $LOGFILE
}
stoplogging() {
echo $DASHES >> $LOGFILE
echo "$0 finished running at $(date)" >> $LOGFILE >> $LOGFILE
echo $DASHES2 >> $LOGFILE
}
DASHES="---------------------------------------------------------------------------------"
DASHES2="================================================================================="
startlogging
rotate_backups
#Backup website files
# Get domain ID
IDS=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e 'select id from psa.domains, psa.hosting where id = dom_id order by id;')
#Use ID to get domain name and www_root folders and create archive using domain name.
for ID in $IDS; do
DOMAIN_NAME=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e 'select name from psa.domains,psa.hosting where id = '$ID' AND dom_id ='$ID' order by id;')
WWW_ROOT=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e 'select www_root from psa.domains,psa.hosting where id = '$ID' AND dom_id ='$ID' order by id;')
echo "$(date) [MESSAGE] Creating archive of $DOMAIN_NAME" >> $LOGFILE
if [ $DOMAIN_NAME = 'cloud.comprofix.com' ]; then
zip -rq $BACKUPDIR/$DOMAIN_NAME.$BAKDATE.zip $WWW_ROOT -x '*data*'
else
zip -rq $BACKUPDIR/$DOMAIN_NAME.$BAKDATE.zip $WWW_ROOT
fi
done
#Backup databases
databases=$(MYSQL_PWD=$(cat /etc/psa/.psa.shadow) mysql -sN -uadmin -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] && [[ "$db" != "apsc" ]] && [[ "$db" != "horde" ]] && [[ "$db" != phpmyadmin_* ]] && [[ "$db" != "psa" ]] && [[ "$db" != "roundcubemail" ]] ; then
echo "$(date) [MESSAGE] Dumping $db to sql file" >> $LOGFILE
mysqldump --force --opt --user=$DBUSER --password=$DBPASS --databases $db > $BACKUPDIR/$db.$BAKDATE.sql
fi
done
#Backup files to offsite location
echo "$(date) [MESSAGE] Copying backup files to offsite location" >> $LOGFILE
#scp -rq -P 2222 $BACKUPDIR/* [email protected]:/data/backup/website
rsync -avz -e "ssh -p 2222 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" /BACKUP/ [email protected]:/data/backup/websites/ >> $LOGFILE
echo "$(date) [MESSAGE] Sending email of backup report" >> $LOGFILE
stoplogging
#sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "$SUBJECT" -m "$(cat /tmp/dbbackup.msg)" -q
#Use below if using POSTFIX
cat $LOGFILE | mail -s "$SUBJECT" "$MAIL"