#!/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 [ $(id -u) -ne 0 ]; then printf "ERROR:\tNeed to be root :O\n" exit 1 fi if [ ! -x "$(which 7z)" ]; then printf "ERROR:\t7z Not Installed :O\n" exit 1 fi if [ ! -x "$(which mysql)" ]; then printf "ERROR:\tMySQL Not Installed :O\n" exit 1 fi if [ ! -x "$(which mysqldump)" ]; then printf "ERROR:\tMySQL Not Installed :O\n" exit 1 fi #Data mysqluser="root" mysqlpass="mysqlpasswd" pass7z="7zencryptionpasswd" user="www-data" group="www-data" backupDir=/var/www/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/var/www\n" #cd /usr/share/nginx tempfile=$(mktemp -t exclude-XXX) echo "www/backup" > $tempfile echo "www/main/public" >> $tempfile 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /var/www -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 & performance_schema for database in $lista; do valid=true for excep in Database information_schema 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/\n" 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /etc/nginx > /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 tar -c -zf - /var/mail | 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput -simail.tar.gz > /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 #Copy GOGS printf "Comprimiendo:\t%s\n" /opt/gogs 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /opt/gogs > /dev/null if [ $? != 0 ];then printf "WARNING:\tError copiando GOGS (Ejecucion continua).\n" fi #Permissions chown $user:$group $backupOutput chmod 640 $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 "------------------------------------------------"