Explorar el Código

Version 2.0 (Major Changes)
- Add option parser
- Add new compression methods (Gzip, Bzip2, XZ and 7Z)
- Add gpg2 encryption

Guzmán Castanedo Villalba hace 6 años
padre
commit
9aa574e723
Se han modificado 3 ficheros con 476 adiciones y 98 borrados
  1. 116 7
      README.md
  2. 358 90
      backup-server
  3. 2 1
      scripts/cron.d/backup-server

+ 116 - 7
README.md

@@ -1,18 +1,127 @@
 # backup-server
 
-Backup and download scripts to compress with 7z data for a debian server and download it to another machine.
+Backup and download scripts to compress with:
+* Gzip (DEFLATE)
+* Bzip2 (BWT)
+* XZ (LZMA2)
+* 7Z (LZMA)
 
 The backup script (**backup-server**) compress with 7z server info like:
-* Nginx configuration (sites-available)
-* MySQL Databases
-* Emails
-* Letsencrypt certificates
-* /home directories
+* Web Sites (/var/www)
+* SQL Databases (MySQL or MariaDB)
+* HTTP Server Configuration (/etc/nginx)
+* Let's Encrypt Certificates (/etc/letsencrypt)
+* Mail Mailboxes (/var/mail)
+* Home Directory (/home)
+* GOGS Git Repository (/opt/gogs)
 
 Also it's:
 * Autoremove old backups
-* Put it in a http/https server with Auth Basic Authentication.
+* Set backup dir
+* Encrypt backup (gpg2)
 
+## Instalation
+`# cp backup-server /usr/local/bin/backup-server`
+
+## Usage
+`# backup-server [options]... -z|-j|-J|-7 [backupdir]`
+
+###### Backup Directory:
+* `backupdir`: argument to set to directory to save the backup
+  * Default: /var/www/backup
+
+###### Backup Options:
+* `-p`, `--prefix name`: prefix for the name of the backup
+  * Default: backup-castanedo.es
+* `--no-postfix`: disable the postfi: the full name will be the prefix
+  * Default: prefix-yyyy-mm-dd(date)-XXXXXX(random)
+* `--no-remove`: no remove old files
+  * Default: false
+*	`--remove-days number`: set number of days to considere a backup old
+  * Default: 15
+*	`-u`, `--user uid`: name of the user owner of the backup
+  * Default: www-data
+*	`-g`, `--group gid`: name of the group owner of the backup
+  * Default: www-data
+*	`--permision-mask number`: octal mask to set accesss permision of the backup
+  * Default: 640
+
+###### Compression Options:
+*	`-z`, `--gzip`: compress using gzip (tar.gz or tar.gz.gpg)
+*	`-j`, `--bzip2`: compress using bzip2 (tar.bz2 or tar.bz2.gpg)
+*	`-J`, `--xz`: compress using xz (tar.xz or tar.xz.gpg)
+*	`-7`, `--7zip`: compress using 7z (7z)
+
+###### Encryption Options:
+*	`-k`, `--key-id ID`: set gpg2 public key-id used for encryption
+  * Use with Gzip, Bzip2 or XZ compression
+*	`--no-encryption`: disable the encryption of the backup
+  * Default: false
+*	`--7z-password password`: set password (ONLY for 7z)
+  * INSECURE Method
+
+###### Web Options:
+*	`--web-dir dir`: set web pages directory to backup
+  * Default: /var/www
+*	`--no-web`: disable backup of web pages
+  * Default: false
+
+###### SQL Options:
+*	`--no-sql`: disable MySQL/MariaDB backup
+  * Default: false
+*	`--sql-user username`: set MySQL/MariaDB username
+  * Default: root
+*	`--sql-password password`: set MySQL/MariaDB password
+  * INSECURE Method
+
+###### Nginx Options:
+*	`--nginx-dir dir`: set nginx configuration directory to backup
+  * Default: /etc/nginx
+*	`--no-nginx`: disable Nginx backup
+  * Default: false
+
+###### Let's Encrypt Options:
+*	`--letsencrypt-dir dir`: set Let's Encrypt configuration directory to backup
+  * Default: /etc/letsencrypt
+*	`--no-letsencrypt`: disable Let's Encrypt backup
+  * Default: false
+
+###### Mail Options:
+*	`--mail-dir dir`: set Mailboxes directory to backup
+  * Default: /var/mail
+*	`--no-mail`: disable mailboxes backup
+  * Default: false
+
+###### Home Options:
+*	`--home-dir dir`: set home directories to backup
+  * Default: /home
+*	`--no-home`: disable home directory backup
+  * Default: false
+
+###### GOGS Options:
+*	``--gogs-dir dir`: set GOGS Git Repository to backup
+  * Default: /opt/gogs
+*	`--no-gogs`: disable gogs backup
+  * Default: false
+
+###### Other Options:
+* -h, --help: shows this message and exit
+
+## Examples
+* `backup-server --xz -k ABCDEFG -p backup-example.com --no-sql /var/backup`
+  * Make a backup in `/var/backup` compressed with XZ and encrypted with GPG2 Public Key (`backup-example.com-2018-03-19-a1b2c3.tar.xz.gpg`)
+  * Disable SQL backup
+* `backup-server -7 --7z-password 7zpass --sql-user root --sql-password toor /var/www/backup`
+  * Make a backup in `/var/www/backup` compressed with 7Zip and encrypted (`backup-castanedo.es-2018-03-19-abcedf.7z`)
+* `backup-server -g --no-encryption --no-sql /var/backup`
+  * Make a backup in `/var/backup` compressed with Gzip and no encrypted (`/var/backup/backup-castanedo.es-2018-03-19-qwerty.tar.gz`)
+
+## Automatic Backups (Cron.d)
+It's possible to make backups automatic creating a cron.d task (`/etc/cron.d/backup-server`).
+
+There are an [example](https://code.castanedo.es/guzman/backup-server/src/master/scripts/cron.d/backup-server) in `scripts` folder.
+
+## Download scripts
 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).

+ 358 - 90
backup-server

@@ -1,36 +1,48 @@
 #!/bin/bash
 #/###################################################################\
-#| Make backup of castanedo.es server compressed with:               |
+#| Make backup of:                                                   |
+#|   -Web Pages (/var/www)                                           |
+#|   -SQL Databases (MySQL or MariaDB)                               |
+#|   -HTTP Server Configuration (/etc/nginx)                         |
+#|   -Let's Encrypt Certificates (/etc/letsencrypt)                  |
+#|   -Mail Mailboxes (/var/mail)                                     |
+#|   -Home Directory (/home)                                         |
+#|   -GOGS Git Repository (/opt/gogs)                                |
+#| Compressed with:                                                  |
 #|   -Gzip                                                           |
 #|   -Bzip2                                                          |
 #|   -XZ                                                             |
 #|   -7zip                                                           |
-#| and encrypt it.                                                   |
+#| and encrypted (or not).                                           |
+#|                                                                   |
+#| USAGE: backup-server [options] -z|-j|-J|-7 backupdir              |
+#| HELP:  backup-server -h                                           |
 #|                                                                   |
 #| Guzmán Castanedo (guzman@castanedo.es)                            |
-#| Version 1.9 (March 2018)                                          |
+#| Version 2.0 (March 2018)                                          |
 #| Licence: GPL v3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
 #\###################################################################/
 
 function usage {
-	printf "%s Version 1.9\n" $(basename $0)
-	printf "Usage: %s [options]... -z|-j|-J \n" $(basename $0)
+	printf "%s Version 2.0\n" $(basename $0)
+	printf "Usage: %s [options]... -z|-j|-J|-7 [backupdir]\n" $(basename $0)
 	printf "Make a encrypted backup of a server compressed with: \n"
 	printf "\t·Gzip (tar.gz or tar.gz.gpg)\n"
 	printf "\t·Bzip2 (tar.bz2 or tar.gz.gpg)\n"
 	printf "\t·XZ (tar.xz or tar.xz.gpg)\n"
 	printf "\t·7Z (7z)\n"
 	printf "\n"
-	printf "Backup Options:\n"
-	printf "\t-d, --directory dir: backup directory\n"
+	printf "Backup Directory:\n"
+	printf "\tbackupdir: argument to set to directory to save the backup\n"
 	printf "\t\tDefault: %s\n" $backupDir
+	printf "Backup Options:\n"
 	printf "\t-p, --prefix name: prefix for the name of the backup\n"
 	printf "\t\tDefault: %s\n" $backupPrefix
 	printf "\t--no-postfix: disable the postfi: the full name will be the prefix\n"
 	printf "\t\tDefault: prefix-yyyy-mm-dd(date)-XXXXXX(random)\n"
 	printf "\t--no-remove: no remove old files\n"
 	printf "\t\tDefault: false\n"
-	printf "\t--remove-days numbet: set number of days to considere a backup old\n"
+	printf "\t--remove-days number: set number of days to considere a backup old\n"
 	printf "\t\tDefault: %s\n" $deleteDays
 	printf "\t-u, --user uid: name of the user owner of the backup\n"
 	printf "\t\tDefault: %s\n" $user
@@ -47,10 +59,11 @@ function usage {
 	printf "\n"
 	printf "Encryption Options:\n"
 	printf "\t-k, --key-id ID: set gpg2 public key-id used for encryption\n"
+	printf "\t\tUse with Gzip, Bzip2 or XZ compression\n"
 	printf "\t\tDefault: %s\n" $keyID
 	printf "\t--no-encryption: disable the encryption of the backup\n"
 	printf "\t\tDefault: %s\n" $noEncryption
-	printf "\t--7z-password password: set password (ONLY for 7z)\n"
+	printf "\t--7z-password password: set password (ONLY for 7z) (INSECURE)\n"
 	printf "\t\tDefault: %s\n" $pass7z
 	printf "\n"
 	printf "Web Options:\n"
@@ -60,8 +73,12 @@ function usage {
 	printf "\t\tDefault: %s\n" $noWeb
 	printf "\n"
 	printf "SQL Options:\n"
-	printf "\t--no-sql: disable MySQL/MAriaDB backup\n"
+	printf "\t--no-sql: disable MySQL/MariaDB backup\n"
 	printf "\t\tDefault: %s\n" $noSql
+	printf "\t--sql-user username: set MySQL/MariaDB username\n"
+	printf "\t\tDefault: %s\n" $mysqluser
+	printf "\t--sql-password password: set MySQL/MariaDB password (INSECURE)\n"
+	printf "\t\tDefault: %s\n" $mysqlpass
 	printf "\n"
 	printf "Nginx Options:\n"
 	printf "\t--nginx-dir dir: set nginx configuration directory to backup\n"
@@ -78,7 +95,7 @@ function usage {
 	printf "Mail Options:\n"
 	printf "\t--mail-dir dir: set Mailboxes directory to backup\n"
 	printf "\t\tDefault: %s\n" $mailDir
-	printf "\t\t--no-mail: disable mailboxes backup\n"
+	printf "\t--no-mail: disable mailboxes backup\n"
 	printf "\t\tDefault: %s\n" $noMail
 	printf "\n"
 	printf "Home Options:\n"
@@ -88,27 +105,33 @@ function usage {
 	printf "\t\tDefault: %s\n" $noHome
 	printf "\n"
 	printf "GOGS Options:\n"
-	printf "\t--gogs-dir dir: set GOGS Repository to backup\n"
+	printf "\t--gogs-dir dir: set GOGS Git Repository to backup\n"
 	printf "\t\tDefault: %s\n" $gogsDir
 	printf "\t--no-gogs: disable gogs backup\n"
 	printf "\t\tDefault: %s\n" $noGogs
-	exit
+	printf "\n"
+	printf "Other Options:\n"
+	printf "\t-h, --help: shows this message and exit\n"
 }
 
 function makep7zip {
 	#Destination file
-	ext=".7z"
-	backupOutput=$backupOutput$ext
+	encrypt7z=""
+	if [ ! -z $pass7z ];then
+		encrypt7z="-p$pass7z -mhe"
+	fi
+	if [ $noEncryption = true ];then
+		encrypt7z=""
+	fi
 	printf "Backup File (7z):\t%s\n" $backupOutput
 
 	#Copy webpages code (except backup and main/public)
 	if [ $noWeb = false ] && [ -d $webDir ];then
 		printf "Compressing:\t%s\n" $webDir
-		#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 $webDir -x@$tempfile > /dev/null
+		7z a -t7z -mx=9 $encrypt7z $backupOutput $webDir -x@$tempfile > /dev/null
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying web pages (Continue).\n"
 		fi
@@ -116,7 +139,7 @@ function makep7zip {
 	fi
 
 	#Copy MySQL databases (mysqldump)
-	if [ $noSql = false ] && [ -x "$(which mysql)" ] && [ -x "$(which mysqldump)" ]; then
+	if [ $noSql = false ] && [ ! -z $mysqluser ] && [ ! -z $mysqlpass ] && [ -x "$(which mysql)" ] && [ -x "$(which mysqldump)" ]; then
 		list=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;")
 		#Parse databases expect information_schema & performance_schema
 		for database in $list; do
@@ -129,7 +152,7 @@ function makep7zip {
 			done
 			if [ $valid = true ]; then
 				printf "Compressing 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 2>&1
+				mysqldump -u $mysqluser -p$mysqlpass $database | 7z a -t7z -mx=9 $encrypt7z $backupOutput -simysql/$database.sql > /dev/null 2>&1
 				if [ $? != 0 ];then
 					printf "WARNING:\tError compressing database (%s) (Continue).\n" $database
 				fi
@@ -140,7 +163,7 @@ function makep7zip {
 	#Copy nginx configuration (sites-available)
 	if [ $noNginx = false ] && [ -d $nginxDir ];then
 		printf "Compressing:\t%s\n" $nginxDir
-		7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput $nginxDir > /dev/null
+		7z a -t7z -mx=9 $encrypt7z $backupOutput $nginxDir > /dev/null
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying nginx configuration (Continue)\n"
 		fi
@@ -149,7 +172,7 @@ function makep7zip {
 	#Copy Email (this could be heavy in the future)
 	if [ $noMail = false ] && [ -d $mailDir ];then
 		printf "Compressing:\t%s\n" $mailDir
-		7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput $mailDir > /dev/null
+		7z a -t7z -mx=9 $encrypt7z $backupOutput $mailDir > /dev/null
 		#tar -c -zf - $mailDir | 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput -simail.tar.gz > /dev/null
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying mailboxes (Continue)\n"
@@ -159,7 +182,7 @@ function makep7zip {
 	#Copy Certificates (LetsEncrypt)
 	if [ $noLetsencrypt = false ] && [ -d $letsencryptDir ];then
 		printf "Compressing:\t%s\n" $letsencryptDir
-		7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput $letsEncryptDir > /dev/null
+		7z a -t7z -mx=9 $encrypt7z $backupOutput $letsEncryptDir > /dev/null
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying Let's Encrypt certificates (Continue)\n"
 		fi
@@ -168,7 +191,7 @@ function makep7zip {
 	#Copy /home
 	if [ $noHome = false ] && [ -d $homeDir ];then
 		printf "Compressing:\t%s\n" $homeDir
-		7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput $homeDir > /dev/null
+		7z a -t7z -mx=9 $encrypt7z $backupOutput $homeDir > /dev/null
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying home dir (Continue)\n"
 		fi
@@ -177,7 +200,7 @@ function makep7zip {
 	#Copy GOGS
 	if [ $noGogs = false ] && [ -d $gogsDir ];then
 		printf "Compressing:\t%s\n" $gogsDir
-		7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput $gogsDir > /dev/null
+		7z a -t7z -mx=9 $encrypt7z $backupOutput $gogsDir > /dev/null
 		if [ $? != 0 ];then
 			printf "WARNING:\tError copying GOGS Repository (Continue)\n"
 		fi
@@ -186,7 +209,7 @@ function makep7zip {
 
 function makeTar {
 	#Destination file
-	printf "Backup File:\t%s\n" $backupOutput.tar.gz.gpg
+	printf "Backup File:\t%s\n" $backupOutput
 
 	#Copy webpages code (except backup and main/public)
 	if [ $noWeb = false ] && [ -d $webDir ];then
@@ -198,7 +221,7 @@ function makeTar {
 	fi
 
 	#Copy MySQL databases (mysqldump)
-	if [ $noSql = false ] && [ -x "$(which mysql)" ] && [ -x "$(which mysqldump)" ]; then
+	if [ $noSql = false ] && [ ! -z $mysqluser ] && [ ! -z $mysqlpass ] && [ -x "$(which mysql)" ] && [ -x "$(which mysqldump)" ]; then
 		list=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;" 2> /dev/null)
 		mkdir /tmp/mysql
 		#Parse databases expect information_schema & performance_schema
@@ -224,7 +247,7 @@ function makeTar {
 				rm /tmp/mysql/$database.sql
 			fi
 		done
-		rmdir /tmp/mysql
+		rm -R /tmp/mysql
 	fi
 
 	#Copy nginx configuration (sites-available)
@@ -274,77 +297,132 @@ function makeTar {
 }
 
 function gzUnencrypted {
-	ext=".tar.gz"
-	backupOutput=$backupOutput$ext
-	printf "Compressing (GZIP)... %s\n" $backupOutput
+	printf "Compressing (GZIP)..."
 	gzip -9 --stdout $tempOutput > $backupOutput
 	if [ $? != 0 ]; then
-		printf "ERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
+		printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
 		exit 1
 	fi
 	rm $tempOutput
 }
 
 function gzEncrypted {
-	ext=".tar.gz.gpg"
-	backupOutput=$backupOutput$ext
-	printf "Compressing (GZIP) and encrypting... %s\n" $backupOutput
+	printf "Compressing (GZIP) and encrypting..."
 	gzip -9 --stdout $tempOutput | gpg2 --no-batch --output $backupOutput --encrypt -r $keyID -
 	if [ $? != 0 ]; then
-		printf "ERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
+		printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
 		exit 1
 	fi
 	rm $tempOutput
 }
 
 function bz2Unencrypted {
-	ext=".tar.bz2"
-	backupOutput=$backupOutput$ext
-	printf "Compressing (BZIP2)... %s\n" $backupOutput
+	printf "Compressing (BZIP2)..."
 	bzip2 -9 --stdout $tempOutput > $backupOutput
 	if [ $? != 0 ]; then
-		printf "ERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
+		printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
 		exit 1
 	fi
 	rm $tempOutput
 }
 
 function bz2Encrypted {
-	ext=".tar.bz2.gpg"
-	backupOutput=$backupOutput$ext
-	printf "Compressing (BZIP2) and encrypting... %s\n" $backupOutput
+	printf "Compressing (BZIP2) and encrypting..."
 	bzip2 -9 --stdout $tempOutput | gpg2 --no-batch --output $backupOutput --encrypt -r $keyID -
 	if [ $? != 0 ]; then
-		printf "ERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
+		printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
 		exit 1
 	fi
 	rm $tempOutput
 }
 
 function xzUnencrypted {
-	ext=".tar.xz"
-	backupOutput=$backupOutput$ext
-	printf "Compressing (XZ)... %s\n" $backupOutput
+	printf "Compressing (XZ)..."
 	xz -9 --stdout $tempOutput > $backupOutput
 	if [ $? != 0 ]; then
-		printf "ERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
+		printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
 		exit 1
 	fi
 	rm $tempOutput
 }
 
 function xzEncrypted {
-	ext=".tar.xz.gpg"
-	backupOutput=$backupOutput$ext
-	printf "Compressing (XZ) and encrypting... %s\n" $backupOutput
+	printf "Compressing (XZ) and encrypting..."
 	xz -9 --stdout $tempOutput | gpg2 --no-batch --output $backupOutput --encrypt -r $keyID -
 	if [ $? != 0 ]; then
-		printf "ERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
+		printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
 		exit 1
 	fi
 	rm $tempOutput
 }
 
+function checkRoutes {
+	if [ ! -d $backupDir ];then
+		printf "ERROR:\tBackup directory don't exist\n"
+		usage
+		exit 1
+	fi
+	if [ ! -d $webDir ];then
+		printf "WARNING:\t%s don't exist (no backup)\n" $webDir
+	fi
+	if [ ! -d $nginxDir ];then
+		printf "WARNING:\t%s don't exist (no backup)\n" $nginxDir
+	fi
+	if [ ! -d $letsencryptDir ];then
+		printf "WARNING:\t%s don't exist (no backup)\n" $letsencryptDir
+	fi
+	if [ ! -d $mailDir ];then
+		printf "WARNING:\t%s don't exist (no backup)\n" $mailDir
+	fi
+	if [ ! -d $homeDir ];then
+		printf "WARNING:\t%s don't exist (no backup)\n" $homeDir
+	fi
+	if [ ! -d $gogsDir ];then
+		printf "WARNING:\t%s don't exist (no backup)\n" $gogsDir
+	fi
+}
+
+function checkMethod {
+	if [ $gzipOn = false ] && [ $bzip2On = false ] && [ $xzOn = false ] && [ $p7zipOn = false ];then
+		printf "ERROR:\tCompressing method not set\n"
+		exit 1
+	fi
+	if [ $noEncryption = false ];then
+		if [ $gzipOn = true ] || [ $bzip2On = true ] || [ $xzOn = true ];then
+			if [ ! -x $(which gpg2) ];then
+				printf "WARNING:\tGPG2 Not Installed: continue without encryption (type \"%s -h\" for help)\n" $(basename $0)
+				noEncryption=true
+			else
+				if [ ! -z $keyID ];then
+					ext=$ext.gpg
+					gpg2 --list-keys $keyID > /dev/null 2>&1
+					if [ $? != 0 ];then
+						printf "ERROR:\tKeyID %s don't exist in keyring (\"gpg2 --list-keys\" to see all keys)\n" $keyID
+						exit 1
+					fi
+				else
+					printf "WARNING:\tKeyID (-k keyID) not set: continue without encryption (type \"%s -h\" for help)\n" $(basename $0)
+					noEncryption=true
+				fi
+			fi
+		else
+			if [ -z $pass7z ];then
+				printf "WARNING:\t7Z password (--7z-password) not set: continue without encryption (type \"%s -h\" for help)\n" $(basename $0)
+				noEncryption=true
+			fi
+		fi
+	fi
+}
+
+function checkSqlAuth {
+	if [ $noSql = false ];then
+		if [ -z $mysqluser ] || [ -z $mysqlpass ]; then
+			printf "WARNING:\tSQL User/Password not set: continue without SQL backup (type \"%s -h\" for help)\n" $(basename $0)
+			noSql=true
+		fi
+	fi
+}
+
 #Check root
 startTime=$(date +"%s")
 if [ $(id -u) -ne 0 ]; then
@@ -352,21 +430,19 @@ if [ $(id -u) -ne 0 ]; then
 	exit 1
 fi
 
-#Data
+#Default Data
 mysqluser="root"
-mysqlpass="mysqlpasswd"
-keyID="A288A3FB"
-pass7z="password-for-7z"
+mysqlpass=""
+keyID=""
+pass7z=""
 backupDir=/var/www/backup
 backupPrefix="backup-castanedo.es"
-backupName=$backupPrefix-$(date +"%Y-%m-%d")-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1)
-backupOutput=$backupDir/$backupName
-tempOutput=/tmp/$backupName.tar
+backupPostfix=-$(date +"%Y-%m-%d")-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1)
 deleteDays="15"
 user="www-data"
 group="www-data"
 
-#Routes
+#Default Routes
 webDir="/var/www"
 nginxDir="/etc/nginx"
 mailDir="/var/mail"
@@ -374,12 +450,13 @@ letsencryptDir="/etc/letsencrypt"
 homeDir="/home"
 gogsDir="/opt/gogs"
 
-#Control
+#Control Variables
 noEncryption=false
 gzipOn=false
 bzip2On=false
-xzOn=true
+xzOn=false
 p7zipOn=false
+ext=""
 permisionMask=640
 removeOld=true
 noWeb=false
@@ -391,10 +468,216 @@ noHome=false
 noGogs=false
 
 #Parse args
-#TEMP="$(getopt)"
-#usage
+TEMP=$(getopt -q -o zjJ7p:u:g:k:h --longoptions gzip,bzip2,xz,7zip,prefix:,no-postfix,no-remove,remove-days:,user:,group:,permision-mask:,key-id:,no-encryption,7z-password:,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,help --name $(basename $0) -- $@)
+eval set -- $TEMP
+unset TEMP
+while true; do
+	case $1 in
+		-z|--gzip)
+			gzipOn=true
+			ext=".tar.gz"
+			if [ $bzip2On = true ] || [ $xzOn = true ] || [ $p7zipOn = true ];then
+				printf "ERROR:\tOnly choose one compressing method\n"
+				exit 1
+			fi
+			if [ ! -x $(which gzip) ];then
+				printf "ERROR:\tGzip Not Installed\n"
+				exit 1
+			fi
+			shift
+			;;
+		-j|--bzip2)
+			bzip2On=true
+			ext=".tar.bz2"
+			if [ $gzipOn = true ] || [ $xzOn = true ] || [ $p7zipOn = true ];then
+				printf "ERROR:\tOnly choose one compressing method\n"
+				exit 1
+			fi
+			if [ ! -x $(which bzip2) ];then
+				printf "ERROR:\tBzip2 Not Installed\n"
+				exit 1
+			fi
+			shift
+			;;
+		-J|--xz)
+			xzOn=true
+			ext=".tar.xz"
+			if [ $gzipOn = true ] || [ $bzip2On = true ] || [ $p7zipOn = true ];then
+				printf "ERROR:\tOnly choose one compressing method\n"
+				exit 1
+			fi
+			if [ ! -x $(which xz) ];then
+				printf "ERROR:\tXZ Not Installed\n"
+				exit 1
+			fi
+			shift
+			;;
+		-7|--7zip)
+			p7zipOn=true
+			ext=".7z"
+			if [ $gzipOn = true ] || [ $bzip2On = true ] || [ $xzOn = true ];then
+				printf "ERROR:\tOnly choose one compressing method\n"
+				exit 1
+			fi
+			if [ ! -x $(which 7z) ];then
+				printf "ERROR:\t7Z Not Installed\n"
+				exit 1
+			fi
+			shift
+			;;
+		-p|--prefix)
+			backupPrefix=$2
+			shift 2
+			;;
+		--no-postfix)
+			backupPostfix=""
+			shift
+			;;
+		--no-remove)
+			removeOld=false
+			shift
+			;;
+		--remove-days)
+			deleteDays=$2
+			shift 2
+			;;
+		-u|--user)
+			grep $2 /etc/passwd > /dev/null 2>&1
+			if [ $? = 0 ]; then
+				user=$2
+			else
+				printf "ERROR:\tUser %s don't exist\n" $2
+				exit 1
+			fi
+			shift 2
+			;;
+		-g|--group)
+			grep $2 /etc/group > /dev/null 2>&1
+			if [ $? = 0 ];then
+				group=$2
+			else
+				printf "ERROR:\tGroup %s don't exist\n" $2
+				exit 1
+			fi
+			shift 2
+			;;
+		--permision-mask)
+			if [ $2 -eq $2 ] 2> /dev/null;then
+				permisionMask=$2
+			else
+				printf "ERROR:\tPermission has to be a number (%s)\n" $2
+				exit 1
+			fi
+			shift 2
+			;;
+		-k|--key-id)
+			keyID=$2
+			shift 2
+			;;
+		--no-encryption)
+			noEncryption=true
+			shift
+			;;
+		--7z-password)
+			pass7z=$2
+			shift 2
+			;;
+		--web-dir)
+			webDir=$2
+			shift 2
+			;;
+		--no-web)
+			noWeb=true
+			shift
+			;;
+		--no-sql)
+			noSql=true
+			shift
+			;;
+		--sql-user)
+			mysqluser=$2
+			shift 2
+			;;
+		--sql-password)
+			mysqlpass=$2
+			shift 2
+			;;
+		--nginx-dir)
+			nginxDir=$2
+			shift 2
+			;;
+		--no-nginx)
+			noNginx=true
+			shift
+			;;
+		--letsencrypt-dir)
+			letsencryptDir=$2
+			shift 2
+			;;
+		--no-letsencrypt)
+			noLetsencrypt=true
+			shift
+			;;
+		--mail-dir)
+			mailDir=$2
+			shift 2
+			;;
+		--no-mail)
+			noMail=true
+			shift
+			;;
+		--home-dir)
+			homeDir=$2
+			shift 2
+			;;
+		--no-home)
+			noHome=true
+			shift
+			;;
+		--gogs-dir)
+			gogsDir=$2
+			shift 2
+			;;
+		--no-gogs)
+			noGogs=true
+			shift
+			;;
+		-h|--help)
+			usage
+			exit 0
+			shift
+			;;
+		--)
+			#Last One
+			shift
+			break
+			;;
+		*)
+			#Unspected
+			usage
+			printf "\nERROR:\tInvalid Option (%s)\n" $1
+			exit 1
+			;;
+	esac
+done
+if [ ! $# -eq 1 ];then
+	printf "ERROR:\tBackup directory not set\n"
+	usage
+	exit 1
+else
+	backupDir=$1
+	backupName=$backupPrefix$backupPostfix
+	backupOutput=$backupDir/$backupName
+	tempOutput=/tmp/$backupName.tar
+fi
+
+#Check (routes, compression, encryption and SQL auth)
+checkRoutes
+checkMethod
+checkSqlAuth
 
 #make tar file or 7z
+backupOutput=$backupOutput$ext
 if [ $p7zipOn = true ];then
 	if [ ! -x $(which 7z) ];then
 		printf "ERROR:\tp7zip Not Installed\n"
@@ -409,47 +692,31 @@ else
 	makeTar
 fi
 
-#Compression and encryption
+#Compression and encryption (Except 7Zip -> Already done)
 if [ $gzipOn = true ];then
-	if [ ! -x $(which gzip) ];then
-		printf "ERROR:\tGzip Not Installed\n"
-		rm $tempOutput
-		exit 1
-	fi
-	if [ $noEncryption = true ] || [ ! -x $(which gpg2) ];then
+	if [ $noEncryption = true ];then
 		gzUnencrypted
 	else
 		gzEncrypted
 	fi
 elif [ $bzip2On = true ];then
-	if [ ! -x $(which bzip2) ];then
-		printf "ERROR:\tBzip2 Not Installed\n"
-		rm $tempOutput
-		exit 1
-	fi
-	if [ $noEncryption = true ] || [ ! -x $(which gpg2) ];then
+	if [ $noEncryption = true ];then
 		bz2Unencrypted
 	else
 		bz2Encrypted
 	fi
 elif [ $xzOn = true ];then
-	if [ ! -x $(which xz) ];then
-		printf "ERROR:\tXZ Not Installed\n"
-		rm $tempOutput
-		exit 1
-	fi
-	if [ $noEncryption = true ] || [ ! -x $(which gpg2) ];then
+	if [ $noEncryption = true ];then
 		xzUnencrypted
 	else
 		xzEncrypted
 	fi
-elif [ $p7zipOn = true ];then
-	#Nothing to do
-	printf "moo" > /dev/null
 else
-	printf "ERROR:\tCompression method not set\n"
-	rm $tempOutput
-	exit 1
+	if [ $p7zipOn = false ];then
+		printf "ERROR:\tCompression method not set\n"
+		rm $tempOutput
+		exit 1
+	fi
 fi
 
 #Permissions
@@ -458,8 +725,8 @@ chmod $permisionMask $backupOutput
 
 #Remove files older than 15 days
 if [ $removeOld = true ];then
-	printf "Eliminando backups antiguos (+15 dias)\n"
-	find $backupDir -mindepth 1 -mtime +$deleteDays -type f -iname $backupPrefix*$ext -delete
+	printf "\nEliminando backups antiguos (+15 dias)\n"
+	find $backupDir -mindepth 1 -maxdepth 1 -mtime +$deleteDays -type f -iname $backupPrefix*$ext -delete
 	if [ $? != 0 ];then
 		printf "WARNING:\tError eliminando backup's antiguos (%s dias)\n" $deleteDays
 	fi
@@ -467,6 +734,7 @@ fi
 
 #End
 finalTime=$(date +"%s")
+printf "\n"
 echo "------------------------------------------------"
 printf "Backup completado con exito en %s segundos :)\n" $((finalTime-startTime))
 echo "------------------------------------------------"

+ 2 - 1
scripts/cron.d/backup-server

@@ -1,5 +1,6 @@
 # /etc/cron.d/backup-server: crontab for backup castanedo.es
 
 # Backup every day at 4:00 am
-0 4 * * *  root  [ -x /usr/local/bin/backup-server ] && /usr/local/bin/backup-server >> /var/log/backup-server.log 2>&1
+#0 4 * * *  root  [ -x /usr/local/bin/backup-server ] && /usr/local/bin/backup-server --7zip --7z-password 7zpass --sql-password sqlpass -p backup-castanedo.es /var/www/backup >> /var/log/backup-server.log 2>&1
+0 4 * * *  root  [ -x /usr/local/bin/backup-server ] && /usr/local/bin/backup-server --xz -k keyID --sql-password sqlpass -p backup-castanedo.es /var/www/backup >> /var/log/backup-server.log 2>&1