[Guide] Linux Server auf Windows Share sichern

xrated

Enthusiast
Thread Starter
Mitglied seit
31.01.2007
Beiträge
861
Bei mir ist es so das die Linux Kiste ständig an ist und Daten enthält die zusätzlich gesichert werden sollen. Als Sicherungsziel macht ja nun der selbe Rechner wenig Sinn also wählte ich einen PC mit Windows. Dazu verwende ich rdiff-backup und das schöne ist das dies geänderte Files nicht nochmal komplett speichert sondern nur die Deltas.

Ich habe ein Script geschrieben welches zuerst bestimmte Verz. z.B. /etc /usr /var in ein passwortgeschütztes RAR kopiert und anschließend wird ein Ordner per rdiff-backup auf ein Cifs Share gesichert.

Weil Windows die Files nicht nach upper/lowercase unterscheidet und mit rdiff-backup sonst ulkige Filenamen entstehen und das ganze irgendwann nicht mehr richtig funktioniert, ist auf dem CIFS Share eine große Datei mit einem Ext3 FS das mit loop gemountet wird. So wie es hier beschrieben wird:
» Rdiff-backup unter Debian auf SMB/CIFS-Mount - No nix narrets! - neunzehn83.de

Zusätzlich kann man sich noch eine Mail schicken lassen bei Backup Ende.

Wenn man das Script mit mount gestartet wird, wird nur das Ext3 gemountet und man z.B. einen restore mit rdiff-web machen.

Das ganze kann man dann in crontab -e packen z.B. täglich mittags:

0 12 * * * /etc/rdiffback.sh backup

Das Script:

Code:
#!/bin/bash

#backup options
BACKUPRDIFF=yes #start rdiff-backup? if set to no, backupconfig can still be done
FILEOT=1Y #delete files older than
BACKUPFOLDER=/data #folder to backup on local machine, do not end path with /
LOGLEVEL=5 #loglevel 1-9
RDIFFLOG=/etc/rdiffback.log

#CIFS share as target for backup
SMBSOURCE=192.168.2.104 # host or ip
SMBSHARE=rdiff
CREDFILE=/etc/.smb #contains user,password for smb mount
MOUNTCIFS=rw,proto=tcp,hard,forcedirectio,mapchars,nocase,iocharset=utf8,dir_mode=0660,file_mode=0660,credentials=$CREDFILE
SMBTARGET=/mnt/smbvenus #where SMB share should be mounted to, do not end path with /

#Loopmount
BACKUPFILE=backup.ext3 #backup file on SMB share
MOUNTFS=ext3 #linux filesystem for loopmount
LOOPTARGET=/mnt/loopvenus #path where file should be loop mounted to, do not end path with /
MOUNTLINUX=sync,loop,rw,noatime,barrier=1 #mount options for linux fs

#backup config? files will be added to rar file with password on specified location
BACKUPCONFIG=yes #runs also if rdiff-backup is disabled above in backup options
RARPWD=$(cat /etc/.rar)
BACKUPCONFIGFOLDER=/Dokumente/debian #config will be stored in subfolder of Backupfolder, do not end path with /
ETCFOLDER="/etc" #folder to backup
ETCRAR=etc.rar #target for rar
USRFOLDER="/usr/lib/nagios* /usr/share/nagios* /usr/share/logwatch*"
USRRAR=usr.rar
VARFOLDER="/var/vmail* /var/www* /var/lib/mysql"
VARRAR=var.rar

#email?
EMAILDO=yes #old logs will be deleted if set to yes
EMAILTO=mail@intern.net

case "$1" in
    backup)

    if [ "${BACKUPCONFIG,,}" == "yes" ]; then
	#rar various config folders to backup folder
	rar a -hp$RARPWD $BACKUPFOLDER$BACKUPCONFIGFOLDER/$ETCRAR $ETCFOLDER
	rar a -hp$RARPWD $BACKUPFOLDER$BACKUPCONFIGFOLDER/$USRRAR $USRFOLDER
	rar a -hp$RARPWD $BACKUPFOLDER$BACKUPCONFIGFOLDER/$VARRAR $VARFOLDER
	#add information to backup log
    	echo "The following folders: $ETCFOLDER, $USRFOLDER, $VARFOLDER were compressed (rar) to $BACKUPFOLDER$BACKUPCONFIGFOLDER" >> $RDIFFLOG
    fi
    
    if [ "${BACKUPRDIFF,,}" == "yes" ]; then
	if [ "$(ping -c 1 $SMBSOURCE | grep "1 received")" ]; then
	    #stop rdiff-web so that unmount cannot fail
    	    /etc/init.d/rdiff-web stop
	    mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
    	    mount -t $MOUNTFS -o $MOUNTLINUX $SMBTARGET/$BACKUPFILE $LOOPTARGET
    	    if [ "$(ls -A $LOOPTARGET)" ]; then
    		# add information to log
    		echo "$(date) : rdiff-backup of $BACKUPFOLDER to //$SMBSOURCE/$SMBSHARE/$BACKUPFILE started. $BACKUPFILE contains an $MOUNTFS filesystem." >> $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log
    		# delete older than defined in variable FILEOT
    		rdiff-backup -v$LOGLEVEL --remove-older-than $FILEOT $LOOPTARGET/data/
		# start backup
    		rdiff-backup -v$LOGLEVEL $BACKUPFOLDER/ $LOOPTARGET$BACKUPFOLDER/
		# copy rdiff-backup log to script logfile
    		cat $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log >> $RDIFFLOG
    		if [ "${EMAILDO,,}" == "yes" ]; then
    		    # delete backup log, otherwise old logs get mailed again
    		    rm $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log
    		fi
    	    fi
    	    echo "umount Loop ..."
    	    sleep 20
    	    umount $LOOPTARGET
    	    echo "umount SMB ..."
    	    sleep 20
    	    umount $SMBTARGET
	fi
    fi
    
    if [ "${EMAILDO,,}" == "yes" ]; then
    	# mail file
    	mail -s "[rdiffback.sh] - Backup completed" $EMAILTO < $RDIFFLOG
    	# delete old log, otherwise old logs will be mailed everytime again
    	rm $RDIFFLOG
    fi

    ;;

    mount)

	mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
	sleep 20
	mount -t $MOUNTFS -o $MOUNTLINUX $SMBTARGET/$BACKUPFILE $LOOPTARGET
	sleep 3
	/etc/init.d/rdiff-web start
	echo "you can start rdiff-web now on http://<host>:port, usually port 8080"
    ;;

    umount)

	/etc/init.d/rdiff-web stop
	sleep 3
	umount $LOOPTARGET
	sleep 20
	umount $SMBTARGET

    ;;

    *)

	echo "Script uses rdiff-backup in order to backup to an SMB share."
	echo "Create on SMB a big file, format it with linux file system and change script parameters, before using it."
	echo "If you have problem with corrupted backups, check output of /var/log/kern.log."
	echo "Usage: /etc/rdiff {backup|mount|umount}"
	exit 1
	;;

esac
 
Zuletzt bearbeitet:
Wenn Du diese Anzeige nicht sehen willst, registriere Dich und/oder logge Dich ein.
Beispielsweise habe ich 150GB Musik per rar verpackt und per rdiff auf den Server gesichert. Soweit so gut.
Nun kommt ein neues Album hinzu.
Also packt er wieder 150GB Musik + das neue Album und sendet per rdiff an den Server.
Neu gesichert wird von rdiff also nur das neue Album ABER das Verpacken wird dabei komplett von anfang bis ende durchgeführt und dauert ewig oder habe ich was übersehen im Skript?

Ich nutze ebenfalls rdiff-backup ohne vorher das packen. Das rdiff-web war mir neu aber man kann auch per WinSCP auf die Daten ganz einfach zugreifen.
 
Zuletzt bearbeitet:
Also in ein rar gelangen bei mir nur ein paar Configfiles mit wenigen MB.

Wenn man alles vorher raren würde, dann würde das mit rdiff ja keinen Sinn mehr machen weil da kein differential mehr funktioniert (glaube ich jedenfalls, dass die Files darin jedes mal komplett anders sind).

Ich habe das nur mit rar zusätzlich reingemacht weil ich insgesamt nur einen Folder sichern möchte und dort z.B. Passwörter drin gespeichert sind, wo nicht jeder drauf zugriff erhalten soll.

Man kann das mit dem rar auch global im Script deaktivieren falls man das nicht braucht.
 
zusätzlich zu den rar Dateien würde ich noch parity *.par2 Dateien erstellen lassen.
 
Also ich hab ein bißchen mit rar rumgespielt und mit dem update switch muss man nicht jedesmal alles neu packen. Das funktioniert dann allerdings nicht mehr mit multivolumes die für recovery volumes notwendig wären. Statt dessen müsste -rr5% eine gute Alternative sein. Es werden damit recovery records hinzugefügt.
 
Anbei eine überarbeitete Version

Code:
#!/bin/bash
#rdiffback.sh
#version 1.1
#by xrated 11/2011

#backup options
BACKUPRDIFF=yes #start rdiff-backup? if set to no, backupconfig can still be done
FILEOT=1Y #delete files older than
BACKUPFOLDER=/data #folder to backup on local machine, do not end path with /
LOGLEVEL=5 #loglevel 1-9
RDIFFLOG=/var/log/rdiffback.log
RDIFFBIN=/usr/bin/rdiff-backup

#CIFS share as target for backup
SMBSOURCE=192.168.2.104 # host or ip
SMBSHARE=rdiff
CREDFILE=/etc/.smb #contains user,password for smb mount separated by new line: username=youruser password=yourpassword
MOUNTCIFS=rw,hard,forcedirectio,mapchars,nocase,iocharset=utf8,dir_mode=0660,file_mode=0660,credentials=$CREDFILE
SMBTARGET=/mnt/smbvenus #where SMB share should be mounted to, do not end path with /

#Loopmount (contains linux fs)
BACKUPFILE=backup.ext3 #backup file on SMB share
MOUNTFS=ext3 #linux filesystem for loopmount
LOOPTARGET=/mnt/loopvenus #path where file should be loop mounted to, do not end path with /
MOUNTLINUX=sync,loop,rw,noatime,barrier=1 #mount options for linux fs

#backup config? files will be added to rar file with password on specified location
BACKUPCONFIG=yes #runs also if rdiff-backup is disabled above in backup options
RARBIN=/usr/bin/rar
RARPWD=$(cat /etc/.rar) # put just one line with password into that file
BACKUPCONFIGFOLDER=/Dokumente/debian #config will be stored in subfolder of Backupfolder, do not end path with /
RARFOLDER1="/etc" #folder to backup
RAR1=etc.rar #target for rar
RARFOLDER2="/usr/lib/nagios* /usr/share/nagios* /usr/share/logwatch*"
RAR2=usr.rar
RARFOLDER3="/var/vmail* /var/www* /var/lib/mysql"
RAR3=var.rar

#email?
EMAILDO=yes #old logs will be deleted if set to yes
EMAILTO=mail@intern.net

funct-mount ()
{
	if [ ! "$(mount | grep $SMBTARGET)" ]; then
	    echo "mount" $SMBTARGET
	    mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "mounting" $SMBTARGET "was successfull"
	    else
		echo "Error:" $SMBTARGET "could not be mounted!" >> $RDIFFLOG
	    fi
	else
	    echo $SMBTARGET "already mounted"
	fi
	
	if [ ! "$(mount | grep $LOOPTARGET)" ]; then
	    echo "mount" $LOOPTARGET
	    sleep 20
	    mount -t $MOUNTFS -o $MOUNTLINUX $SMBTARGET/$BACKUPFILE $LOOPTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "mounting" $LOOPTARGET "was successfull"
	    else
		echo "Error:" $LOOPTARGET "could not be mounted!" >> $RDIFFLOG
	    fi
	else
	    echo $LOOPTARGET "already mounted"
	fi
	
	sleep 3
	
	if [ ! "$(ps -A | grep rdiff-web)" ]; then
	    if test -e /etc/init.d/rdiff-web; then
		echo "starting rdiff-web"
		/etc/init.d/rdiff-web start
		if [ "$(ps -A | grep rdiff-web)" ]; then
		    echo "rdiff-web successfully started"
		    echo "you can start rdiff-web now on http://<host>:port, usually port 8080"
		else
		    echo "Error: rdiff-web could not be started!" >> $RDIFFLOG
		fi
	    else
		echo "Error: rdiff-web not installed!" >> $RDIFFLOG
	    fi
	else
	echo "rdiff-web already started"
	fi

}

funct-umount ()
{
	if [ "$(ps -A | grep rdiff-web)" ]; then
	    echo "stopping rdiff-web"
	    /etc/init.d/rdiff-web stop
	    sleep 3
	    if [ ! "$(ps -A | grep rdiff-web)" ]; then
		echo "rdiff-web is stopped now"
	    else
		echo "Error: rdiff-web could not be stopped!" >> $RDIFFLOG
	    fi
	else
	echo "rdiff-web already stopped"
	fi
	
	
	if [ "$(mount | grep $LOOPTARGET)" ]; then
	    echo "umount" $LOOPTARGET
	    sleep 3
	    umount $LOOPTARGET
	    if [ "$(mount | grep $LOOPTARGET)" ]; then
		echo "Error:" $LOOPTARGET "is still mounted!" >> $RDIFFLOG
	    else
	    echo "umounting" $LOOPTARGET "was successfull"
	    fi
	fi
	
	if [ "$(mount | grep $SMBTARGET)" ]; then
	    echo "umount" $SMBTARGET
	    sleep 20
	    umount $SMBTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "Error:" $SMBTARGET "is still mounted!" >> $RDIFFLOG
	    else
		echo "unmounting" $SMBTARGET "was successfull"
	    fi
	fi

}

case "$1" in
    backup)

    
    if [ "${EMAILDO,,}" == "yes" ]; then
	#delete old log, otherwise old logs will be mailed everytime again
    	if test -e $RDIFFLOG; then
    	    rm $RDIFFLOG
    	fi
    fi


    if [ "${BACKUPCONFIG,,}" == "yes" ]; then
    	#add information to backup log
    	echo "The following folders: $RARFOLDER1, $RARFOLDER2, $RARFOLDER3 were compressed (rar) to $BACKUPFOLDER$BACKUPCONFIGFOLDER see log below" >> $RDIFFLOG
	#rar various config folders to backup folder	
	if test -e $RARBIN; then
	    $RARBIN u -hp$RARPWD -ow -rr5% $BACKUPFOLDER$BACKUPCONFIGFOLDER/$RAR1 $RARFOLDER1 1>> $RDIFFLOG
	    $RARBIN u -hp$RARPWD -ow -rr5% $BACKUPFOLDER$BACKUPCONFIGFOLDER/$RAR2 $RARFOLDER2 1>> $RDIFFLOG
	    $RARBIN u -hp$RARPWD -ow -rr5% $BACKUPFOLDER$BACKUPCONFIGFOLDER/$RAR3 $RARFOLDER3 1>> $RDIFFLOG
	else
	echo "Error:" $RARBIN "not found!" >> $RDIFFLOG
	echo "Your system reports it at" && which rar >> $RDIFFLOG
	fi
	echo "-----------------------------------------------------------------------------------------------------------------------------" >> $RDIFFLOG
	echo "-----------------------------------------------------------------------------------------------------------------------------" >> $RDIFFLOG
    fi

    if [ "${BACKUPRDIFF,,}" == "yes" ]; then
	if [ "$(ping -c 1 $SMBSOURCE | grep "1 received")" ]; then
	    funct-mount
    	    if [ "$(mount | grep $LOOPTARGET)" ]; then
    		# add information to log
    		echo $(date)": rdiff-backup of $BACKUPFOLDER to //$SMBSOURCE/$SMBSHARE/$BACKUPFILE started. $BACKUPFILE contains an $MOUNTFS filesystem." >> $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log
    		# delete older than defined in variable FILEOT
    		if test -e $RDIFFBIN; then
    		    $RDIFFBIN -v$LOGLEVEL --remove-older-than $FILEOT $LOOPTARGET/data/
		    # start backup
    		    $RDIFFBIN -v$LOGLEVEL $BACKUPFOLDER/ $LOOPTARGET$BACKUPFOLDER/
		else
		    echo "Error:" $RDIFFBIN "not found!" >> $RDIFFLOG
		    echo "Your system reports it at" && which rdiff-backup >> $RDIFFLOG
		fi
		# copy rdiff-backup log to script logfile because will not be available when unmounted
    		cat $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log >> $RDIFFLOG
    		if [ "${EMAILDO,,}" == "yes" ]; then
    		    # delete backup log, otherwise old logs get mailed again
    		    rm $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log
    		fi
    	    else
    		echo "Error:" $LOOPTARGET "not mounted, cannot continue with rdiff-backup!" >> $RDIFFLOG
    	    fi
    	    
    	    funct-umount
	fi
    fi
    
    if [ "${EMAILDO,,}" == "yes" ]; then
    	# mail file
    	if mail -s "[rdiffback.sh] - Backup completed" $EMAILTO < $RDIFFLOG; then
    	    echo "Mail sent to " $EMAILTO >> $RDIFFLOG
    	else
    	    echo "Error: Mail could not be sent!" >> $RDIFFLOG
    	fi
    fi

    ;;

    mount)

    funct-mount
    ;;

    umount)
    funct-umount
    ;;

    showlog)
    less $RDIFFLOG
    ;;
    
    createloop)
    echo -n "WARNING: THIS WILL CREATE NEW BACKUPFILE:" $BACKUPFILE "ON:" $SMBSOURCE"/"$SMBSHARE", REALLY DO IT (yn]?"
    read response
    if [[ $response =~ y|Y ]]; then
	echo "do clean umount to make sure there is no loopmount active"
	funct-umount
	
     if [ ! "$(mount | grep $SMBTARGET)" ]; then
	    echo "mount" $SMBTARGET
	    mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
	
	if [ "$(mount | grep $SMBTARGET)" ]; then
	    
		if test -e $SMBTARGET/$BACKUPFILE; then
		    echo -n -e "\033[41m WARNING:" $BACKUPFILE "ALREADY EXISTS, DO YOU REALLY WANT TO OVERWRITE IT (Type in the word AGREE)? \033[0m"
		    read response2
		    if [[ $response2 =~ agree|AGREE ]]; then
			echo -n "How big the" $MOUNTFS "formatted file should be, enter in MB (this operation can take a while) ?"
			read response3
			if [ -z "$(echo $response3|tr -d '[:digit:]')" ]; then
		    
			    if [ "$(mount | grep $LOOPTARGET)" ]; then
				echo "Error:" $LOOPTARGET "is still mounted, cannot continue" >> $RDIFFLOG
			    else
				echo "Overwrite File" $BACKUPFILE "this may take a while, do not cancel"
				dd if=/dev/zero of=$SMBTARGET/$BACKUPFILE bs=1M count=$response3
		
				if test -e $SMBTARGET/$BACKUPFILE; then
				    echo "Format existing" $BACKUPFILE " with Filesystem" $MOUNTFS
				    mkfs.$MOUNTFS $SMBTARGET/$BACKUPFILE
				    echo "Everything finished!"
				else
				    echo "Error:" $BACKUPFILE "could not be created!" >> $RDIFFLOG
				fi
			    fi
			else
			    echo "Canceled, value is not in digits"
			fi
		    else
		    echo "Operation Canceled on user request"
		    fi
		else
		    echo "Couldnt find existing backupfile" $BACKUPFILE "so create new one, hold on this may take a while"
		    #dd if=/dev/zero of=$SMBTARGET/$BACKUPFILE bs=1M count=$response3
		    
		    if test -e $SMBTARGET/$BACKUPFILE; then
			echo "Format new file " $BACKUPFILE "with Filesystem" $MOUNTFS
			mkfs.$MOUNTFS $SMBTARGET/$BACKUPFILE
			echo "Everything finished!"
		    else
			echo "Error:" $BACKUPFILE "could not be created!" >> $RDIFFLOG
		    fi
		fi
	else
	    echo "Error:" $SMBTARGET "could not be mounted, cannot continue!" >> $RDIFFLOG
	fi
	
     else
	echo "Error: There was an error during umount" >> $RDIFFLOG
     fi

    else
	echo "Operation Canceled on user request"
    fi
    
    funct-umount
    ;;

    *)

	echo
	echo "rdiffback.sh"
	echo 
	echo "Script uses rdiff-backup in order to backup to an SMB share."
	echo "Because Windows cannot handle different files with lower/upper case, a loop mount to an linux fs will be used."
	echo
	echo "Instructions: Create on SMB a big file, format it with linux file system and change script parameters, before using it."
	echo "You can also start the script with the createloop option, to create the loop mount file."
	echo 
	echo "If you have problem with corrupted backups, check output of /var/log/kern.log."
	echo
	echo "Usage: ./rdiffback.sh {backup|createloop|mount|umount|showlog}"
	exit 1
	;;

esac
 
Update: Neue Version 1.3.
Jetzt auch mit Sicherung auf andere Linux/Unix Hosts (Linux -> Solaris funktioniert bestens). Zusätzlich habe ich eine Routine eingebaut die nicht nur prüft ob der andere Host up ist (ping), sondern das Backup läuft nur maximal 1x pro Tag. Es wird das aktuelle Datum mit dem Datum vom letzten Log verglichen.
Damit lässt sich auch auf Hosts sichern, die nicht regelmäßig an sind - was ja @home bei einem Singleserver öfter vorkommt.
Dazu braucht man das Script nur in bestimmten Zeitabständen per cron starten z.B. stündlich zu Linux/Unix: /etc/rdiff-back.sh backupssh oder zu Windows: /etc/rdiff-back.sh backupwin

Code:
#!/bin/bash
#rdiffback.sh
#version 1.3
#by xrated 07/2012

########################################################

#global backup options
BACKUPRDIFF=yes #start rdiff-backup? if set to no, backupconfig can still be done
FILEOT=1Y #delete files older than
BACKUPFOLDER=/data #folder to backup on local machine, do not end path with /
LOGLEVEL=5 #loglevel 1-9, 3 is default, 5 is showing files
SCRIPTLOC=/etc #where is this script stored? do not end path with /
RDIFFLOGWIN=/var/log/rdiffback-win.log
RDIFFLOGSSH=/var/log/rdiffback-ssh.log
RDIFFLOGCONFIG=/var/log/rdiffback-config.log
RDIFFLOGLOOP=/var/log/rdiffback-createloop.log
RDIFFBIN=/usr/bin/rdiff-backup

#########################################################

#CIFS share as target for backup
#Section is used only when backupwin option is used!
SMBSOURCE=192.168.2.104 # host or ip
SMBSHARE=rdiff
CREDFILE=/etc/.smb #contains user,password for smb mount separated by new line: username=youruser password=yourpassword
MOUNTCIFS=rw,hard,forcedirectio,mapchars,nocase,iocharset=utf8,dir_mode=0660,file_mode=0660,credentials=$CREDFILE
SMBTARGET=/mnt/smbvenus #where SMB share should be mounted to, do not end path with /
#Loopmount (contains linux fs)
BACKUPFILE=backup.ext3 #backup file on SMB share
MOUNTFS=ext3 #linux filesystem for loopmount
LOOPTARGET=/mnt/loopvenus #path where file should be loop mounted to, do not end path with /
MOUNTLINUX=sync,loop,rw,noatime,barrier=1 #mount options for linux fs
RDIFFWEBUNLOAD=yes #unload rdiff-web before umount, otherwise umount fails

##########################################################

#Linux as backup target 
#Section is used only when backupssh option is used!
#as preperation you have to run once (ssh command has no password option):
#ssh-keygen -t rsa as specific user $LINUXUSER on local host
#copy (scp) the pub file from ~/.ssh to other server into ~/.ssh/authorized_keys file 
#chmod 600 key files and authorized_keys file on both systems afterwards
#now it should be possible to ssh into other system without password
LINUXHOST=192.168.2.3
LINUXUSER=trechber #user for ssh (normaly root not allowed for ssh)
LINUXFOLDER=/data1/public/rdiff #backup target path
SCPBIN=/usr/bin/scp # path is local
SSHBIN=/usr/bin/ssh # path is local

###########################################################

#backup config? files will be added to rar file with password on specified location
#Section is used only when backupconfig option is used!
RARBIN=/usr/bin/rar
RARPWD=$(cat /etc/.rar) # put just one line with password into that file
BACKUPCONFIGFOLDER=/Dokumente/debian #config will be stored in subfolder of Backupfolder (specified in general options), do not end path with /
RARFOLDER1="/etc" #folder to backup
RAR1=etc.rar #target for rar
RARFOLDER2="/usr/lib/nagios* /usr/share/nagios* /usr/share/logwatch*"
RAR2=usr.rar
RARFOLDER3="/var/vmail* /var/www* /var/lib/mysql"
RAR3=var.rar

###########################################################

#email?
# older logs will be deleted if enabled
EMAILWIN=yes # email when rdiff-backup backups to windows host
EMAILSSH=yes # email when rdiff-backup backups to linux/unix host
EMAILCONFIG=yes # email log of backup config
EMAILTO=root

###########################################################

funct-mount ()
{
	if [ ! "$(mount | grep $SMBTARGET)" ]; then
	    echo "mount" $SMBTARGET
	    mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "mounting" $SMBTARGET "was successfull"
	    else
		echo "Error:" $SMBTARGET "could not be mounted!" >> $RDIFFLOGWIN
	    fi
	else
	    echo $SMBTARGET "already mounted"
	fi
	
	if [ ! "$(mount | grep $LOOPTARGET)" ]; then
	    echo "mount" $LOOPTARGET
	    sleep 20
	    mount -t $MOUNTFS -o $MOUNTLINUX $SMBTARGET/$BACKUPFILE $LOOPTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "mounting" $LOOPTARGET "was successfull"
	    else
		echo "Error:" $LOOPTARGET "could not be mounted!" >> $RDIFFLOGWIN
	    fi
	else
	    echo $LOOPTARGET "already mounted"
	fi
	
	sleep 3
	
	if [ ! "$(ps -A | grep rdiff-web)" ]; then
	    if test -e /etc/init.d/rdiff-web; then
		echo "starting rdiff-web"
		/etc/init.d/rdiff-web start
		if [ "$(ps -A | grep rdiff-web)" ]; then
		    echo "rdiff-web successfully started"
		    echo "you can start rdiff-web now on http://<host>:port, usually port 8080"
		else
		    echo "Error: rdiff-web could not be started!" >> $RDIFFLOGWIN
		fi
	    else
		echo "Error: rdiff-web not installed!" >> $RDIFFLOGWIN
	    fi
	else
	echo "rdiff-web already started"
	fi

}

funct-umount ()
{
	if [ "${RDIFFWEBUNLOAD,,}" == "yes" ]; then
	    if [ "$(ps -A | grep rdiff-web)" ]; then
		echo "stopping rdiff-web"
		/etc/init.d/rdiff-web stop
		sleep 3
		if [ ! "$(ps -A | grep rdiff-web)" ]; then
		    echo "rdiff-web is stopped now"
		else
		    echo "Error: rdiff-web could not be stopped!" >> $RDIFFLOGWIN
		fi
	    else
	    echo "rdiff-web already stopped"
	    fi
	fi
	
	
	if [ "$(mount | grep $LOOPTARGET)" ]; then
	    echo "umount" $LOOPTARGET
	    sleep 3
	    umount $LOOPTARGET
	    if [ "$(mount | grep $LOOPTARGET)" ]; then
		echo "Error:" $LOOPTARGET "is still mounted!" >> $RDIFFLOGWIN
	    else
	    echo "umounting" $LOOPTARGET "was successfull"
	    fi
	fi
	
	if [ "$(mount | grep $SMBTARGET)" ]; then
	    echo "umount" $SMBTARGET
	    sleep 20
	    umount $SMBTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "Error:" $SMBTARGET "is still mounted!" >> $RDIFFLOGWIN
	    else
		echo "unmounting" $SMBTARGET "was successfull"
	    fi
	fi

}

funct-backupconfig ()

{

    
    if [ "${EMAILCONFIG,,}" == "yes" ]; then
	#delete old log, otherwise old logs will be mailed everytime again
    	if test -e $RDIFFLOGCONFIG; then
    	    rm $RDIFFLOGCONFIG
    	fi
    fi


    #add information to backup log
    echo "The following folders: $RARFOLDER1, $RARFOLDER2, $RARFOLDER3 were compressed (rar) to $BACKUPFOLDER$BACKUPCONFIGFOLDER see log below" >> $RDIFFLOGCONFIG
    #rar various config folders to backup folder	
    if test -e $RARBIN; then
        $RARBIN u -hp$RARPWD -ow -rr5% $BACKUPFOLDER$BACKUPCONFIGFOLDER/$RAR1 $RARFOLDER1 1>> $RDIFFLOGCONFIG
        $RARBIN u -hp$RARPWD -ow -rr5% $BACKUPFOLDER$BACKUPCONFIGFOLDER/$RAR2 $RARFOLDER2 1>> $RDIFFLOGCONFIG
        $RARBIN u -hp$RARPWD -ow -rr5% $BACKUPFOLDER$BACKUPCONFIGFOLDER/$RAR3 $RARFOLDER3 1>> $RDIFFLOGCONFIG
    else
        echo "Error:" $RARBIN "not found!" >> $RDIFFLOGCONFIG
        echo "Your system reports it at" && which rar >> $RDIFFLOGCONFIG
    fi


}



case "$1" in
    backupssh)
    
date "+%Y-%m-%d" > $SCRIPTLOC/rdiffbackactualdate
rdiffdate1=`head -n 1 $SCRIPTLOC/rdiffbackactualdate`
if test -e $RDIFFLOGSSH; then
ls $RDIFFLOGSSH --full-time | awk '{print $6}' > $SCRIPTLOC/rdiffbacklastrunssh
rdiffdate2=`head -n 1 $SCRIPTLOC/rdiffbacklastrunssh`
fi

if [ "$rdiffdate1" != "$rdiffdate2" ]; then
echo "OK: rdiffback has not been run today"


	if [ "$(ping -c 1 $LINUXHOST | grep "1 received")" ]; then
	echo "OK: remote host" $LINUXHOST "is reachable"
	echo "Wait 10min to be sure host is ready and not still booting up..."
	sleep 600
	
	    funct-backupconfig
    
	    if [ "${EMAILSSH,,}" == "yes" ]; then
		#delete old log, otherwise old logs will be mailed everytime again
    		if test -e $RDIFFLOGSSH; then
		    if ! rm $RDIFFLOGSSH; then
		    echo "Error: Could not delete old log!" >> $RDIFFLOGSSH
		    fi
    		fi
	    fi

	    if [ "${EMAILCONFIG,,}" == "yes" ]; then
	    cat $RDIFFLOGCONFIG >> $RDIFFLOGSSH
	    fi
    
		if test -e $RDIFFBIN; then
		    if sudo -u $LINUXUSER $RDIFFBIN -v$LOGLEVEL --remove-older-than $FILEOT $LINUXUSER@$LINUXHOST::$LINUXFOLDER; then
			if sudo -u $LINUXUSER $RDIFFBIN -v$LOGLEVEL $BACKUPFOLDER $LINUXUSER@$LINUXHOST::$LINUXFOLDER; then
			    if test -e $SCPBIN; then
				# copy backup log to local tmp folder (user has no rights on /var/log and its not always possible to use root over ssh)
				if sudo -u $LINUXUSER $SCPBIN $LINUXUSER@$LINUXHOST:$LINUXFOLDER/rdiff-backup-data/backup.log /tmp/backuprdifftemp.log; then
				    # move log from tmp to /var/log
				    if mv /tmp/backuprdifftemp.log $RDIFFLOGSSH; then
					if test -e $SSHBIN; then
					    # delete log on other system
					    if ! sudo -u $LINUXUSER $SSHBIN -l $LINUXUSER $LINUXHOST "rm $LINUXFOLDER/rdiff-backup-data/backup.log"; then
					    echo "Error: Backup log could not be deleted on other host!" >> $RDIFFLOGSSH
					    fi
					else
					    echo "Error:" $SSHBIN "not found!" >> $RDIFFLOGSSH
					fi
				    else
					echo "Error: Backup log could not be moved!" >> $RDIFFLOGSSH
				    fi
				else
				    echo "Error: Backup log could not be copied from other host!" >> $RDIFFLOGSSH
				fi
			    else
				echo "Error:" $SCPBIN "not found!" >> $RDIFFLOGSSH
			    fi
			else
			    echo "Error: Rdiff-backup could not run" >> $RDIFFLOGSSH
			fi
		    else
			echo "Error: Rdiff-backup could not run --remove-older-than job" >> $RDIFFLOGSSH
		    fi
		
		else
		    echo "Error:" $RDIFFBIN "not found!" >> $RDIFFLOGSSH
	    	    echo "Your system reports it at" && which rdiff-backup >> $RDIFFLOGSSH
		fi
	

    
	    if [ "${EMAILSSH,,}" == "yes" ]; then
    		# mail file
    		if mail -s "[rdiffback.sh] Rdiff-backup $(hostname) to $LINUXHOST completed" $EMAILTO < $RDIFFLOGSSH; then
    		    echo "Mail sent to " $EMAILTO >> $RDIFFLOGSSH
    		else
    		    echo "Error: Mail could not be sent!" >> $RDIFFLOGSSH
    		fi
	    fi

	else
	    echo "Error: remote host" $LINUXHOST "is not reachable!"
	fi

else
echo "Error: rdiffback did run alreaday today!"
fi


    ;;

    backupconfig)
    
    funct-backupconfig

    if [ "${EMAILCONFIG,,}" == "yes" ]; then
    	# mail file
    	if mail -s "[rdiffback.sh] - RAR Backup on $(hostname) completed" $EMAILTO < $RDIFFLOGCONFIG; then
    	    echo "Mail sent to " $EMAILTO >> $RDIFFLOGCONFIG
    	else
    	    echo "Error: Mail could not be sent!" >> $RDIFFLOGCONFIG
    	fi
    fi


    ;;


    backupwin)

date "+%Y-%m-%d" > $SCRIPTLOC/rdiffbackactualdate
rdiffdate1=`head -n 1 $SCRIPTLOC/rdiffbackactualdate`
if test -e $RDIFFLOGWIN; then
ls $RDIFFLOGWIN --full-time | awk '{print $6}' > $SCRIPTLOC/rdiffbacklastrunwin
rdiffdate2=`head -n 1 $SCRIPTLOC/rdiffbacklastrunwin`
fi

if [ "$rdiffdate1" != "$rdiffdate2" ]; then
    echo "OK: rdiffback has not been run today"


    if [ "$(ping -c 1 $SMBSOURCE | grep "1 received")" ]; then
	echo "OK: remote host" $SMBSOURCE "is reachable"
	echo "Wait 10min to be sure host is ready and not still booting up..."
	sleep 600


	    funct-backupconfig

	    if [ "${EMAILWIN,,}" == "yes" ]; then
		#delete old log, otherwise old logs will be mailed everytime again
    		if test -e $RDIFFLOGWIN; then
    		    if ! rm $RDIFFLOGWIN; then
    		    echo "Error: Could not delete old log!" >> $RDIFFLOGWIN
    		    fi
    		fi
	    fi

	    if [ "${EMAILCONFIG,,}" == "yes" ]; then
		cat $RDIFFLOGCONFIG >> $RDIFFLOGWIN
	    fi

	    funct-mount
    	    if [ "$(mount | grep $LOOPTARGET)" ]; then
    		# add information to log
    		echo $(date)": rdiff-backup of $BACKUPFOLDER to //$SMBSOURCE/$SMBSHARE/$BACKUPFILE started. $BACKUPFILE contains an $MOUNTFS filesystem." >> $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log
    		# delete older than defined in variable FILEOT
    		if test -e $RDIFFBIN; then
    			$RDIFFBIN -v$LOGLEVEL --remove-older-than $FILEOT $LOOPTARGET/data/
			# start backup
    			$RDIFFBIN -v$LOGLEVEL $BACKUPFOLDER/ $LOOPTARGET$BACKUPFOLDER/
		else
		    echo "Error:" $RDIFFBIN "not found!" >> $RDIFFLOGWIN
		    echo "Your system reports it at" && which rdiff-backup >> $RDIFFLOGWIN
		fi
		# copy rdiff-backup log to script logfile because will not be available when unmounted
    		cat $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log >> $RDIFFLOGWIN
    		if [ "${EMAILWIN,,}" == "yes" ]; then
    		    # delete backup log, otherwise old logs get mailed again
    		    if ! rm $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log; then
    		    echo "Error: Could not delete old log on loopmount!" >> $RDIFFLOGWIN
    		    fi
    		fi
    	    else
    		echo "Error:" $LOOPTARGET "not mounted, cannot continue with rdiff-backup!" >> $RDIFFLOGWIN
    	    fi
    	    
    	    funct-umount
    
    
	    if [ "${EMAILWIN,,}" == "yes" ]; then
    		# mail file
    		if mail -s "[rdiffback.sh] - Rdiff-backup $(hostname) to $SMBSOURCE completed" $EMAILTO < $RDIFFLOGWIN; then
    		    echo "Mail sent to " $EMAILTO >> $RDIFFLOGWIN
    		else
    		    echo "Error: Mail could not be sent!" >> $RDIFFLOGWIN
    		fi
	    fi

    else
	echo "Error: remote host" $SMBSOURCE "is not reachable!"
    fi

else
echo "Error: rdiffback did run alreaday today!"
fi

    ;;

    mount)

    funct-mount
    ;;

    umount)
    funct-umount
    ;;

    
    createloop)
    echo -n "WARNING: THIS WILL CREATE NEW BACKUPFILE:" $BACKUPFILE "ON:" $SMBSOURCE"/"$SMBSHARE", REALLY DO IT (yn]?"
    read response
    if [[ $response =~ y|Y ]]; then
	echo "do clean umount to make sure there is no loopmount active"
	funct-umount
	
     if [ ! "$(mount | grep $SMBTARGET)" ]; then
	    echo "mount" $SMBTARGET
	    mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
	
	if [ "$(mount | grep $SMBTARGET)" ]; then
	    
		if test -e $SMBTARGET/$BACKUPFILE; then
		    echo -n -e "\033[41m WARNING:" $BACKUPFILE "ALREADY EXISTS, DO YOU REALLY WANT TO OVERWRITE IT (Type in the word AGREE)? \033[0m"
		    read response2
		    if [[ $response2 =~ agree|AGREE ]]; then
			echo -n "How big the" $MOUNTFS "formatted file should be, enter in MB (this operation can take a while) ?"
			read response3
			if [ -z "$(echo $response3|tr -d '[:digit:]')" ]; then
		    
			    if [ "$(mount | grep $LOOPTARGET)" ]; then
				echo "Error:" $LOOPTARGET "is still mounted, cannot continue" >> $RDIFFLOGLOOP
			    else
				echo "Overwrite File" $BACKUPFILE "this may take a while, do not cancel"
				dd if=/dev/zero of=$SMBTARGET/$BACKUPFILE bs=1M count=$response3
		
				if test -e $SMBTARGET/$BACKUPFILE; then
				    echo "Format existing" $BACKUPFILE " with Filesystem" $MOUNTFS
				    mkfs.$MOUNTFS $SMBTARGET/$BACKUPFILE
				    echo "Everything finished!"
				else
				    echo "Error:" $BACKUPFILE "could not be created!" >> $RDIFFLOGLOOP
				fi
			    fi
			else
			    echo "Canceled, value is not in digits"
			fi
		    else
		    echo "Operation Canceled on user request"
		    fi
		else
		    echo "Couldnt find existing backupfile" $BACKUPFILE "so create new one, hold on this may take a while"
		    dd if=/dev/zero of=$SMBTARGET/$BACKUPFILE bs=1M count=$response3
		    
		    if test -e $SMBTARGET/$BACKUPFILE; then
			echo "Format new file " $BACKUPFILE "with Filesystem" $MOUNTFS
			mkfs.$MOUNTFS $SMBTARGET/$BACKUPFILE
			echo "Everything finished!"
		    else
			echo "Error:" $BACKUPFILE "could not be created!" >> $RDIFFLOGLOOP
		    fi
		fi
	else
	    echo "Error:" $SMBTARGET "could not be mounted, cannot continue!" >> $RDIFFLOGLOOP
	fi
	
     else
	echo "Error: There was an error during umount" >> $RDIFFLOGLOOP
     fi

    else
	echo "Operation Canceled on user request"
    fi
    
    funct-umount
    ;;

    *)

	echo
	echo -e "\033[1mrdiffback.sh \033[0m"
	echo "Usage: ./rdiffback.sh {backupssh|backupwin|backupconfig|createloop|mount|umount}"
	echo "Options:"
	echo -e "\033[1mbackupssh\033[0m: rdiff-backup to other linux/unix host over ssh."
	echo -e "\033[1mbackupwin\033[0m: rdiff-backup to other win host over CIFS/SMB."
	echo "Because Windows cannot handle different files with lower/upper case, a loop mount" 
	echo "to an linux fs on that CIFS share will be used."
	echo "Instructions: Create on SMB a big file, format it with linux file system and change" 
	echo "script parameters, before using it. Or start script with createloop option."
	echo "If you have problem with corrupted backups, check output of /var/log/kern.log,"
	echo "Normaly this is related to linux fs mount options."
	echo -e "\033[1mbackupconfig\033[0m: RAR specified folders to local file system (included also in above options)."
	echo -e "\033[1mcreateloop\033[0m: Starts prompted script for creation of Linux filesystem on SMB Share."
	echo -e "\033[1mmount\033[0m: Mounts SMB Share and loop mounts an Linux filesystem on it."
	echo -e "\033[1mumount\033[0m: Same as above with unmount and unloading of Rdiff-web."
	exit 1
	;;

esac
 
Neue Version 1.4

- Bug entfernt: Im Abschnitt backupwin war beim remove-older-than der Pfad $LINUXFOLDER/data eingetragen statt $LINUXFOLDER$BACKUPFOLDER.
- Bug entfernt: Im Abschnitt backupssh wurde das rar log überschrieben von rdiff-backup
- Das SSH Backup wird nun in einen Folder gleichnamig wie die Quelle gespeichert, nicht mehr direkt in den NFS mount (bitte Backupdaten umkopieren, falls vorher ältere Version verwendet wird)
- Alte Logs werden nun nicht mehr gelöscht sondern verschoben mit Datumsangabe.
- Wenn rdiff-backup beim ausführen einen Errorlevel ausgibt dann steht in der Mail "... had errors" statt "... completed".
Allerdings geht das nicht bei rar, weil der einen Fehler ausgibt wenn es keine neuen Files zu sichern gibt.
Desweiteren ist es dann bei rdiff-backup Fehlerstatus, automatisch möglich den Job dann am selben Tag nochmals zu starten.
- Neuer Force switch der es ermöglicht, den Job mehrmals pro Tag zu starten.
- Es ist nun möglich das SSH Backup Target zu mounten (mount ssh), damit lokal rdiff-web funktioniert.

Code:
#!/bin/bash
#rdiffback.sh
PROGVER=1.4
#by xrated 07/2012

########################################################

#global backup options
FILEOT=1Y #delete files older than
BACKUPFOLDER=/data #folder to backup on local machine, do not end path with /
LOGLEVEL=5 #loglevel 1-9, 3 is default, 5 is showing files
LOGPATH=/var/log/rdiffback
RDIFFLOGCONFIG=rdiffback-config #logfile
RDIFFBIN=/usr/bin/rdiff-backup
RDIFFWEBUNLOAD=yes #unload rdiff-web before umount, otherwise umount fails

#########################################################

# Option for createloop option
RDIFFLOGLOOP=rdiffback-createloop

########################################################

#CIFS share as target for backup
#Section is used only when backupwin option is used!
TIMERVALWIN=1 # seconds to wait before backup starts, to be sure that remote host is up and running (in case you are not sure if its really up because it may still load its services after a successfull ping
RDIFFLOGWIN=rdiffback-win
RDIFFWINDAYCHECK=yes # if set to no, its possible to run the backup again on the same day - yes may be handy, if you dont know when the remote host is up and the script is started multiple times per day via cron

SMBSOURCE=192.168.2.101 # host or ip
SMBSHARE=rdiff
CREDFILE=/etc/.smb #contains user,password for smb mount separated by new line: username=youruser password=yourpassword
MOUNTCIFS=rw,hard,forcedirectio,mapchars,nocase,iocharset=utf8,dir_mode=0660,file_mode=0660,credentials=$CREDFILE
SMBTARGET=/mnt/smbvenus #where SMB share should be mounted to, do not end path with /, make sure this folder exists
#Loopmount (contains linux fs)
BACKUPFILE=backup.ext3 #backup file on SMB share, this is where all the files go into
MOUNTFS=ext3 #linux filesystem for loopmount
LOOPTARGET=/mnt/loopvenus #path where file should be loop mounted to, do not end path with /
MOUNTLINUX=sync,loop,rw,noatime,barrier=1 #mount options for linux fs

##########################################################

#Linux/Unix as backup target 
#Section is used only when backupssh option is used!
#as preperation you have to run once (ssh command has no password option):
#ssh-keygen -t rsa as specific user $LINUXUSER on local host
#copy (scp) the pub file from ~/.ssh to other server into ~/.ssh/authorized_keys file 
#chmod 600 key files and authorized_keys file on both systems afterwards
#now it should be possible to ssh into other system without password
TIMERVALSSH=600 # seconds to wait before backup starts, to be sure that remote host is up and running (in case you are not sure if its really up because it may still load its services after a successfull ping
RDIFFLOGSSH=rdiffback-ssh
RDIFFSSHDAYCHECK=yes # if set to no, its possible to run the backup again on the same day - yes may be handy, if you dont know when the remote host is up and the script is started multiple times per day via cron

LINUXHOST=192.168.2.3
LINUXUSER=trechber #user for ssh (normaly root not allowed for ssh)
LINUXFOLDER=/data1/public/rdiff #backup target path
LINUXTARGET=/mnt/nfssolaris #this is only to see backed up files (in rdiff-web)
SCPBIN=/usr/bin/scp # path is local
SSHBIN=/usr/bin/ssh # path is local

###########################################################

#backup config? files will be added to rar file with password on specified location
#Section is used only when backupconfig option is used!
BACKUPCONFIG=yes
RARBIN=/usr/bin/rar
RARPWD=$(cat /etc/.rar) # put just one line with password into that file
BACKUPCONFIGFOLDER=/Dokumente/debian #config will be stored in subfolder of Backupfolder (specified in general options), do not end path with /
RARFOLDER1="/etc" #folder to backup
RAR1=etc.rar #target for rar
RARFOLDER2="/usr/lib/nagios* /usr/share/nagios* /usr/share/logwatch*"
RAR2=usr.rar
RARFOLDER3="/var/vmail* /var/www* /var/lib/mysql"
RAR3=var.rar

###########################################################

#email logs?
EMAILWIN=yes # email when rdiff-backup backups to windows host
EMAILSSH=yes # email when rdiff-backup backups to linux/unix host
EMAILCONFIG=yes # email log of backup config, note this will only be used if script is started with backupconfig option
EMAILTO=root

###########################################################

# do not change everything below

RDIFFDAYCHECK=$2
PROGNAME=`basename $0`
PROGPATH=`pwd`
mounterror=0
backuperror=0

#readout actual date, needed to see if job has been run today
date "+%Y-%m-%d" > $PROGPATH/rdiffbackactualdate
rdiffdate1=`head -n 1 $PROGPATH/rdiffbackactualdate`

#check if logfolder exists
if ! test -e $LOGPATH; then
echo "Error: Specified logfolder does not exist!"
fi

funct-mount-win ()
{
	if [ ! "$(mount | grep $SMBTARGET)" ]; then
	    echo "mount" $SMBTARGET
	    mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "mounting" $SMBTARGET "was successfull"
	    else
		echo "Error:" $SMBTARGET "could not be mounted!" >> $LOGPATH/$LOGPATH/$RDIFFLOGWIN
		mounterror=1
	    fi
	else
	    echo $SMBTARGET "already mounted"
	fi
	
	if [ ! "${mounterror,,}" == "1" ]; then
	    if [ ! "$(mount | grep $LOOPTARGET)" ]; then
		echo "mount" $LOOPTARGET
		sleep 1
		mount -t $MOUNTFS -o $MOUNTLINUX $SMBTARGET/$BACKUPFILE $LOOPTARGET
		if [ "$(mount | grep $SMBTARGET)" ]; then
		    echo "mounting" $LOOPTARGET "was successfull"
		else
		    echo "Error:" $LOOPTARGET "could not be mounted!" >> $LOGPATH/$RDIFFLOGWIN
		    mounterror=1
		fi
	    else
		echo $LOOPTARGET "already mounted"
	    fi
	fi
}

funct-mount-ssh ()
{
	if [ ! "$(mount | grep $LINUXTARGET)" ]; then
	    echo "mount" $LINUXTARGET
	    mount -t nfs $LINUXHOST:$LINUXFOLDER $LINUXTARGET
	    if [ "$(mount | grep $LINUXTARGET)" ]; then
		echo "mounting" $LINUXTARGET "was successfull"
	    else
		echo "Error:" $LINUXTARGET "could not be mounted!" >> $LOGPATH/$RDIFFLOGSSH
		mounterror=1
	    fi
	else
	    echo $LINUXTARGET "already mounted"
	fi
}

funct-mount ()
{
	case "$1" in
	    ssh)
	    funct-mount-ssh
	    ;;
	    win)
	    funct-mount-win
	    ;;
	    *)
	    ;;
	esac
	
	sleep 1
	
	if [ ! "${mounterror,,}" == "1" ]; then
	    if [ ! "$(ps -A | grep rdiff-web)" ]; then
		if test -e /etc/init.d/rdiff-web; then
		    /etc/init.d/rdiff-web start
		    if [ "$(ps -A | grep rdiff-web)" ]; then
			echo "rdiff-web successfully started"
			echo "you can start rdiff-web now on http://<thishostorip>:port, usually port 8080"
		    else
			echo "Error: rdiff-web could not be started!" 
		    fi
		else
		    echo "Error: rdiff-web not installed!"
		fi
	    else
	    echo "rdiff-web already started"
	    fi
	fi
}

funct-umount-win ()
{
	if [ "$(mount | grep $LOOPTARGET)" ]; then
	    echo "umount" $LOOPTARGET
	    sleep 1
	    umount $LOOPTARGET
	    if [ "$(mount | grep $LOOPTARGET)" ]; then
		echo "Error:" $LOOPTARGET "is still mounted!" >> $LOGPATH/$RDIFFLOGWIN
		mounterror=1
	    else
	    echo "umounting" $LOOPTARGET "was successfull"
	    fi
	fi
	
	if [ ! "${mounterror,,}" == "1" ]; then
	if [ "$(mount | grep $SMBTARGET)" ]; then
	    echo "umount" $SMBTARGET
	    sleep 1
	    umount $SMBTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "Error:" $SMBTARGET "is still mounted!" >> $LOGPATH/$RDIFFLOGWIN
	    else
		echo "unmounting" $SMBTARGET "was successfull"
	    fi
	fi
	fi
}

funct-umount-ssh ()
{
	if [ "$(mount | grep $LINUXTARGET)" ]; then
	    echo "umount" $LINUXTARGET
	    sleep 1
	    umount $LINUXTARGET
	    if [ "$(mount | grep $LINUXTARGET)" ]; then
		echo "Error:" $LINUXTARGET "is still mounted!" >> $LOGPATH/$RDIFFLOGSSH
	    else
	    echo "umounting" $LINUXTARGET "was successfull"
	    fi
	fi
}


funct-umount ()
{
	if [ "${RDIFFWEBUNLOAD,,}" == "yes" ]; then
	    if [ "$(ps -A | grep rdiff-web)" ]; then
		/etc/init.d/rdiff-web stop
		sleep 1
		if [ ! "$(ps -A | grep rdiff-web)" ]; then
		    echo "rdiff-web is stopped now"
		else
		    echo "Error: rdiff-web could not be stopped!"
		fi
	    else
	    echo "rdiff-web already stopped"
	    fi
	fi
	
	case "$1" in
	    win)
	    funct-umount-win
	    ;;
	    ssh)
	    funct-umount-ssh
	    ;;
	    *)
	    ;;
	esac

}

funct-backupconfig ()

{

    
    if [ "${EMAILCONFIG,,}" == "yes" ]; then
	#move old log, otherwise old logs will be mailed everytime again
    	if test -e $LOGPATH/$RDIFFLOGCONFIG; then
    	    mv $LOGPATH/$RDIFFLOGCONFIG $LOGPATH/$RDIFFLOGCONFIG-$rdiffdate1
    	fi
    fi


    #add information to backup log
    echo "The following folders: $RARFOLDER1, $RARFOLDER2, $RARFOLDER3 were compressed (rar) to $BACKUPFOLDER$BACKUPCONFIGFOLDER see error log below" >> $LOGPATH/$RDIFFLOGCONFIG
    #rar various config folders to backup folder	
    if test -e $RARBIN; then
        if $RARBIN u -hp$RARPWD -ow -rr5% $BACKUPFOLDER$BACKUPCONFIGFOLDER/$RAR1 $RARFOLDER1 >> $LOGPATH/$RDIFFLOGCONFIG; then
    	    echo . >> $LOGPATH/$RDIFFLOGCONFIG
    	    echo "RAR OK: $RARFOLDER1" >> $LOGPATH/$RDIFFLOGCONFIG
        else
    	    echo "Error (maybe just no new files?): $RARFOLDER1" >> $LOGPATH/$RDIFFLOGCONFIG
        fi
        if $RARBIN u -hp$RARPWD -ow -rr5% $BACKUPFOLDER$BACKUPCONFIGFOLDER/$RAR2 $RARFOLDER2 >> $LOGPATH/$RDIFFLOGCONFIG; then
    	    echo "RAR OK: $RARFOLDER2" >> $LOGPATH/$RDIFFLOGCONFIG
        else
    	    echo "Error (maybe just no new files?): $RARFOLDER2" >> $LOGPATH/$RDIFFLOGCONFIG
        fi
        if $RARBIN u -hp$RARPWD -ow -rr5% $BACKUPFOLDER$BACKUPCONFIGFOLDER/$RAR3 $RARFOLDER3 >> $LOGPATH/$RDIFFLOGCONFIG; then
    	    echo "RAR OK: $RARFOLDER3" >> $LOGPATH/$RDIFFLOGCONFIG
	else
	    echo "Error (maybe just no new files?): $RARFOLDER3" >> $LOGPATH/$RDIFFLOGCONFIG
	fi
    else
        echo "Error:" $RARBIN "not found!" >> $LOGPATH/$RDIFFLOGCONFIG
        echo "Your system reports it at" && which rar >> $LOGPATH/$RDIFFLOGCONFIG
	backuperror=1
    fi
    echo "#######################################################################################################################" >> $LOGPATH/$RDIFFLOGCONFIG

}

funct-backupsshdaycheck ()

{
if test -e $LOGPATH/$RDIFFLOGSSH; then
ls $LOGPATH/$RDIFFLOGSSH --full-time | awk '{print $6}' > $PROGPATH/rdiffbacklastrunssh
rdiffdate2=`head -n 1 $PROGPATH/rdiffbacklastrunssh`
fi

if [ "$rdiffdate1" != "$rdiffdate2" ]; then
echo "OK: rdiffback has not been run today"
backupssh=1
else
    if [ ! "$RDIFFDAYCHECK" == "--force" ]; then
	echo "INFO: rdiffback was already run today. If you want to run it again use --force switch"
    fi
backupssh=0
fi
}

funct-backupwindaycheck ()

{
if test -e $LOGPATH/$RDIFFLOGWIN; then
ls $LOGPATH/$RDIFFLOGWIN --full-time | awk '{print $6}' > $PROGPATH/rdiffbacklastrunwin
rdiffdate2=`head -n 1 $PROGPATH/rdiffbacklastrunwin`
fi

if [ "$rdiffdate1" != "$rdiffdate2" ]; then
echo "OK: rdiffback has not been run today"
backupwin=1
else
    if [ ! "$RDIFFDAYCHECK" == "--force" ]; then
	echo "INFO: rdiffback was already run today. If you want to run it again, use --force switch"
    fi	
backupwin=0

fi
}


case "$1" in
    backupssh)

funct-backupsshdaycheck

if [ "$backupssh" == "1" ] || [ "$RDIFFDAYCHECK" == "--force" ]; then

	if [ "$RDIFFDAYCHECK" == "--force" ]; then
	echo "INFO: --force switch is enabled, so the script is allowed to run multiple times per day"
	fi
    
	if [ "$(ping -c 1 $LINUXHOST | grep "1 received")" ]; then
	echo "OK: remote host" $LINUXHOST "is reachable"
	echo "Wait $TIMERVALSSH seconds to be sure host is ready and not still booting up..."
	sleep $TIMERVALSSH
	
    
	    #move old log, otherwise old logs will be mailed everytime again
    	    if test -e $LOGPATH/$RDIFFLOGSSH; then
	        if ! mv $LOGPATH/$RDIFFLOGSSH $LOGPATH/$RDIFFLOGSSH-$rdiffdate1; then
		    echo "Error: Could not move old log!" >> $LOGPATH/$RDIFFLOGSSH
	        fi
	    fi
	    
	    if [ "$BACKUPCONFIG" == "yes" ]; then
	    funct-backupconfig
	    cat $LOGPATH/$RDIFFLOGCONFIG >> $LOGPATH/$RDIFFLOGSSH
	    fi


    
		if test -e $RDIFFBIN; then
		    if sudo -u $LINUXUSER $RDIFFBIN -v$LOGLEVEL --remove-older-than $FILEOT $LINUXUSER@$LINUXHOST::$LINUXFOLDER$BACKUPFOLDER; then
			if sudo -u $LINUXUSER $RDIFFBIN -v$LOGLEVEL $BACKUPFOLDER $LINUXUSER@$LINUXHOST::$LINUXFOLDER$BACKUPFOLDER; then
			    if test -e $SCPBIN; then
				# copy backup log to local tmp folder (user has no rights on /var/log and its not always possible to use root over ssh)
				if sudo -u $LINUXUSER $SCPBIN $LINUXUSER@$LINUXHOST:$LINUXFOLDER$BACKUPFOLDER/rdiff-backup-data/backup.log /tmp/backuprdifftemp.log; then
				    # copy log from tmp to /var/log
				    if cat /tmp/backuprdifftemp.log >> $LOGPATH/$RDIFFLOGSSH; then
					if test -e $SSHBIN; then
					    # delete log on other system
					    if ! sudo -u $LINUXUSER $SSHBIN -l $LINUXUSER $LINUXHOST "rm $LINUXFOLDER$BACKUPFOLDER/rdiff-backup-data/backup.log"; then
					    echo "Error: Backup log could not be deleted on other host!" >> $LOGPATH/$RDIFFLOGSSH
					    fi
					else
					    echo "Error:" $SSHBIN "not found!" >> $LOGPATH/$RDIFFLOGSSH
					fi
				    else
					echo "Error: Backup log could not be moved!" >> $LOGPATH/$RDIFFLOGSSH
				    fi
				else
				    echo "Error: Backup log could not be copied from other host!" >> $LOGPATH/$RDIFFLOGSSH
				fi
			    else
				echo "Error:" $SCPBIN "not found!" >> $LOGPATH/$RDIFFLOGSSH
			    fi
			else
			    echo "Error: Rdiff-backup could not run" >> $LOGPATH/$RDIFFLOGSSH
			    backuperror=1
			fi
		    else
			echo "Error: Rdiff-backup could not run --remove-older-than job" >> $LOGPATH/$RDIFFLOGSSH
			backuperror=1
		    fi
		
		else
		    echo "Error:" $RDIFFBIN "not found!" >> $LOGPATH/$RDIFFLOGSSH
	    	    echo "Your system reports it at" && which rdiff-backup >> $LOGPATH/$RDIFFLOGSSH
		    backuperror=1
		fi
	

    
	    if [ "${EMAILSSH,,}" == "yes" ]; then
    		# mail file
		if [ "${backuperror,,}" == "1" ]; then
		
		    if mail -s "[$PROGNAME] Rdiff-backup $(hostname) to $LINUXHOST had errors" $EMAILTO < $LOGPATH/$RDIFFLOGSSH; then
    			echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGSSH
    		    else
    			echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGSSH
    		    fi
		
		else
		
    		    if mail -s "[$PROGNAME] Rdiff-backup $(hostname) to $LINUXHOST completed" $EMAILTO < $LOGPATH/$RDIFFLOGSSH; then
    			echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGSSH
    		    else
    			echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGSSH
    		    fi
    		fi
	    fi
	    
	    
	    if [ "${backuperror,,}" == "1" ]; then
		#move to old log, so we could run backup again on the same day (but only in case errors)
    		if test -e $LOGPATH/$RDIFFLOGSSH; then
    		    if ! mv $LOGPATH/$RDIFFLOGSSH $LOGPATH/$RDIFFLOGSSH-$rdiffdate1; then
    		        echo "Error: Could not move old log!" >> $LOGPATH/$RDIFFLOGSSH
    		    fi
    		fi
    	    fi

	else
	    echo "Error: remote host" $LINUXHOST "is not reachable!"
	fi



fi


    ;;

    backupconfig)
    
    funct-backupconfig

    if [ "${EMAILCONFIG,,}" == "yes" ]; then
    	# mail file
    	
    	if [ "${backuperror,,}" == "1" ]; then
	
    	
    	    if mail -s "[$PROGNAME] - RAR Backup on $(hostname) had errors" $EMAILTO < $LOGPATH/$RDIFFLOGCONFIG; then
    		echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGCONFIG
    	    else
    		echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGCONFIG
    	    fi
    
	else
	
	    if mail -s "[$PROGNAME] - RAR Backup on $(hostname) completed" $EMAILTO < $LOGPATH/$RDIFFLOGCONFIG; then
    		echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGCONFIG
    	    else
    		echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGCONFIG
    	    fi
    	    
	fi
    
    fi


    ;;


    backupwin)


funct-backupwindaycheck

if [ "$backupwin" == "1" ] || [ "$RDIFFDAYCHECK" == "--force" ]; then

    if [ "$RDIFFDAYCHECK" == "--force" ]; then
	echo "INFO: --force switch is enabled, so the script is allowed to run multiple times per day"
    fi


    if [ "$(ping -c 1 $SMBSOURCE | grep "1 received")" ]; then
	echo "OK: remote host" $SMBSOURCE "is reachable"
	echo "Wait $TIMERVALWIN seconds to be sure host is ready and not still booting up..."
	sleep $TIMERVALWIN

	    
	    #move old log, otherwise old logs will be mailed everytime again
    	    if test -e $LOGPATH/$RDIFFLOGWIN; then
    	        if ! mv $LOGPATH/$RDIFFLOGWIN $LOGPATH/$RDIFFLOGWIN-$rdiffdate1; then
    	        echo "Error: Could not move old log!" >> $LOGPATH/$RDIFFLOGWIN
    	        fi
    	    fi
    	    
    	    if [ "$BACKUPCONFIG" == "yes" ]; then
    	    funct-backupconfig
    	    cat $LOGPATH/$RDIFFLOGCONFIG >> $LOGPATH/$RDIFFLOGWIN
	    fi

	    funct-mount win
	    
    	    if [ "$(mount | grep $LOOPTARGET)" ]; then
    		# add information to log
    		echo $(date)": rdiff-backup of $BACKUPFOLDER to //$SMBSOURCE/$SMBSHARE/$BACKUPFILE started. $BACKUPFILE contains an $MOUNTFS filesystem." >> $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log
    		# delete older than defined in variable FILEOT
    		if test -e $RDIFFBIN; then
    			if $RDIFFBIN -v$LOGLEVEL --remove-older-than $FILEOT $LOOPTARGET$BACKUPFOLDER/; then
    			    echo "rdiff-backup OK: --remove-older-than $FILEOT $LOOPTARGET$BACKUPFOLDER/" >> $LOGPATH/$RDIFFLOGWIN
    			else
    			    backuperror=1
    			fi
			# start backup
    			if $RDIFFBIN -v$LOGLEVEL $BACKUPFOLDER/ $LOOPTARGET$BACKUPFOLDER/; then
    			    echo "rdiff-backup OK: $BACKUPFOLDER/ $LOOPTARGET$BACKUPFOLDER/"
    			else
    			    backuperror=1
    			fi
		else
		    echo "Error:" $RDIFFBIN "not found!" >> $LOGPATH/$RDIFFLOGWIN
		    echo "Your system reports it at" && which rdiff-backup >> $LOGPATH/$RDIFFLOGWIN
		    backuperror=1
		fi
		# copy rdiff-backup log to script logfile because will not be available when unmounted
    		cat $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log >> $LOGPATH/$RDIFFLOGWIN
    		if [ "${EMAILWIN,,}" == "yes" ]; then
    		    # delete backup log, otherwise old logs get mailed again
    		    if ! rm $LOOPTARGET$BACKUPFOLDER/rdiff-backup-data/backup.log; then
    		    echo "Error: Could not delete old log on loopmount!" >> $LOGPATH/$RDIFFLOGWIN
    		    fi
    		fi
    	    else
    		echo "Error:" $LOOPTARGET "not mounted, cannot continue with rdiff-backup!" >> $LOGPATH/$RDIFFLOGWIN
    		backuperror=1
    	    fi
    	    
	    funct-umount win
    
	    if [ "${EMAILWIN,,}" == "yes" ]; then
    		# mail file
    		
    		if [ "${backuperror,,}" == "1" ]; then
    		
    		    if mail -s "[$PROGNAME] - Rdiff-backup $(hostname) to $SMBSOURCE had errors" $EMAILTO < $LOGPATH/$RDIFFLOGWIN; then
    			echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGWIN    		    
    		    else
    			echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGWIN
    		    fi
    		
    		else
    		
    		    if mail -s "[$PROGNAME] - Rdiff-backup $(hostname) to $SMBSOURCE completed" $EMAILTO < $LOGPATH/$RDIFFLOGWIN; then
    			echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGWIN
    		    else
    			echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGWIN
    		    fi
    		
    		fi
	    fi

	    if [ "${backuperror,,}" == "1" ]; then
	        #move to old log, so we could run backup again on the same day (but only in case errors)
    		if test -e $LOGPATH/$RDIFFLOGWIN; then
    		    if ! mv $LOGPATH/$RDIFFLOGWIN $LOGPATH/$RDIFFLOGWIN-$rdiffdate1; then
    			echo "Error: Could not move old log!" >> $LOGPATH/$RDIFFLOGWIN
    		    fi
    		fi
    	    fi

    else
	echo "Error: remote host" $SMBSOURCE "is not reachable!"
    fi


fi

    ;;

    mount)

    	case "$2" in
	    win)
	    funct-mount win
	    ;;
	    ssh)
	    funct-mount ssh
	    ;;
	    *)
	    ;;
	esac

    ;;

    umount)
    
    	case "$2" in
	    win)
	    funct-umount win
	    ;;
	    ssh)
	    funct-umount ssh
	    ;;
	    *)
	    ;;
	esac

    ;;

    
    createloop)
    echo -n "WARNING: THIS WILL CREATE NEW BACKUPFILE:" $BACKUPFILE "ON:" $SMBSOURCE"/"$SMBSHARE", REALLY DO IT (yn]?"
    read response
    if [[ $response =~ y|Y ]]; then
	echo "do clean umount to make sure there is no loopmount active"
	funct-umount win
	
     if [ ! "$(mount | grep $SMBTARGET)" ]; then
	    echo "mount" $SMBTARGET
	    mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
	
	if [ "$(mount | grep $SMBTARGET)" ]; then
	    
		if test -e $SMBTARGET/$BACKUPFILE; then
		    echo -n -e "\033[41m WARNING:" $BACKUPFILE "ALREADY EXISTS, DO YOU REALLY WANT TO OVERWRITE IT (Type in the word AGREE)? \033[0m"
		    read response2
		    if [[ $response2 =~ agree|AGREE ]]; then
			echo -n "How big the" $MOUNTFS "formatted file should be, enter in MB (this operation can take a while) ?"
			read response3
			if [ -z "$(echo $response3|tr -d '[:digit:]')" ]; then
		    
			    if [ "$(mount | grep $LOOPTARGET)" ]; then
				echo "Error:" $LOOPTARGET "is still mounted, cannot continue" >> $LOGPATH/$RDIFFLOGLOOP
			    else
				echo "Overwrite File" $BACKUPFILE "this may take a while, do not cancel"
				dd if=/dev/zero of=$SMBTARGET/$BACKUPFILE bs=1M count=$response3
		
				if test -e $SMBTARGET/$BACKUPFILE; then
				    echo "Format existing" $BACKUPFILE " with Filesystem" $MOUNTFS
				    mkfs.$MOUNTFS $SMBTARGET/$BACKUPFILE
				    echo "Everything finished!"
				else
				    echo "Error:" $BACKUPFILE "could not be created!" >> $LOGPATH/$RDIFFLOGLOOP
				fi
			    fi
			else
			    echo "Canceled, value is not in digits"
			fi
		    else
		    echo "Operation Canceled on user request"
		    fi
		else
		    echo "Couldnt find existing backupfile" $BACKUPFILE "so create new one, hold on this may take a while"
		    dd if=/dev/zero of=$SMBTARGET/$BACKUPFILE bs=1M count=$response3
		    
		    if test -e $SMBTARGET/$BACKUPFILE; then
			echo "Format new file " $BACKUPFILE "with Filesystem" $MOUNTFS
			mkfs.$MOUNTFS $SMBTARGET/$BACKUPFILE
			echo "Everything finished!"
		    else
			echo "Error:" $BACKUPFILE "could not be created!" >> $LOGPATH/$RDIFFLOGLOOP
		    fi
		fi
	else
	    echo "Error:" $SMBTARGET "could not be mounted, cannot continue!" >> $LOGPATH/$RDIFFLOGLOOP
	fi
	
     else
	echo "Error: There was an error during umount" >> $LOGPATH/$RDIFFLOGLOOP
     fi

    else
	echo "Operation Canceled on user request"
    fi
    
    funct-umount win
    ;;

    *)

	echo
	echo -e "\033[33m\033[44m$PROGNAME \033[0m Version $PROGVER"
	echo "Makes it possible to backup a local folder to an remote Linux/Unix or Windows host."
	echo "Usage: $PROGNAME {backupssh (--force)|backupwin (--force)|backupconfig|createloop|mount(win|ssh)|umount(win|ssh)}"
	echo -e "\033[31mMake sure you change settings inside $PROGNAME to your needs before you run first backup!\033[0m"
	echo
	echo -e "\033[4mMain Options\033[0m"
	echo
	echo -e "\033[1mbackupssh\033[0m: rdiff-backup to other linux/unix host over ssh. Make sure the access is not denied on the remote host."
	echo -e "If --force is used, it can be run multiple times per day."
	echo -e "\033[1mbackupwin\033[0m: rdiff-backup to other win host over CIFS/SMB. Use --force to override daycheck routine."
	echo "Because Windows cannot handle different files with lower/upper case, a loop mount" 
	echo "to an linux fs on that CIFS share will be used."
	echo "Instructions: Create on SMB a big file, format it with linux file system and change" 
	echo "script parameters, before using it. Or start script with createloop option."
	echo "If you have problem with corrupted backups, check output of /var/log/kern.log,"
	echo "Normaly this is related to linux fs mount options."
	echo -e "\033[1mbackupconfig\033[0m: RAR specified folders to local file system (included also in above options)."
	echo
	echo -e "\033[4mHelper Options\033[0m"
	echo
	echo -e "\033[1mcreateloop\033[0m: Starts prompted script for creation of Linux filesystem on SMB Share."
	echo
	echo -e "\033[1mmount win\033[0m: Mounts SMB Share and loop mounts an Linux filesystem on it so you can access the backup files."
	echo -e "\033[1mumount win\033[0m: Same as above with unmount and unloading of Rdiff-web."
	echo -e "\033[1mmount ssh\033[0m: Mounts Unix/Linux share over NFS in order to see backed files (in rdiff-web)."
	echo -e "\033[1mumount ssh\033[0m: Same as above with unmount and unloading of Rdiff-web."
	exit 1
	;;

esac
 
Warum nimmst du rar?!

Rar sichert die Dateirechte afair nicht mit! Damit kannst du dir beim Backup einspielen dein System zerlegen wenns systemwichtige Dateien sind.

Nimm tar.*** ohne Komprimierungsgrad, das sichert je nach Schalter die Dateirechte, und schieb das noch mal durch die rar sache. Obwoh ich denke das xz besser komprimiert.
 
Warum rar, hat ein paar nette Features:
- Unterstützt update, Archiv muss nicht jedes mal neu angelegt werden (geht wesentlich schneller)
- Passwort
- Recovery records
- User/Group permissions können mit -ow gesichert werden

Nachteile:
- Errorlevel nicht zu gebrauchen, man kann nicht abfragen ob das Backup ok ist
- Logs schlecht zu lesen

Aber nochmal, rar ist nicht die Hauptsicherungsmethode bei dem Script.
 
Zuletzt bearbeitet:
Ich habe das Script wieder komplett überarbeitet, da mein /var und /usr Folder doch recht groß wurde (u.a. wegen Postfix) und die Deduplikation bei Archiven eben nicht richtig funktioniert.
Jetzt ist es möglich mehrere Folder direkt per rdiffbackup zu sichern.

Changelog:
- Bug beseitigt: Wenn bei der Option backupssh noch kein backup existierte, stoppte das Script nach remove older than
- Es ist nun möglich mit rdiff-backup mehrere Folder zu sichern (exclude/include list)
Weil nun der Quellfolder in Include steht, ist das Script zu Backupdaten, die mit älteren Versionen des Scripts erstellt wurden,
nicht mehr kompatibel
(rdiff-backup-data liegt nun in einem anderen Verzeichnis) und die Backupdaten können auch nicht angepasst werden
- Es wird nun automatisch ein Folder entsprechend dem Hostnamen angelegt als Verzeichnis über dem Backup
(falls man mehrere Hosts zum selben Ziel sichern möchte)
- Pfad für Script Location gefixt, temp Files (Datumcheck) werden nun immer im selben Pfad gespeichert wo das Script liegt
- Falls rar genutzt wird (backupconfig) ist es nun möglich auch nur ein einzelnes RAR-File zu erzeugen
- Wenn Daycheck aktiv (Option) ist, ist es ohne --force nicht mehr möglich das Script im Fehlerfall nochmal zu starten
- Mail mit Warning wenn Kapazität im Target > x%, zusätzliches critical Level ab dem Backup nicht mehr läuft
- Bei Nutzung von rdiff-backup über ssh werden nun die Files lokal als root kopiert aber mittels dem spezifizierten Linuxuser über ssh
übertragen, dazu ist eine Änderung in /root/.ssh/config notwendig. Das war notwendig, weil manche Files nur mit Root lesbar sind z.B. SQL,Postfix

Code:
#!/bin/bash
#rdiffback.sh
PROGVER=2.0
#by xrated 08/2012

########################################################

# What this script does: 
# You can backup your local data to other Windows, Linux or Unix hosts, by using rdiff-backup.
# Rdiff-backup is working on block level, so multiple generations are really small in size.
# If you want to backup to Linux/Unix, then package rdiff-backup needs to exist on the other host and should be the same version if possible.
# If Windows is the backup target, then you need to configure SMB and a loop mount.

# Additionally you can use the backupconfig section, which backup folders to an archive.
# For example: To backup data such as files including passwords, typically /etc folder.
# Please note that block level backup works very limited on archives. So dont use this for big amount of data.

########################################################

#global backup options
FILEOT=1Y #delete files older than
BACKUPFOLDER=/ #folder to backup on local machine, only single folder possible, do not end with /
INCLUDELIST=rdiffbackinclude # !! everything here takes precedence over exlude list !!
EXCLUDELIST=rdiffbackexclude # in your exclude file you can list just ** to exclude everything, so only the include list will work
LOGLEVEL=5 #loglevel 1-9, 3 is default, 5 is showing files
LOGPATH=/var/log/rdiffback # make sure folder exists
RDIFFLOGCONFIG=rdiffback-config #logfile
RDIFFBIN=/usr/bin/rdiff-backup
RDIFFWEBUNLOAD=yes #unload rdiff-web before umount, otherwise umount fails
CAPWARN=85% #warn level in %, send mail when capacity reaches this level
CAPCRIT=98% #critical level in %, backup will not work when reached
#########################################################

# Option for createloop option
RDIFFLOGLOOP=rdiffback-createloop
_temp="/tmp/tmp.rdiffback"
DIALOGBIN=/usr/bin/dialog

########################################################

#CIFS share as target for backup
#Section is used only when backupwin option is used!
#SMB needs to be configured on the target host and you need a file on it, which will be loop mounted to an linux fs.
#This is done because linux cannot handle lower/upper case files.
#You can also use the createloop option to do this for you.
#Instructions: On your SMB share create a big file enough to fit backup, format it with linux file system and change
#script parameters, before using it. Or start script with createloop option.
#If you have problem with corrupted backups, check output of /var/log/kern.log,
#normaly this is related to linux fs mount options.

TIMERVALWIN=1 # seconds to wait before backup starts, to be sure that remote host is up and running, in case you are not sure if its really up because it may still load its services after a successfull ping
RDIFFLOGWIN=rdiffback-win
RDIFFWINDAYCHECK=yes # if set to no, its possible to run the backup again on the same day - yes may be handy, if you dont know when the remote host is up and the script is started multiple times per day via cron to catch it at least one time
SMBSOURCE=192.168.2.103 # host or ip
SMBSHARE=rdiff
CREDFILE=/etc/.smb #contains user,password for smb mount separated by new line: username=youruser password=yourpassword
MOUNTCIFS=rw,soft,forcedirectio,mapchars,nocase,iocharset=utf8,dir_mode=0660,file_mode=0660,credentials=$CREDFILE #changed hard to soft here
SMBTARGET=/mnt/smbvenus #where SMB share should be mounted to, do not end path with /, make sure this folder exists
#Loopmount, contains linux fs
BACKUPFILE=backup.ext3 #backup file on SMB share, this is where all the files go into
MOUNTFS=ext3 #linux filesystem for loopmount
LOOPTARGET=/mnt/loopvenus #path where file should be loop mounted to, do not end path with /
MOUNTWINLINUX=sync,loop,rw,noatime,barrier=1 #mount options for linux fs, use barrier do avoid delayed write, loop makes it possible to use a filesystem on a single file

##########################################################

#Linux/Unix as backup target 
#Section is used only when backupssh option is used!
#Instructions
#step1: configure ssh to work withou password (because ssh has no password option)
#login as specified user
#ssh-keygen -t rsa 
#copy - via scp - the pub file from ~/.ssh to other server into ~/.ssh/authorized_keys file:
#scp ~/.ssh/id_rsa.pub username@host:~/.ssh/authorized_keys
#chmod 600 key files and authorized_keys file on both systems afterwards
#now it should be possible to ssh into other system without password
#step2: to make it possible that user root uses rd_rsa file of that other user
#create /root/.ssh/config file
#enter the following:
#host targethost
#hostname targethost
#user username
#identityfile /home/trechber/.ssh/id_rsa
#finally chmod it 600
TIMERVALSSH=1 # see above in SMB section
RDIFFLOGSSH=rdiffback-ssh
RDIFFSSHDAYCHECK=yes # see above in SMB section
MOUNTNFSOPT=rw,async # mount nfs options
MOUNTNFSVER=4 # mount nfs with version as specified
LINUXHOST=192.168.2.3
LINUXUSER=trechber #user for ssh, normaly root not allowed for ssh
LINUXFOLDER=/data1/public/rdiff #backup target path
LINUXTARGET=/mnt/nfssolaris #this is only to see backed up files in rdiff-web
SCPBIN=/usr/bin/scp # path is local
SSHBIN=/usr/bin/ssh # path is local

###########################################################

#backup config? files will be added to rar file with password on specified location
#Please note: rdiff-backup may create big files if you use it because deduplication does not work well
#User and group permissions will be also stored in archive

RARBIN=/usr/bin/rar
COMPRPWD=$(cat /etc/.comprpwd) # put just one line with password into that file
BACKUPCONFIGFOLDER=/data/Dokumente/debian #do not end path with /, make sure you have also included this folder in rdiff-backup if you need it
COMPRARCHIVES=1 # how many target archives to create, 1 means, dont do COMPRFOLDER2 and COMPRFOLDER3, 0 means dont do any at all
COMPRFOLDER1="/etc" #folder to backup
COMPRFILE1=etc #target file for rar
COMPRFOLDER2="/usr/lib/nagios* /usr/share/nagios* /usr/share/logwatch*"
COMPRFILE22=usr
COMPRFOLDER3="/var/vmail* /var/www* /var/lib/mysql"
COMPRFILE3=var

###########################################################

#email logs?
EMAILWIN=yes # email when rdiff-backup backups to windows host
EMAILSSH=yes # email when rdiff-backup backups to linux/unix host
EMAILCONFIG=yes # email log of backup config, note this will only be used if script is started with backupconfig option
EMAILWARN=yes #email disk space warning/critical
EMAILTO=root

###########################################################

# do not change anything below

#misc parameters
CAPWARN=$(echo $CAPWARN | sed -e 's/[^0-9]//g') #convert to number
CAPCRIT=$(echo $CAPCRIT | sed -e 's/[^0-9]//g')
RDIFFDAYCHECK=$2
PROGNAME=`basename $0`
PROGPATH="$(cd $(dirname $0) && pwd)"


#readout actual date, needed to see if job has been run today
date "+%Y-%m-%d" > $PROGPATH"/rdiffbackactualdate"
rdiffdate1=`head -n 1 $PROGPATH/rdiffbackactualdate`

#check if logfolder exists
if ! test -e $LOGPATH; then
echo "Error: Specified logfolder does not exist!"
fi

#append slash if $BACKUPFOLDER is not root, otherwise resulting path will be messed later
if [ ! "${BACKUPFOLDER,,}" == "/" ]; then
BACKUPFOLDER=$BACKUPFOLDER"/"
fi

funct-listmount ()
{


    case "$1" in
    
    win)
    if mount | grep $LOOPTARGET > /dev/null; then
	df -h $LOOPTARGET
    else
	echo "backupwin target" $LOOPTARGET "is not mounted"
    fi 
    ;;
    ssh)
    echo
    if mount | grep $LINUXTARGET > /dev/null; then
	df -h $LINUXTARGET
    else
	echo "backupssh target" $LINUXTARGET "is not mounted"
    fi
    ;;
    *)
    echo "Error: parameter missing!"
    ;;
    esac



}

funct-mount-win ()
{
	if [ ! "$(mount | grep $SMBTARGET)" ]; then
	    echo "mount" $SMBTARGET
	    mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "mounting" $SMBTARGET "was successfull, you should see" $BACKUPFILE "here"
	    else
		echo "Error:" $SMBTARGET "could not be mounted!" >> $LOGPATH/$LOGPATH/$RDIFFLOGWIN
		mounterror=1
	    fi
	else
	    echo $SMBTARGET "already mounted"
	fi
	
	if [ ! "${mounterror,,}" == "1" ]; then
	    if [ ! "$(mount | grep $LOOPTARGET)" ]; then
		echo "mount" $LOOPTARGET
		sleep 1
		mount -t $MOUNTFS -o $MOUNTWINLINUX $SMBTARGET/$BACKUPFILE $LOOPTARGET
		if [ "$(mount | grep $SMBTARGET)" ]; then
		    echo "mounting" $LOOPTARGET "was successfull, target path for rdiff-backup"
		else
		    echo "Error:" $LOOPTARGET "could not be mounted!" >> $LOGPATH/$RDIFFLOGWIN
		    mounterror=1
		fi
	    else
		echo $LOOPTARGET "already mounted"
	    fi
	fi
}

funct-mount-ssh ()
{
	if [ ! "$(mount | grep $LINUXTARGET)" ]; then
	    echo "mount" $LINUXTARGET
	    mount -t nfs$MOUNTNFSVER -o $MOUNTNFSOPT $LINUXHOST:$LINUXFOLDER $LINUXTARGET
	    if [ "$(mount | grep $LINUXTARGET)" ]; then
		echo "mounting" $LINUXTARGET "was successfull"
	    else
		echo "Error:" $LINUXTARGET "could not be mounted!" >> $LOGPATH/$RDIFFLOGSSH
		mounterror=1
	    fi
	else
	    echo $LINUXTARGET "already mounted"
	fi
}

funct-mount ()
{
	case "$1" in
	    ssh)
	    funct-mount-ssh
	    ;;
	    win)
	    funct-mount-win
	    ;;
	    *)
	    ;;
	esac
	
	sleep 1
	
	if [ ! "${mounterror,,}" == "1" ]; then
	    if [ ! "$(ps -A | grep rdiff-web)" ]; then
		if test -e /etc/init.d/rdiff-web; then
		    /etc/init.d/rdiff-web start
		    if [ "$(ps -A | grep rdiff-web)" ]; then
			echo "rdiff-web successfully started"
			echo "you can start rdiff-web now on http://<thishostorip>:port, usually port 8080"
		    else
			echo "Error: rdiff-web could not be started!"
		    fi
		else
		    echo "Error: rdiff-web not installed!"
		fi
	    else
	    echo "rdiff-web already started"
	    fi
	fi
}

funct-umount-win ()
{
	if [ "$(mount | grep $LOOPTARGET)" ]; then
	    echo "umount" $LOOPTARGET
	    sleep 1
	    umount $LOOPTARGET
	    if [ "$(mount | grep $LOOPTARGET)" ]; then
		echo "Error:" $LOOPTARGET "is still mounted!" >> $LOGPATH/$RDIFFLOGWIN
		mounterror=1
	    else
	    echo "umounting" $LOOPTARGET "was successfull"
	    fi
	fi
	
	if [ ! "${mounterror,,}" == "1" ]; then
	if [ "$(mount | grep $SMBTARGET)" ]; then
	    echo "umount" $SMBTARGET
	    sleep 1
	    umount $SMBTARGET
	    if [ "$(mount | grep $SMBTARGET)" ]; then
		echo "Error:" $SMBTARGET "is still mounted!" >> $LOGPATH/$RDIFFLOGWIN
	    else
		echo "unmounting" $SMBTARGET "was successfull"
	    fi
	fi
	fi
}

funct-umount-ssh ()
{
	if [ "$(mount | grep $LINUXTARGET)" ]; then
	    echo "umount" $LINUXTARGET
	    sleep 1
	    umount $LINUXTARGET
	    if [ "$(mount | grep $LINUXTARGET)" ]; then
		echo "Error:" $LINUXTARGET "is still mounted!" >> $LOGPATH/$RDIFFLOGSSH
	    else
	    echo "umounting" $LINUXTARGET "was successfull"
	    fi
	fi
}


funct-umount ()
{
	if [ "${RDIFFWEBUNLOAD,,}" == "yes" ]; then
	    if [ "$(ps -A | grep rdiff-web)" ]; then
		/etc/init.d/rdiff-web stop
		sleep 1
		if [ ! "$(ps -A | grep rdiff-web)" ]; then
		    echo "rdiff-web is stopped now"
		else
		    echo "Error: rdiff-web could not be stopped!"
		fi
	    else
	    echo "rdiff-web already stopped"
	    fi
	fi
	
	case "$1" in
	    win)
	    funct-umount-win
	    ;;
	    ssh)
	    funct-umount-ssh
	    ;;
	    *)
	    ;;
	esac

}

funct-spacefree ()

{

case "$1" in

win)

spacefreeperc=`df -h | grep $LOOPTARGET | awk '{ print $5 }' | sed -e 's/[^0-9]//g'`
spacefree=`df -h | grep $LOOPTARGET | awk '{ print $4 }'`

    if [ "${spacefreeperc,,}" -ge $CAPWARN ]; then
	if [ "${spacefreeperc,,}" -ge $CAPCRIT ]; then
	    echo "Error: "$CAPCRIT"% capacity reached" >> $LOGPATH/$RDIFFLOGWIN
	    if [ "${EMAILWARN,,}" == "yes" ] ; then
    		if mail -s "[$PROGNAME] - Rdiff-backup $(hostname) to $SMBSOURCE - disk space critical - $CAPCRIT% capacity reached" $EMAILTO < /dev/null ; then
    		    echo "Mail capacity warning sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGWIN
    		else
    		    echo "Error: Mail capacity warning could not be sent!" >> $LOGPATH/$RDIFFLOGWIN
    		fi
	    fi
	else
	    echo "Warning: "$CAPWARN"% capacity reached" >> $LOGPATH/$RDIFFLOGWIN
	    if [ "${EMAILWARN,,}" == "yes" ] ; then
    		if mail -s "[$PROGNAME] - Rdiff-backup $(hostname) to $SMBSOURCE - disk space warning - $CAPWARN% capacity reached" $EMAILTO < /dev/null ; then
    		    echo "Mail capacity warning sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGWIN
    		else
    		    echo "Error: Mail capacity warning could not be sent!" >> $LOGPATH/$RDIFFLOGWIN
    		fi
	    fi
	fi
    else
	echo "OK: Free capacity on target volume is "$spacefree"/"$spacefreeperc"%, warning level is at "$CAPWARN"%." >> $LOGPATH/$RDIFFLOGWIN
    fi

;;

ssh)

spacefreeperc=`df -h | grep $LINUXTARGET | awk '{ print $4 }' | sed -e 's/[^0-9]//g'`
spacefree=`df -h | grep $LINUXTARGET | awk '{ print $3 }'`

    if [ "${spacefreeperc,,}" -ge $CAPWARN ]; then
	if [ "${spacefreeperc,,}" -ge $CAPCRIT ]; then
	    echo "Error: "$CAPCRIT"% capacity reached" >> $LOGPATH/$RDIFFLOGSSH
	    if [ "${EMAILWARN,,}" == "yes" ] ; then
    		if mail -s "[$PROGNAME] - Rdiff-backup $(hostname) to $LINUXHOST - disk space critical - $CAPCRIT% capacity reached" $EMAILTO < /dev/null ; then
    		    echo "Mail capacity warning sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGSSH
    		else
    		    echo "Error: Mail capacity warning could not be sent!" >> $LOGPATH/$RDIFFLOGSSH
    		fi
	    fi
	else
	    echo "Warning: "$CAPWARN"% capacity reached" >> $LOGPATH/$RDIFFLOGSSH
	    if [ "${EMAILWARN,,}" == "yes" ] ; then
    		if mail -s "[$PROGNAME] - Rdiff-backup $(hostname) to $LINUXHOST - disk space warning - $CAPWARN% capacity reached" $EMAILTO < /dev/null ; then
    		    echo "Mail capacity warning sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGSSH
    		else
    		    echo "Error: Mail capacity warning could not be sent!" >> $LOGPATH/$RDIFFLOGSSH
    		fi
	    fi
	fi
    else
	echo "OK: Free capacity on target volume is "$spacefree"/"$spacefreeperc"%, warning level is at "$CAPWARN"%." >> $LOGPATH/$RDIFFLOGSSH
    fi

;;

*)
;;
esac

}

funct-backupconfig ()

{

    
    if [ "${EMAILCONFIG,,}" == "yes" ]; then
	#move old log, otherwise old logs will be mailed everytime again
    	if test -e $LOGPATH/$RDIFFLOGCONFIG; then
    	    mv $LOGPATH/$RDIFFLOGCONFIG $LOGPATH/$RDIFFLOGCONFIG-$rdiffdate1
    	fi
    fi


    #add information to backup log
    echo "The following folders:" $COMPRFOLDER1 "were compressed into rar to" $BACKUPCONFIGFOLDER "see log below" >> $LOGPATH/$RDIFFLOGCONFIG
    #rar various config folders to backup folder	
    if test -e $RARBIN ; then
	if [ "${COMPRARCHIVES,,}" -ge 1 ]; then
    		if $RARBIN u -hp$COMPRPWD -ow -rr5% $BACKUPCONFIGFOLDER/$COMPRFILE1.rar $COMPRFOLDER1 >> $LOGPATH/$RDIFFLOGCONFIG; then
    		    echo . >> $LOGPATH/$RDIFFLOGCONFIG
    		    echo "RAR OK:" $COMPRFOLDER1 >> $LOGPATH/$RDIFFLOGCONFIG
    		else
    		    echo "Error, maybe just no new files?:" $COMPRFOLDER1 >> $LOGPATH/$RDIFFLOGCONFIG
    		fi
        fi
        
        if [ "${COMPRARCHIVES,,}" -ge 2 ]; then
    		if $RARBIN u -hp$COMPRPWD -ow -rr5% $BACKUPCONFIGFOLDER/$COMPRFILE2.rar $COMPRFOLDER2 >> $LOGPATH/$RDIFFLOGCONFIG; then
    		    echo "RAR OK:" $COMPRFOLDER2 >> $LOGPATH/$RDIFFLOGCONFIG
    		else
    		    echo "Error, maybe just no new files?:" $COMPRFOLDER2 >> $LOGPATH/$RDIFFLOGCONFIG
    		fi
        fi
        
        if [ "${COMPRARCHIVES,,}" -ge 3 ]; then
    		if $RARBIN u -hp$COMPRPWD -ow -rr5% $BACKUPCONFIGFOLDER/$COMPRFILE3.rar $COMPRFOLDER3 >> $LOGPATH/$RDIFFLOGCONFIG; then
    		    echo "RAR OK:" $COMPRFOLDER3 >> $LOGPATH/$RDIFFLOGCONFIG
		else
		    echo "Error, maybe just no new files?:" $COMPRFOLDER3 >> $LOGPATH/$RDIFFLOGCONFIG
		fi
	fi
	
    else
        echo "Error:" $RARBIN "not found!" >> $LOGPATH/$RDIFFLOGCONFIG
        echo "Your system reports it at" && which rar >> $LOGPATH/$RDIFFLOGCONFIG
	backuperror=1
    fi
    echo "#######################################################################################################################" >> $LOGPATH/$RDIFFLOGCONFIG

}

funct-backupsshdaycheck ()

{
if test -e $LOGPATH/$RDIFFLOGSSH; then
ls $LOGPATH/$RDIFFLOGSSH --full-time | awk '{print $6}' > $PROGPATH/rdiffbacklastrunssh
rdiffdate2=`head -n 1 $PROGPATH/rdiffbacklastrunssh`
fi

if [ "$rdiffdate1" != "$rdiffdate2" ]; then
echo "OK: rdiffback has not been run today"
backupssh=1
else
    if [ ! "$RDIFFDAYCHECK" == "--force" ]; then
	echo "INFO: rdiffback was already run today. If you want to run it again use --force switch"
    fi
backupssh=0
fi
}

funct-backupwindaycheck ()

{
if test -e $LOGPATH/$RDIFFLOGWIN; then
ls $LOGPATH/$RDIFFLOGWIN --full-time | awk '{print $6}' > $PROGPATH/rdiffbacklastrunwin
rdiffdate2=`head -n 1 $PROGPATH/rdiffbacklastrunwin`
fi

if [ "$rdiffdate1" != "$rdiffdate2" ]; then
echo "OK: rdiffback has not been run today"
backupwin=1
else
    if [ ! "$RDIFFDAYCHECK" == "--force" ]; then
	echo "INFO: rdiffback was already run today. If you want to run it again, use --force switch"
    fi	
backupwin=0

fi
}


case "$1" in
    backupssh)

    funct-backupsshdaycheck
        
if [ "$backupssh" == "1" ] || [ "$RDIFFDAYCHECK" == "--force" ] ; then

    if [ "$RDIFFDAYCHECK" == "--force" ]; then
	echo "INFO: --force switch is enabled, so the script is allowed to run multiple times per day"
    fi
    
    if [ "$(ping -c 1 $LINUXHOST | grep "1 received")" ]; then
	echo "OK: remote host" $LINUXHOST "is reachable"
	echo "Wait" $TIMERVALSSH "seconds to be sure host is ready and not still booting up..."
	sleep $TIMERVALSSH
	
	funct-mount-ssh
	funct-spacefree ssh
	funct-umount-ssh

	if [ ! "${spacefreeperc,,}" -ge $CAPCRIT ]; then
    
	    #move old log, otherwise old logs will be mailed everytime again
    	    if test -e $LOGPATH/$RDIFFLOGSSH; then
	        if ! mv $LOGPATH/$RDIFFLOGSSH $LOGPATH/$RDIFFLOGSSH-$rdiffdate1; then
		    echo "Error: Could not move old log!" >> $LOGPATH/$RDIFFLOGSSH
	        fi
	    fi
	    
	    if [ "${COMPRARCHIVES,,}" -ge 1 ]; then
	    funct-backupconfig
	    cat $LOGPATH/$RDIFFLOGCONFIG >> $LOGPATH/$RDIFFLOGSSH
	    fi


    
		if test -e $RDIFFBIN; then
		    if $RDIFFBIN -v$LOGLEVEL --remove-older-than $FILEOT $LINUXUSER@$LINUXHOST::$LINUXFOLDER/$(hostname)$BACKUPFOLDER; then
			echo "OK: Rdiff-backup removed files older than" $FILEOT >> $LOGPATH/$RDIFFLOGSSH
		    else
		    	echo "Error: Rdiff-backup could not run --remove-older-than job" >> $LOGPATH/$RDIFFLOGSSH
			backuperror=1
		    fi
		
		    if $RDIFFBIN -v$LOGLEVEL --include-globbing-filelist $PROGPATH/$INCLUDELIST --exclude-globbing-filelist $PROGPATH/$EXCLUDELIST $BACKUPFOLDER $LINUXUSER@$LINUXHOST::$LINUXFOLDER/$(hostname)$BACKUPFOLDER; then
		        echo "OK: Rdiff-backup gave no Error" >> $LOGPATH/$RDIFFLOGSSH
		        if test -e $SCPBIN; then
		    	# copy backup log to local tmp folder, user has no rights on /var/log and its not always possible to use root over ssh
		    	    if sudo -u $LINUXUSER $SCPBIN $LINUXUSER@$LINUXHOST:$LINUXFOLDER/$(hostname)$BACKUPFOLDER"rdiff-backup-data/backup.log" /tmp/backuprdifftemp.log; then
				# copy log from tmp to /var/log
				if cat /tmp/backuprdifftemp.log >> $LOGPATH/$RDIFFLOGSSH; then
				    if test -e $SSHBIN; then
					# delete log on other system
					if ! sudo -u $LINUXUSER $SSHBIN -l $LINUXUSER $LINUXHOST rm $LINUXFOLDER/$(hostname)$BACKUPFOLDER"rdiff-backup-data/backup.log"; then
					    echo "Error: Backup log could not be deleted on other host!" >> $LOGPATH/$RDIFFLOGSSH
					fi
				    else
					echo "Error:" $SSHBIN "not found!" >> $LOGPATH/$RDIFFLOGSSH
				    fi
				else
				    echo "Error: Backup log could not be moved!" >> $LOGPATH/$RDIFFLOGSSH
				fi
			    else
			        echo "Error: Backup log could not be copied from other host!" >> $LOGPATH/$RDIFFLOGSSH
			    fi
			else
			    echo "Error:" $SCPBIN "not found!" >> $LOGPATH/$RDIFFLOGSSH
			fi
		    else
		        echo "Error: Rdiff-backup could not run" >> $LOGPATH/$RDIFFLOGSSH
		        backuperror=1
		    fi
		
		
		else
		    echo "Error:" $RDIFFBIN "not found!" >> $LOGPATH/$RDIFFLOGSSH
	    	    echo "Your system reports it at" && which rdiff-backup >> $LOGPATH/$RDIFFLOGSSH
		    backuperror=1
		fi
	

    
	    if [ "${EMAILSSH,,}" == "yes" ]; then
    		# mail file
		if [ "${backuperror,,}" == "1" ]; then
		
		    if mail -s "[$PROGNAME] Rdiff-backup $(hostname) to $LINUXHOST had errors" $EMAILTO < $LOGPATH/$RDIFFLOGSSH; then
    			echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGSSH
    		    else
    			echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGSSH
    		    fi
		
		else
		
    		    if mail -s "[$PROGNAME] Rdiff-backup $(hostname) to $LINUXHOST completed" $EMAILTO < $LOGPATH/$RDIFFLOGSSH; then
    			echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGSSH
    		    else
    			echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGSSH
    		    fi
    		fi
	    fi
	    
	    
	    if [ "${backuperror,,}" == "1" ] && [ "${RDIFFSSHDAYCHECK,,}" == "no" ]; then
		#move to old log, so we could run backup again on the same day, but only in case errors
    		if test -e $LOGPATH/$RDIFFLOGSSH; then
    		    if ! mv $LOGPATH/$RDIFFLOGSSH $LOGPATH/$RDIFFLOGSSH-$rdiffdate1; then
    		        echo "Error: Could not move old log!" >> $LOGPATH/$RDIFFLOGSSH
    		    fi
    		fi
    	    fi

	else
	    echo "Error: remote volume on" $LINUXHOST "critial capacity reached!"
	    funct-umount-ssh
	fi
	
    else
        echo "Error: remote host" $LINUXHOST "is not reachable!"
    fi

fi


    ;;

    backupconfig)
    
    if [ "${COMPRARCHIVES,,}" -ge 1 ]; then
    
    funct-backupconfig

    if [ "${EMAILCONFIG,,}" == "yes" ]; then
    	# mail file
    	
    	if [ "${backuperror,,}" == "1" ]; then
	
    	
    	    if mail -s "[$PROGNAME] - RAR Backup on $(hostname) had errors" $EMAILTO < $LOGPATH/$RDIFFLOGCONFIG; then
    		echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGCONFIG
    	    else
    		echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGCONFIG
    	    fi
    
	else
	
	    if mail -s "[$PROGNAME] - RAR Backup on $(hostname) completed" $EMAILTO < $LOGPATH/$RDIFFLOGCONFIG; then
    		echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGCONFIG
    	    else
    		echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGCONFIG
    	    fi
    	    
	fi
    
    fi

    else
    
    echo "Error: Configure parameters first!" >> $LOGPATH/$RDIFFLOGCONFIG

    fi

    ;;


    backupwin)

funct-backupwindaycheck

if [ "$backupwin" == "1" ] || [ "$RDIFFDAYCHECK" == "--force" ] ; then

    if [ "$RDIFFDAYCHECK" == "--force" ]; then
	echo "INFO: --force switch is enabled, so the script is allowed to run multiple times per day"
    fi


    if [ "$(ping -c 1 $SMBSOURCE | grep "1 received")" ]; then
	echo "OK: remote host" $SMBSOURCE "is reachable"
	echo "Wait" $TIMERVALWIN "seconds to be sure host is ready and not still booting up..."
	sleep $TIMERVALWIN

	funct-mount-win
	funct-spacefree win
	
	if [ ! "${spacefreeperc,,}" -ge $CAPCRIT ]; then
	    
	    #move old log, otherwise old logs will be mailed everytime again
    	    if test -e $LOGPATH/$RDIFFLOGWIN; then
    	        if ! mv $LOGPATH/$RDIFFLOGWIN $LOGPATH/$RDIFFLOGWIN-$rdiffdate1; then
    	        echo "Error: Could not move old log!" >> $LOGPATH/$RDIFFLOGWIN
    	        fi
    	    fi
    	    
    	    if [ "${COMPRARCHIVES,,}" -ge 1 ]; then
    	    funct-backupconfig
    	    cat $LOGPATH/$RDIFFLOGCONFIG >> $LOGPATH/$RDIFFLOGWIN
	    fi
	    
    	    if [ "$(mount | grep $LOOPTARGET)" ]; then
    		# add information to log
    		echo $(date)": rdiff-backup of" $BACKUPFOLDER "to //"$SMBSOURCE/$SMBSHARE/$BACKUPFILE "started." $BACKUPFILE "contains an" $MOUNTFS "filesystem." >> $LOOPTARGET/$(hostname)$BACKUPFOLDER"rdiff-backup-data/backup.log"
    		# delete older than defined in variable FILEOT
    		if test -e $RDIFFBIN; then
    			if $RDIFFBIN -v$LOGLEVEL --remove-older-than $FILEOT $LOOPTARGET/$(hostname)$BACKUPFOLDER; then
    			    echo "rdiff-backup OK: --remove-older-than" $FILEOT $LOOPTARGET/$(hostname)$BACKUPFOLDER >> $LOGPATH/$RDIFFLOGWIN
    			else
    			    backuperror=1
    			fi
			# start backup
    			if $RDIFFBIN -v$LOGLEVEL --include-globbing-filelist $PROGPATH/$INCLUDELIST --exclude-globbing-filelist $PROGPATH/$EXCLUDELIST $BACKUPFOLDER $LOOPTARGET/$(hostname)$BACKUPFOLDER; then
    			    echo "rdiff-backup OK:" $BACKUPFOLDER $LOOPTARGET/$(hostname)$BACKUPFOLDER
    			else
    			    backuperror=1
    			fi
		else
		    echo "Error:" $RDIFFBIN "not found!" >> $LOGPATH/$RDIFFLOGWIN
		    echo "Your system reports it at" && which rdiff-backup >> $LOGPATH/$RDIFFLOGWIN
		    backuperror=1
		fi
		# copy rdiff-backup log to script logfile because will not be available when unmounted
    		cat $LOOPTARGET/$(hostname)$BACKUPFOLDER"rdiff-backup-data/backup.log" >> $LOGPATH/$RDIFFLOGWIN
    		if [ "${EMAILWIN,,}" == "yes" ]; then
    		    # delete backup log, otherwise old logs get mailed again
    		    if ! rm $LOOPTARGET/$(hostname)$BACKUPFOLDER"rdiff-backup-data/backup.log"; then
    		    echo "Error: Could not delete old log on loopmount!" >> $LOGPATH/$RDIFFLOGWIN
    		    fi
    		fi
    	    else
    		echo "Error:" $LOOPTARGET "not mounted, cannot continue with rdiff-backup!" >> $LOGPATH/$RDIFFLOGWIN
    		backuperror=1
    	    fi
    	    
	    funct-umount-win
    
	    if [ "${EMAILWIN,,}" == "yes" ]; then
    		# mail file
    		
    		if [ "${backuperror,,}" == "1" ]; then
    		
    		    if mail -s "[$PROGNAME] - Rdiff-backup $(hostname) to $SMBSOURCE had errors" $EMAILTO < $LOGPATH/$RDIFFLOGWIN; then
    			echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGWIN    		    
    		    else
    			echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGWIN
    		    fi
    		
    		else
    		
    		    if mail -s "[$PROGNAME] - Rdiff-backup $(hostname) to $SMBSOURCE completed" $EMAILTO < $LOGPATH/$RDIFFLOGWIN; then
    			echo "Mail sent to " $EMAILTO >> $LOGPATH/$RDIFFLOGWIN
    		    else
    			echo "Error: Mail could not be sent!" >> $LOGPATH/$RDIFFLOGWIN
    		    fi
    		
    		fi
	    fi

	    if [ "${backuperror,,}" == "1" ] && [ "${RDIFFWINDAYCHECK,,}" == "no" ]; then
	        #move to old log, so we could run backup again on the same day, but only in case errors
    		if test -e $LOGPATH/$RDIFFLOGWIN; then
    		    if ! mv $LOGPATH/$RDIFFLOGWIN $LOGPATH/$RDIFFLOGWIN-$rdiffdate1; then
    			echo "Error: Could not move old log!" >> $LOGPATH/$RDIFFLOGWIN
    		    fi
    		fi
    	    fi

	else
	    echo "Error: remote volume on" $SMBSOURCE "critical capacity reached!"
	    funct-umount-win
	fi

    else
	echo "Error: remote host" $SMBSOURCE "is not reachable!"
    fi


fi

    ;;

    mount)

    	case "$2" in
	    win)
	    funct-mount win
	    funct-listmount win
	    ;;
	    ssh)
	    funct-mount ssh
	    funct-listmount ssh
	    ;;
	    *)
	    echo "Error: No valid target given!"
	    ;;
	esac

    ;;

    umount)
    
    	case "$2" in
	    win)
	    funct-umount win
	    ;;
	    ssh)
	    funct-umount ssh
	    ;;
	    *)
	    echo "Error: No valid target given!"
	    ;;
	esac

    ;;

    listmount)
    
	case "$2" in
	    win)
	    funct-listmount win
	    ;;
	    ssh)
	    funct-listmount ssh
	    ;;
	    *)
	    echo "Error: No valid target given!"
	    ;;
	esac
    
    
    ;;
    
    createloop)

if test -e $DIALOGBIN; then

$DIALOGBIN --ok-label "Submit" --title "Create Loop" --form "WARNING: THE NEXT STEPS WILL CREATE NEW BACKUPFILE. Found the settings below. If you need to change them, you need to modify parameters inside the script" 15 60 0 \
"CIFS Host:" 1 1 "$SMBSOURCE" 1 20 100 0 \
"CIFS Share:" 2 1 "$SMBSHARE" 2 20 100 0 \
"CIFS mounted on:" 3 1 "$SMBTARGET" 3 20 100 0 \
"CIFS mount options:" 4 1 "$MOUNTCIFS" 4 20 200 0 \
"Backupfile:" 5 1 "$BACKUPFILE" 5 20 100 0 \
"Filesystem:" 6 1 "$MOUNTFS" 6 20 100 0 \
"Mounted on:" 7 1 "$LOOPTARGET" 7 20 100 0 2>$_temp


    if [ $? -eq 0 ]; then
 

	SMBSOURCE=`cat $_temp | sed -ne '1p'`
	SMBSHARE=`cat $_temp | sed -ne '2p'`
	SMBTARGET=`cat $_temp | sed -ne '3p'`
	MOUNTCIFS=`cat $_temp | sed -ne '4p'`
	BACKUPFILE=`cat $_temp | sed -ne '5p'`
	MOUNTFS=`cat $_temp | sed -ne '6p'`
	LOOPTARGET=`cat $_temp | sed -ne '7p'`

	echo "do clean umount to make sure there is no loopmount active"
	funct-umount win
     if [ ! "$(mount | grep $SMBTARGET)" ]; then
	    echo "mount" $SMBTARGET
	    echo "execute: mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET" 
	    mount -t cifs -o $MOUNTCIFS //$SMBSOURCE/$SMBSHARE $SMBTARGET
	
	if [ "$(mount | grep $SMBTARGET)" ]; then
		if test -e $SMBTARGET/$BACKUPFILE; then
		    $DIALOGBIN --title "Create Loop" --inputbox "WARNING: $BACKUPFILE ALREADY EXISTS, DO YOU REALLY WANT TO OVERWRITE IT? Type in the word AGREE" 10 60 2>$_temp
		    result=`cat $_temp`
		    if [[ $result =~ agree|AGREE ]]; then
			$DIALOGBIN --title "Create Loop" --inputbox "How big the $MOUNTFS formatted file should be? This operation can take a while. Enter size in MB:" 10 60 2>$_temp
			result=`cat $_temp`
			if [ -z "$(echo $result|tr -d '[:digit:]')" ] && [ $? == 0 ]; then
			    if [ "$(mount | grep $LOOPTARGET)" ]; then
				$DIALOGBIN --title "Create Loop" --msgbox "Error: $LOOPTARGET is still mounted, cannot continue!" 10 60
			    else
				echo "Overwrite File" $BACKUPFILE "this may take a while, do not cancel"
				dd if=/dev/zero of=$SMBTARGET/$BACKUPFILE bs=1M count=$result
		
				if test -e $SMBTARGET/$BACKUPFILE; then
				    echo "Format existing" $BACKUPFILE " with Filesystem" $MOUNTFS
				    mkfs.$MOUNTFS $SMBTARGET/$BACKUPFILE
				    $DIALOGBIN --title "Create Loop" --msgbox "Format new file $BACKUPFILE with $result MB and filesystem $MOUNTFS finsihed successfully!" 10 60
				else
				    $DIALOGBIN --title "Create Loop" --msgbox "Error: $BACKUPFILE could not be create!" 10 60
				fi
			    fi
			else
			    $DIALOGBIN --title "Create Loop" --msgbox "Error: Value is not in digits!" 10 60
			fi
		    else
		    $DIALOGBIN --title "Create Loop" --msgbox "Operation canceled on user request!" 10 60
		    fi
		else
		    $DIALOGBIN --title "Create Loop" --inputbox "Couldnt find existing backupfile $BACKUPFILE so i will create a new one. Enter size in MB:" 10 60 2>$_temp
		    result=`cat $_temp`
		    if [ -z "$(echo $result|tr -d '[:digit:]')" ] && [ $? == 0 ]; then
		    dd if=/dev/zero of=$SMBTARGET/$BACKUPFILE bs=1M count=$result
			if test -e $SMBTARGET/$BACKUPFILE; then
			    echo "Format new file " $BACKUPFILE "with Filesystem" $MOUNTFS
			    mkfs.$MOUNTFS $SMBTARGET/$BACKUPFILE
			    $DIALOGBIN --title "Create Loop" --msgbox "Format new file $BACKUPFILE with $result MB and filesystem $MOUNTFS finished successfully!" 10 60
			else
			    $DIALOGBIN --title "Create Loop" --msgbox "Error: $BACKUPFILE could not be created!" 10 60
			fi
		    else
			$DIALOGBIN --title "Create Loop" --msgbox "Error: Value is not in digits!" 10 60 
		    fi
		fi
	else
	    $DIALOGBIN --title "Create Loop" --msgbox "Error: $SMBTARGET could not be mounted, cannot continue!" 10 60
	fi
	
     else
        $DIALOGBIN --title "Create Loop" --msgbox "Error: There was an error during umount!" 10 60
     fi

    else
	$DIALOGBIN --title "Create Loop" --msgbox "Operation canceled on user request!" 10 60
    fi



else
echo "dialog not found, is it installed?"
fi
    ;;

    *)

	echo
	echo -e "\033[33m\033[44m$PROGPATH/$PROGNAME \033[0m Version $PROGVER"
	echo "Makes it possible to backup local folders to an remote Linux/Unix or Windows host."
	echo "Usage: $PROGNAME [backupssh [--force]|backupwin [--force]|backupconfig|createloop|mount [win|ssh]|umount [win|ssh]|listmount [win|ssh]]"
	echo -e "\033[31mMake sure you change settings inside $PROGNAME to your needs before you run first backup!\033[0m"
	echo
	echo -e "\033[4mMain Options\033[0m"
	echo
	echo -e "\033[1mbackupssh\033[0m: rdiff-backup to other linux/unix host over ssh. Make sure the access is not denied on the remote host."
	echo -e "If --force is used, it can be run multiple times per day if RDIFFSSHDAYCHECK is enabled."
	echo -e "\033[1mbackupwin\033[0m: rdiff-backup to other win host over CIFS/SMB. Use --force to override RDIFFWINDAYCHECK parameter."
	echo "Because Windows cannot handle different files with lower/upper case, a loop mount" 
	echo "to an linux fs on that CIFS share will be used."
	echo -e "\033[1mbackupconfig\033[0m: RAR specified folders to local file system, included also in above options."
	echo
	echo -e "\033[4mHelper Options\033[0m"
	echo
	echo -e "\033[1mcreateloop\033[0m: Starts prompted script for creation of Linux filesystem on SMB Share."
	echo
	echo -e "\033[1mmount win\033[0m: Mounts SMB Share and loop mounts an Linux filesystem on it so you can access the backup files."
	echo -e "\033[1mumount win\033[0m: Same as above with unmount and unloading of Rdiff-web."
	echo -e "\033[1mmount ssh\033[0m: Mounts Unix/Linux share over NFS in order to see backed files in rdiff-web."
	echo -e "\033[1mumount ssh\033[0m: Same as above with unmount and unloading of Rdiff-web."
	echo -e "\033[1mlistmount\033[0m: Lists if above paths are mounted."
	exit 1
	;;

esac
 
Zuletzt bearbeitet:
Hardwareluxx setzt keine externen Werbe- und Tracking-Cookies ein. Auf unserer Webseite finden Sie nur noch Cookies nach berechtigtem Interesse (Art. 6 Abs. 1 Satz 1 lit. f DSGVO) oder eigene funktionelle Cookies. Durch die Nutzung unserer Webseite erklären Sie sich damit einverstanden, dass wir diese Cookies setzen. Mehr Informationen und Möglichkeiten zur Einstellung unserer Cookies finden Sie in unserer Datenschutzerklärung.


Zurück
Oben Unten refresh