|
@@ -0,0 +1,127 @@
|
|
|
+#!/bin/bash
|
|
|
+#/###################################################################\
|
|
|
+#| Make backup of castanedo.es server compressed with 7Z and |
|
|
|
+#| encrypted with $pass7z password. |
|
|
|
+#| Help: |
|
|
|
+#| -$mysqluser: admin user for MySQL. |
|
|
|
+#| -$mysqlpass: admin password form MySQL (Caution: check read |
|
|
|
+#| permissions for this file, password save in plain text) |
|
|
|
+#| -$pass7z: password used for encrypting 7Z archive. |
|
|
|
+#| -$backupDir: directory where 7Z is saved, normally is a folder |
|
|
|
+#| served for a web server (with BasicAuth for more security). |
|
|
|
+#| -$backupName: 7Z archive name. Prefix-Date-6 alphanumeric |
|
|
|
+#| random digits.7z |
|
|
|
+#| -$deleteDays: remove 7Z archives older than this days. |
|
|
|
+#| |
|
|
|
+#| Guzmán Castanedo (guzman@castanedo.es) |
|
|
|
+#| March 2017 |
|
|
|
+#| Licence: GPL v3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
|
|
|
+#\###################################################################/
|
|
|
+
|
|
|
+#Check things (root, 7z, mysql, ...)
|
|
|
+startTime=$(date +"%s")
|
|
|
+if [ $(whoami) != "root" ]; then
|
|
|
+ printf "ERROR:\tTienes que ser root :O\n"
|
|
|
+ exit 1
|
|
|
+fi
|
|
|
+if [ ! -x /usr/bin/7z ]; then
|
|
|
+ printf "ERROR:\t7z Not Installed :O\n"
|
|
|
+ exit 1
|
|
|
+fi
|
|
|
+if [ ! -x /usr/bin/mysql ]; then
|
|
|
+ printf "ERROR:\tMySQL Not Installed :O\n"
|
|
|
+ exit 1
|
|
|
+fi
|
|
|
+if [ ! -x /usr/bin/mysqldump ]; then
|
|
|
+ printf "ERROR:\tMySQL Not Installed :O\n"
|
|
|
+ exit 1
|
|
|
+fi
|
|
|
+
|
|
|
+#Data
|
|
|
+mysqluser="root"
|
|
|
+mysqlpass="mysqlpasswd"
|
|
|
+pass7z="7zencryptionpasswd"
|
|
|
+backupDir=/usr/share/nginx/backup
|
|
|
+backupName=backup-castanedo.es-$(date +"%Y-%m-%d")-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1).7z
|
|
|
+backupOutput=$backupDir/$backupName
|
|
|
+deleteDays="+15"
|
|
|
+
|
|
|
+#Destination file
|
|
|
+printf "Archivo Backup:\t%s\n" $backupOutput
|
|
|
+
|
|
|
+#Copy webpages code (except backup and main/public)
|
|
|
+printf "Comprimiendo:\t/usr/share/nginx\n"
|
|
|
+#cd /usr/share/nginx
|
|
|
+tempfile=$(mktemp -t exclude-XXX)
|
|
|
+echo "nginx/backup" > $tempfile
|
|
|
+echo "nginx/main/public" >> $tempfile
|
|
|
+7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /usr/share/nginx -x@$tempfile > /dev/null
|
|
|
+if [ $? != 0 ];then
|
|
|
+ printf "WARNING:\tError copiando paginas web (Ejecucion continua).\n"
|
|
|
+fi
|
|
|
+rm $tempfile
|
|
|
+
|
|
|
+#Copy MySQL databases (mysqldump)
|
|
|
+lista=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;")
|
|
|
+#Parse databases expect information_schema, mysql & performance_schema
|
|
|
+for database in $lista; do
|
|
|
+ valid=true
|
|
|
+ for excep in Database information_schema mysql performance_schema; do
|
|
|
+ if [ $database = $excep ]; then
|
|
|
+ valid=false
|
|
|
+ break
|
|
|
+ fi
|
|
|
+ done
|
|
|
+ if [ $valid = true ]; then
|
|
|
+ printf "Comprimiendo MySQL database:\t%s\n" $database.sql
|
|
|
+ mysqldump -u $mysqluser -p$mysqlpass $database | 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput -simysql/$database.sql > /dev/null
|
|
|
+ if [ $? != 0 ];then
|
|
|
+ printf "WARNING:\tError copiando database (%s) (Ejecucion continua).\n" $database
|
|
|
+ fi
|
|
|
+ fi
|
|
|
+done
|
|
|
+
|
|
|
+#Copy nginx configuration (sites-available)
|
|
|
+printf "Comprimiendo:\t/etc/nginx/sites-available\n"
|
|
|
+7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /etc/nginx/sites-available > /dev/null
|
|
|
+if [ $? != 0 ];then
|
|
|
+ printf "WARNING:\tError copiando paginas web (Ejecucion continua).\n"
|
|
|
+fi
|
|
|
+
|
|
|
+#Copy Email (this could be heavy in the future)
|
|
|
+printf "Comprimiendo:\t%s\n" /var/mail
|
|
|
+7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /var/mail > /dev/null
|
|
|
+if [ $? != 0 ];then
|
|
|
+ printf "WARNING:\tError copiando emails (Ejecucion continua).\n"
|
|
|
+fi
|
|
|
+
|
|
|
+#Copy Certificates (LetsEncrypt)
|
|
|
+printf "Comprimiendo:\t%s\n" /etc/letsencrypt
|
|
|
+7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /etc/letsencrypt > /dev/null
|
|
|
+if [ $? != 0 ];then
|
|
|
+ printf "WARNING:\tError copiando certificados (Ejecucion continua).\n"
|
|
|
+fi
|
|
|
+
|
|
|
+#Copy /home
|
|
|
+printf "Comprimiendo:\t%s\n" /home/
|
|
|
+7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /home > /dev/null
|
|
|
+if [ $? != 0 ];then
|
|
|
+ printf "WARNING:\tError copiando carpeta personal (Ejecucion continua).\n"
|
|
|
+fi
|
|
|
+
|
|
|
+#Permissions
|
|
|
+chown www-data:www-data $backupOutput
|
|
|
+chmod o= $backupOutput
|
|
|
+
|
|
|
+#Remove files older than 15 days
|
|
|
+printf "Eliminando backups antiguos (+15 dias)\n"
|
|
|
+find $backupDir -mindepth 1 -mtime $deleteDays -type f -delete
|
|
|
+if [ $? != 0 ];then
|
|
|
+ printf "WARNING:\tError eliminando backup's antiguos (%s dias)\n" $deleteDays
|
|
|
+fi
|
|
|
+
|
|
|
+#End
|
|
|
+finalTime=$(date +"%s")
|
|
|
+echo "------------------------------------------------"
|
|
|
+printf "Backup completado con exito en %s segundos :)\n" $((finalTime-startTime))
|
|
|
+echo "------------------------------------------------"
|