-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackup.sh
151 lines (112 loc) · 4.03 KB
/
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
###############
###Variables###
###############
#backup of the WWW folder
WWWBACKUPDIR="/backup/WWW/"
#backup of the Let's Encrypt folder
LEBACKUPDIR="/backup/LE/"
#Databases (MariaDB / MySQL)
DBBACKUPDIR="/backup/DBs/"
DBUSER="root"
DBPASSWORD="rootpassword"
#External FTP storage
ROOTFOLDER="/backup/*"
FTPSERVER="server"
FTPUSER="user"
FTPPASS="pass"
#Archives password and folder
ARCHIVEPASSWORD="password1"
ARCHIVEFOLDER="/home/someuser/"
#GPG options
GPGPASSPHRASE="password2"
GPGALGO="twofish"
GPGDIGEST="SHA512"
#Storing old archive name
days=30
###################################################
### !!! Don't edit anything above this line !!! ###
###################################################
#Storing old archive name
datediff=`date -d "today - "$days" days" +%Y%m%d`.full.7z.gpg
datediffsha1=`date -d "today - "$days" days" +%Y%m%d`.full.7z.sha512
datediffsha2=`date -d "today - "$days" days" +%Y%m%d`.full.7z.gpg.sha512
###################
###Creating dirs###
###################
mkdir -p $WWWBACKUPDIR
mkdir -p $LEBACKUPDIR
mkdir -p $DBBACKUPDIR
##############################
###backup of the WWW folder###
##############################
#Go to working dir
cd $WWWBACKUPDIR
#Create a backup of all the /var/www/ dir
tar -czf `date +%Y%m%d`.www.tar.gz /var/www/
#############################
###backup of the LE folder###
#############################
#Go to working dir
cd $LEBACKUPDIR
#Create a backup of all the possible Let's Encrypt dir
if [ -d "/etc/letsencrypt/" ]; then
tar -czf `date +%Y%m%d`.le.etc.tar.gz /etc/letsencrypt/
fi
if [ -d "/opt/letsencrypt/" ]; then
tar -czf `date +%Y%m%d`.le.opt.tar.gz /opt/letsencrypt/
fi
#############################
###backup of the databases###
#############################
#Go to working dir
cd $DBBACKUPDIR
#Extract all databases
databases=`mysql -u $DBUSER -p$DBPASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
#Backup all databases
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != _* ]] ; then
echo "Dumping database: $db"
mysqldump -u $DBUSER -p$DBPASSWORD --databases $db > `date +%Y%m%d`.$db.sql
fi
done
##############################
###Create protected archive###
##############################
#Creating 7zip-aes encrypted archive
mkdir -p /tmp/backupworkingdir/
cd /tmp/backupworkingdir/
7za a -y -tzip -p$ARCHIVEPASSWORD -mem=AES256 `date +%Y%m%d`.full.7z $ROOTFOLDER
sha512sum `date +%Y%m%d`.full.7z > `date +%Y%m%d`.full.7z.sha512
#Creating gpg-twofish encrypted file
if [ -f "/usr/bin/gpg2" ]; then
gpg2 -c --passphrase $GPGPASSPHRASE --batch --no-tty --yes --cipher-algo $GPGALGO --s2k-digest-algo $GPGDIGEST `date +%Y%m%d`.full.7z
else
gpg -c --passphrase $GPGPASSPHRASE --batch --no-tty --yes --cipher-algo $GPGALGO --s2k-digest-algo $GPGDIGEST `date +%Y%m%d`.full.7z
fi
sha512sum `date +%Y%m%d`.full.7z.gpg > `date +%Y%m%d`.full.7z.gpg.sha512
#move to archive folder for sending
mv `date +%Y%m%d`.full.7z.gpg $ARCHIVEFOLDER
mv `date +%Y%m%d`.full.7z.sha512 $ARCHIVEFOLDER
mv `date +%Y%m%d`.full.7z.gpg.sha512 $ARCHIVEFOLDER
##################################
###Send to external FTP storage###
##################################
cd $ARCHIVEFOLDER
#Sending archive and SHA512sums to FTP storage
curl -T $ARCHIVEFOLDER`date +%Y%m%d`.full.7z.gpg.sha512 ftp://$FTPSERVER --user $FTPUSER:$FTPPASS
curl -T $ARCHIVEFOLDER`date +%Y%m%d`.full.7z.sha512 ftp://$FTPSERVER --user $FTPUSER:$FTPPASS
curl -T $ARCHIVEFOLDER`date +%Y%m%d`.full.7z.gpg ftp://$FTPSERVER --user $FTPUSER:$FTPPASS
#Purge old files
curl --quote "-dele $datediff" ftp://$FTPSERVER --user $FTPUSER:$FTPPASS
curl --quote "-dele $datediffsha1" ftp://$FTPSERVER --user $FTPUSER:$FTPPASS
curl --quote "-dele $datediffsha2" ftp://$FTPSERVER --user $FTPUSER:$FTPPASS
#Delete local files
rm -f `date +%Y%m%d`.full.*
#####################################
###Deleting all local backup files###
#####################################
rm -rf $WWWBACKUPDIR
rm -rf $LEBACKUPDIR
rm -rf $DBBACKUPDIR
rm -rf /tmp/backupworkingdir/