Explorar el Código

Add sftp protocol to download-backup and repair email in backup-server
2018-01-24

Guzmán Castanedo Villalba hace 6 años
padre
commit
baf868e85a

+ 3 - 1
README.md

@@ -13,4 +13,6 @@ Also it's:
 * Autoremove old backups
 * Put it in a http/https server with Auth Basic Authentication.
 
-The download script (**download-backup**) download all the compressed files in a remote machine, via http/https Auth Basic Aunthentication.
+The download script (**download-backup-http**) download all the compressed files in a remote machine, via http/https Auth Basic Aunthentication.
+
+The download script (**download-backup-sftp**) download all the compressed files in a remote machine, via sftp (SSH File Transfer Protocol).

+ 11 - 8
backup-server

@@ -21,7 +21,7 @@
 #Check things (root, 7z, mysql, ...)
 startTime=$(date +"%s")
 if [ $(id -u) -ne 0 ]; then
-	printf "ERROR:\tTienes que ser root :O\n"
+	printf "ERROR:\tNeed to be root :O\n"
 	exit 1
 fi
 if [ ! -x "$(which 7z)" ]; then
@@ -41,6 +41,8 @@ fi
 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
@@ -63,7 +65,7 @@ rm $tempfile
 
 #Copy MySQL databases (mysqldump)
 lista=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;")
-#Parse databases expect information_schema, mysql & performance_schema
+#Parse databases expect information_schema & performance_schema
 for database in $lista; do
 	valid=true
 	for excep in Database information_schema performance_schema; do
@@ -89,11 +91,12 @@ if [ $? != 0 ];then
 fi
 
 #Copy Email (this could be heavy in the future)
-#printf "Comprimiendo:\t%s\n" /var/spool/mail
-#7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /var/spool/mail > /dev/null
-#if [ $? != 0 ];then
-#	printf "WARNING:\tError copiando emails (Ejecucion continua).\n"
-#fi
+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
@@ -117,7 +120,7 @@ if [ $? != 0 ];then
 fi
 
 #Permissions
-chown www-data:www-data $backupOutput
+chown $user:$group $backupOutput
 chmod 640 $backupOutput
 
 #Remove files older than 15 days

+ 0 - 0
download-backup → download-backup-http


+ 70 - 0
download-backup-sftp

@@ -0,0 +1,70 @@
+#!/bin/bash
+#/###################################################################\
+#| Download backup's from backup.castanedo.es                        |
+#| Download a complete directory in a ssh server (sftp protocol).    |
+#| Help:                                                             |
+#|   -$host: host name or IP of the remote server.                   |
+#|    Ex: backup.example.com                                         |
+#|   -$sftpuser: user for SFTP.                                      |
+#|   -$sftppass: password for SFTP.                                  |
+#|   -$sftpkey: keyfile for SFTP (if is set $sftppass will be the    |
+#|     key password).                                                |
+#|   -$backupDir: directory to save repository files.                |
+#| Warning:                                                          |
+#|    Store passwords in bash scripts is very unsecure.              |
+#|    (Contributions with keyrings are welcome).                     |
+#|                                                                   |
+#| Guzmán Castanedo (guzman@castanedo.es)                            |
+#| January 2018                                                      |
+#| Licence: GPL v3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
+#\###################################################################/
+
+host="backup.castanedo.es"
+sftpuser="sftpuser"
+sftppass="sftppass"
+sftpkey="" #Not set key file -> Normal Password Auth
+#sftpkey=/home/$USER/.ssh/your.key
+remotefile=/var/www/*.html
+backupDir=/home/$USER/Documentos/backup
+
+#Check dependencies
+if [ ! -x $(which ssh) ];then
+	printf "ERROR:\tssh client Not Installed :O"
+	exit 1
+fi
+if [ ! -x $(which sshpass) ];then
+	printf "ERROR:\tsshpass Not Installed :O\n"
+	exit 1
+fi
+
+#Check directory and remove index.html (or didn't upgrade)
+if [ ! -d $backupDir/$host ];then
+	mkdir -p $backupDir/$host
+	if [ $? != 0 ];then
+		printf "ERROR: Imposible crear %s\n" $backupDir/$host
+		exit 1
+	fi
+fi
+
+#Download with sftp
+printf "Destino backups:\t%s\n" $backupDir/$host
+cd $backupDir/$host
+echo "Sincronizando backup's desde sftp://$sftpuser@$host:$remotefile"
+if [ ! -f $sftpkey ] || [ -z $sftpkey ];then
+	#Use Normal Password Auth
+	export SSHPASS=$sftppass
+	sshpass -e sftp -oBatchMode=no -a $sftpuser@$host:$remotefile
+	if [ $? != 0 ];then
+		printf "ERROR:\tSe han producido errores en la sincronizacion\n"
+		exit 1
+	fi
+else
+	#Use Private Key File
+	export SSHPASS=$sftppass
+	sshpass -e sftp -oBatchMode=no -a -i $sftpkey $sftpuser@$host:$remotefile
+	if [ $? != 0 ];then
+		printf "ERROR:\tSe han producido errores en la sincronizacion\n"
+		exit 1
+	fi
+fi
+printf "Descarga correcta :)\n"

+ 9 - 0
scripts/autostart/download-backup-http.desktop

@@ -0,0 +1,9 @@
+[Desktop Entry]
+Type=Application
+Exec=/usr/local/bin/download-backup-http
+X-GNOME-Autostart-enabled=true
+NoDisplay=false
+Hidden=false
+Name=download-backup-http
+Comment=Download Backup Files from https://backup.castanedo.es
+X-GNOME-Autostart-Delay=0

+ 9 - 0
scripts/autostart/download-backup-sftp.desktop

@@ -0,0 +1,9 @@
+[Desktop Entry]
+Type=Application
+Exec=/usr/local/bin/download-backup-sftp
+X-GNOME-Autostart-enabled=true
+NoDisplay=false
+Hidden=false
+Name=download-backup-sftp
+Comment=Download Backup Files from sftp://backup.castanedo.es
+X-GNOME-Autostart-Delay=0

+ 0 - 9
scripts/autostart/download-backup.desktop

@@ -1,9 +0,0 @@
-[Desktop Entry]
-Type=Application
-Exec=/usr/local/bin/download-backup
-X-GNOME-Autostart-enabled=true
-NoDisplay=false
-Hidden=false
-Name=download-backup
-Comment=Download Backup Files from backup.castanedo.es
-X-GNOME-Autostart-Delay=0