-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcyberpanel-custom-backup.sh
107 lines (92 loc) · 2.71 KB
/
cyberpanel-custom-backup.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
#!/bin/bash
# manual: https://community.cyberpanel.net/t/cyberpanel-command-line-interface/30683
function require_root {
if [[ $(id -u "$(whoami)") -ne 0 ]];then
echo "You are $(whoami)"
echo "Require root"
exit
fi
}
function require_cyberpanel {
if [[ -z $(which cyberpanel) ]];then
echo "Only work for CyberPanel"
exit
fi
}
function require_ncftp {
if [[ -z $(which ncftpput) ]];then
# install ncftp
apt-get install ncftp
fi
}
function init_vars {
BACKUP=/backup
RETENTION=7
BACKUPDIR=$BACKUP/$(date +%F)
}
function init_backupdir {
if [[ ! -d "$BACKUPDIR" ]];then
mkdir -p "$BACKUPDIR"
fi
}
function backup_homedir {
mkdir -p "$BACKUPDIR"/website/
tar -cvzf "$BACKUPDIR"/website/"$WEBSITE".tar.gz /home/"$WEBSITE"
}
function backup_mysql {
mkdir -p "$BACKUPDIR"/mysql/"$WEBSITE"
cyberpanel listDatabasesPretty --databaseWebsite "$WEBSITE"|awk '{print $4}'|sed "s/Database//g"|sort|while read -r SQL;do
if [[ -n "$SQL" ]];then
mysqlcheck -r "$SQL";
mysqldump "$SQL" > "$BACKUPDIR"/mysql/"$WEBSITE"/"$SQL".sql;
fi
done
}
function backup_dnszone {
mkdir -p "$BACKUPDIR"/dnszone/
mysql -Bse "use cyberpanel;select name,type,ttl,content from records;"|grep "$WEBSITE" > "$BACKUPDIR"/dnszone/"$WEBSITE".db
}
function ftp_upload {
if [[ -f /root/.netrc ]];then
FTPUSERNAME=$(awk '/login/ {print $2}' /root/.netrc)
FTPPASSWORD=$(awk '/password/ {print $2}' /root/.netrc)
FTPHOSTNAME=$(awk '/machine/ {print $2}' /root/.netrc)
if [[ -n $FTPUSERNAME ]] && [[ -n $FTPPASSWORD ]] && [[ -n $FTPHOSTNAME ]];then
ncftpput -R -v -u "$FTPUSERNAME" -p "$FTPPASSWORD" "$FTPHOSTNAME" . "$BACKUPDIR"
fi
else
echo "/root/.netrc not found"
echo "echo 'machine [FTP Hostname]' > /root/.netrc"
echo "echo 'login [FTP Username]' >> /root/.netrc"
echo "echo 'password [FTP Password]' >> /root/.netrc"
echo "Whitelist Passive Port Range 49152:65534"
fi
}
function total_backup {
TOTAL_BACKUP=$(find $BACKUP -name "[0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0-9]" -type d|wc -l)
}
function retention_backup {
if [[ "$TOTAL_BACKUP" -gt "$RETENTION" ]];then
TOTALDEL=$(( "$RETENTION" - "$TOTAL_BACKUP" ))
find "$BACKUP" -name "[0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0-9]" -type d|sort|head -n "$TOTALDEL"|while read -r DELBACKUP;do
if [[ -d "$DELBACKUP" ]];then
rm -rf "$DELBACKUP";
fi
done
fi
}
# main program
# requirement check
require_root
require_cyberpanel
require_ncftp
init_vars
init_backupdir
cyberpanel listWebsitesPretty|awk '/Active/ {print $4}'|while read -r WEBSITE;do echo "$WEBSITE";
backup_homedir
backup_mysql
backup_dnszone
done
total_backup
retention_backup
ftp_upload