Explorar el Código

Implement --exclude
Fix bugs in makeTar

Guzmán Castanedo Villalba hace 6 años
padre
commit
4f29645e0c
Se han modificado 2 ficheros con 47 adiciones y 32 borrados
  1. 3 0
      README.md
  2. 44 32
      backup-server

+ 3 - 0
README.md

@@ -63,6 +63,9 @@ Edit `/etc/cron.d/backup-server` to automatic backups as desired.
   * Default: www-data
 *	`--permision-mask number`: octal mask to set accesss permision of the backup
   * Default: 640
+* `-e`, `--exclude file1[,file2,[file3,[...]]]`: files to be excluded of backup
+  * Separate files with commas ","
+  * Default: ""
 
 ###### Compression Options:
 *	`-z`, `--gzip`: compress using gzip (tar.gz or tar.gz.gpg)

+ 44 - 32
backup-server

@@ -56,6 +56,9 @@ function usage {
 	printf "\t\tDefault: %s\n" $group
 	printf "\t--permision-mask number: octal mask to set accesss permision of the backup\n"
 	printf "\t\tDefault: %s\n" $permisionMask
+	printf "\t-e, --exclude file1[,file2,[file3,[...]]]: files to be excluded of backup\n"
+	printf "\t\tSeparate files with commas \",\"\n"
+	printf "\t\tDefault: %s\n" $exclude
 	printf "\n"
 	printf "Compression Options:\n"
 	printf "\t-z, --gzip: compress using gzip (tar.gz or tar.gz.gpg)\n"
@@ -172,20 +175,20 @@ function usage {
 	printf "\t\tDisable SQL backup\n"
 	printf "\t·%s -j -k ABCDEFG --sql-user root --sql-password toor /var/www/backup\n" $(basename $0)
 	printf "\t\tMake a backup in \"/var/www/backup\" compressed with BZip2 and encrypted\n"
-	printf "\t\t(\"backup-'hostname'-2018-03-19-abcdef.tar.bz2.gpg\")\n"
+	printf "\t\t(\"backup-%s-2018-03-19-abcdef.tar.bz2.gpg\")\n" $(hostname)
 	printf "\t·%s -z --no-encryption --no-sql /var/backup\n" $(basename $0)
 	printf "\t\tMake a backup in \"/var/backup\" compressed with GZip and not encrypted\n"
-	printf "\t\t(\"backup-'hostname'-2018-03-19-qwerty.tar.gz\")\n"
+	printf "\t\t(\"backup-%s-2018-03-19-qwerty.tar.gz\")\n" $(hostname)
 }
 
 function makeTar {
 	#Destination file
-	printf "Backup File:\t%s\n" $backupOutput
+	printf "Backup File:\t%s\n" $(realpath $backupOutput)
 
 	#Copy webpages code (except backup and main/public)
 	if [ $noWeb = false ];then
 		printf "Adding:\t%s\n" $webDir
-		tar -rf $tempOutput --exclude=var/www/backup --exclude=var/www/main/public $webDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $webDir > /dev/null 2>&1
 		if [ $? != 0 ]; then
 			printf "WARNING:\tError copying web pages (Continue).\n"
 		fi
@@ -224,7 +227,7 @@ function makeTar {
 	#Copy nginx configuration (sites-available)
 	if [ $noNginx = false ];then
 		printf "Adding:\t%s\n" $nginxDir
-		tar -rf $tempOutput $nginxDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $nginxDir > /dev/null 2>&1
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying nginx configuration (Continue)\n"
 		fi
@@ -233,7 +236,7 @@ function makeTar {
 	#Copy Email (this could be heavy in the future)
 	if [ $noMail = false ];then
 		printf "Adding:\t%s\n" $mailDir
-		tar -rf $tempOutput $mailDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $mailDir > /dev/null 2>&1
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying mailboxes (Continue)\n"
 		fi
@@ -242,7 +245,7 @@ function makeTar {
 	#Copy Certificates (LetsEncrypt)
 	if [ $noLetsencrypt = false ];then
 		printf "Adding:\t%s\n" $letsencryptDir
-		tar -rf $tempOutput $letsencryptDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $letsencryptDir > /dev/null 2>&1
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying Let's Encrypt certificates (Continue)\n"
 		fi
@@ -251,7 +254,7 @@ function makeTar {
 	#Copy /home
 	if [ $noHome = false ];then
 		printf "Adding:\t%s\n" $homeDir
-		tar -rf $tempOutput $homeDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $homeDir > /dev/null 2>&1
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying home dir (Continue)\n"
 		fi
@@ -260,7 +263,7 @@ function makeTar {
 	#Copy GOGS
 	if [ $noGogs = false ];then
 		printf "Adding:\t%s\n" $gogsDir
-		tar -rf $tempOutput $gogsDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $gogsDir > /dev/null 2>&1
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying GOGS Repository (Continue)\n"
 		fi
@@ -269,7 +272,7 @@ function makeTar {
 	#Copy Postfix
 	if [ $noPostfix = false ] && [ -d $postfixDir ];then
 		printf "Adding:\t%s\n" $postfixDir
-		tar -rf $tempOutput $postfixDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $postfixDir > /dev/null 2>&1
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying Postfix configuration (Continue)\n"
 		fi
@@ -278,7 +281,7 @@ function makeTar {
 	#Copy Dovecot
 	if [ $noDovecot = false ];then
 		printf "Adding:\t%s\n" $dovecotDir
-		tar -rf $tempOutput $dovecotDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $dovecotDir > /dev/null 2>&1
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying Dovecot configuration (Continue)\n"
 		fi
@@ -288,39 +291,33 @@ function makeTar {
 	if [ $noOpenDKIM = false ];then
 		if [ -f $openDKIMConf ];then
 			printf "Adding:\t%s\n" $openDKIMConf
-			tar -rf $tempOutput $openDKIMConf > /dev/null 2>&1
+			tar -rf $tempOutput $exclude $openDKIMConf > /dev/null 2>&1
 			if [ $? != 0 ];then
 				printf "WARNING:\tError copying OpenDKIM configuration (Continue)\n"
 			fi
-		else
-			printf "WARNING:\tFile %s not exist\n" $openDKIMConf
 		fi
 		if [ -f $openDKIMDefault ];then
 			printf "Adding:\t%s\n" $openDKIMDefault
-			tar -rf $tempOutput $openDKIMDefault > /dev/null 2>&1
+			tar -rf $tempOutput $exclude $openDKIMDefault > /dev/null 2>&1
 			if [ $? != 0 ];then
 				printf "WARNING:\tError copying OpenDKIM sockets configuration (Continue)\n"
 			fi
-		else
-			printf "WARNING:\tFile %s not exist\n" $openDKIMDefault
 		fi
 		if [ -d $openDKIMKeys ];then
 			printf "Adding:\t%s\n" $openDKIMKeys
-			tar -rf $tempOutput $openDKIMKeys > /dev/null 2>&1
+			tar -rf $tempOutput $exclude $openDKIMKeys > /dev/null 2>&1
 			if [ $? != 0 ];then
 				printf "WARNING:\tError copying OpenDKIM keys (Continue)\n"
 			fi
-		else
-			printf "WARNING:\tDir %s not exist\n" $openDKIMKeys
 		fi
 	fi
 	
 	#Copy SPF
 	if [ $noSPF = false ];then
 		printf "Adding:\t%s\n" $spfDir
-		tar -rf $tempOutput $spfDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $spfDir > /dev/null 2>&1
 		if [ $? != 0 ];then
-			printf "WARNING:\tError copying Postfix configuration (Continue)\n"
+			printf "WARNING:\tError copying SPF configuration (Continue)\n"
 		fi
 	fi
 	
@@ -328,28 +325,24 @@ function makeTar {
 	if [ $noOpenDMARC = false ];then
 		if [ -f $openDMARCConf ];then
 			printf "Adding:\t%s\n" $openDMARCConf
-			tar -rf $tempOutput $openDMARCConf > /dev/null 2>&1
+			tar -rf $tempOutput $exclude $openDMARCConf > /dev/null 2>&1
 			if [ $? != 0 ];then
 				printf "WARNING:\tError copying OpenDMARC configuration (Continue)\n"
 			fi
-		else
-			printf "WARNING:\tFile %s not exist\n" $openDMARCConf
 		fi
 		if [ -f $openDMARCDefault ];then
 			printf "Adding:\t%s\n" $openDMARCDefault
-			tar -rf $tempOutput $openDMARCDefault > /dev/null 2>&1
+			tar -rf $tempOutput $exclude $openDMARCDefault > /dev/null 2>&1
 			if [ $? != 0 ];then
 				printf "WARNING:\tError copying OpenDMARC sockets configuration (Continue)\n"
 			fi
-		else
-			printf "WARNING:\tFile %s not exist\n" $openDMARCDefault
 		fi
 	fi
 	
 	#Copy Amavis
 	if [ $noAmavis = false ];then
 		printf "Adding:\t%s\n" $amavisDir
-		tar -rf $tempOutput $amavisDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $amavisDir > /dev/null 2>&1
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying Amavis configuration (Continue)\n"
 		fi
@@ -358,7 +351,7 @@ function makeTar {
 	#Copy SPAMAssassin
 	if [ $noSpamAssassin = false ];then
 		printf "Adding:\t%s\n" $spamAssassinDir
-		tar -rf $tempOutput $spamAssassinDir > /dev/null 2>&1
+		tar -rf $tempOutput $exclude $spamAssassinDir > /dev/null 2>&1
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying SPAM Assasin configuration (Continue)\n"
 		fi
@@ -498,7 +491,7 @@ function checkRoutes {
 	fi
 	if [ $noSPF = false ] && [ ! -d $spfDir ];then
 		printf "WARNING:\t%s don't exist (no backup)\n" $spfDir
-		noSpf=true
+		noSPF=true
 	fi
 	# No noOpenDMARC=true, to make a parcial copy
 	if [ $noOpenDMARC = false ] && [ ! -f $openDMARCConf ];then
@@ -574,6 +567,7 @@ backupPostfix=-$(date +"%Y-%m-%d")-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -
 deleteDays="15"
 user="www-data"
 group="www-data"
+exclude=""
 
 #Default Routes
 webDir="/var/www"
@@ -618,7 +612,7 @@ noSpamAssassin=false
 sha512=true
 
 #Parse args
-TEMP=$(getopt -q -o zjJp:u:g:k:h --longoptions gzip,bzip2,xz,prefix:,postfix:,no-remove,remove-days:,user:,group:,permision-mask:,key-id:,no-encryption,web-dir:,no-web,no-sql,sql-user:,sql-password:,nginx-dir:,no-nginx,letsencrypt-dir:,no-letsencrypt,mail-dir:,no-mail,home-dir:,no-home,gogs-dir:,no-gogs,postfix-dir:,no-postfix,dovecot-dir:,no-dovecot,opendkim-conf:,opendkim-default:,opendkim-keys:,no-opendkim,spf-dir:,no-spf,opendmarc-conf:,opendmarc-default:,no-opendmarc,amavis-dir:,no-amavis,spamassassin-dir:,no-spamassassin,no-sha512,help --name $(basename $0) -- $@)
+TEMP=$(getopt -q -o zjJp:u:g:e:k:h --longoptions gzip,bzip2,xz,prefix:,postfix:,no-remove,remove-days:,user:,group:,permision-mask:,exclude:,key-id:,no-encryption,web-dir:,no-web,no-sql,sql-user:,sql-password:,nginx-dir:,no-nginx,letsencrypt-dir:,no-letsencrypt,mail-dir:,no-mail,home-dir:,no-home,gogs-dir:,no-gogs,postfix-dir:,no-postfix,dovecot-dir:,no-dovecot,opendkim-conf:,opendkim-default:,opendkim-keys:,no-opendkim,spf-dir:,no-spf,opendmarc-conf:,opendmarc-default:,no-opendmarc,amavis-dir:,no-amavis,spamassassin-dir:,no-spamassassin,no-sha512,help --name $(basename $0) -- $@)
 eval set -- $TEMP
 unset TEMP
 while true; do
@@ -707,6 +701,24 @@ while true; do
 			fi
 			shift 2
 			;;
+		-e|--exclude)
+			cont=1
+			fileCut=$(echo $2 | cut -d "," -f $cont)
+			fileCut=$(realpath -q -m $fileCut 2>/dev/null)
+			while [ $fileCut != "" ] > /dev/null 2>&1
+			do
+				if [ -d $fileCut ] || [ -f $fileCut ];then
+					exclude="$exclude --exclude=$fileCut"
+				else
+					printf "WARNING:\t%s don't exist\n" $fileCut
+				fi
+				cont=$((cont+1))
+				fileCut=$(echo $2 | cut -d "," -f $cont)
+				fileCut=$(realpath -q -m $fileCut 2>/dev/null)
+			done
+			unset cont fileCut
+			shift 2
+			;;
 		-k|--key-id)
 			keyID=$2
 			shift 2