Explorar el Código

Add backup-server and download-backup code

Add backup-server and download-backup code
Guzmán Castanedo Villalba hace 6 años
padre
commit
f795c2f4f7
Se han modificado 2 ficheros con 176 adiciones y 0 borrados
  1. 127 0
      backup-server
  2. 49 0
      download-backup

+ 127 - 0
backup-server

@@ -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 "------------------------------------------------"

+ 49 - 0
download-backup

@@ -0,0 +1,49 @@
+#!/bin/bash
+#/###################################################################\
+#| Download backup's from backup.castanedo.es                        |
+#| Download all files in a http/https repository with Auth Basic     |
+#| authentication.                                                   |
+#| Help:                                                             |
+#|   -$scheme: protocol (http, https, ftp, ...)                      |
+#|   -$url: url without $scheme of the repository.                   |
+#|    Ex: backup.example.com/some/where                              |
+#|   -$httpuser: user for Auth Basic Auth.                           |
+#|   -$httppass: password for Auth Basic Auth.                       |
+#|   -$backupDir: directory to save repository files.                |
+#|                                                                   |
+#| Guzmán Castanedo (guzman@castanedo.es)                            |
+#| March 2017                                                        |
+#| Licence: GPL v3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
+#\###################################################################/
+
+scheme="https://"
+url="backup.castanedo.es"
+httpuser="authbasicuser"
+httppass="authbasicpasswd"
+backupDir=/home/$USER/Documentos/backup
+
+#Check directory and remove index.html (or didn't upgrade)
+if [ ! -d $backupDir/$url ];then
+	mkdir -p $backupDir/$url
+	if [ $? != 0 ];then
+		printf "ERROR: Imposible crear %s\n" $backupDir/$url
+		exit 1
+	fi
+fi
+if [ -f $backupDir/$url/index.html ];then
+	rm $backupDir/$url/index.html
+	if [ $? != 0 ];then
+		printf "ERROR: Imposible borrar %s\n" $backupDir/$url/index.html
+		exit 1
+	fi
+fi
+
+#Download with wget
+printf "Destino backups:\t%s\n" $backupDir
+printf "Sincronizando backup's desde %s\n" $scheme$url
+wget -q --tries=3 -r -c --user=$httpuser --password=$httppass --directory-prefix=$backupDir $scheme$url
+if [ $? != 0 ];then
+	printf "ERROR: Se han producido errores en la sincronizacion\n"
+	exit 1
+fi
+printf "Descarga correcta :)\n"