You are not logged in.

#1 2016-08-30 07:47:04

0devnull0
Member
From: Germany
Registered: 2016-06-16
Posts: 4

Bash backup script | Eyecandy > Dialog/ncurses script

Hello community,
I was using Archlinux since few months and I was able to figure out most of the issues alone or/and by web searching, but now I need your help, so this is my first thread ;-)
Am trying to transform a simple bash script to a dialog/ncurses script to no avail. The main reason why I want to use dialog is -apart from the visual effect- the possibility to use different paths (source/destination), while this wont be possible with the simple script. My scripting skills are quite 'primitive'.

#!/bin/bash
##
## Rsync System Backup And Restore
## A try to implement an ncurses UI with dialog... Still not working... :(

#Set parameters
which dialog &> /dev/null
[ $? -ne 0 ]  && echo "Install Dialog pkg to proceed!" && exit 1
which rsync &> /dev/null
[ $? -ne 0 ]  && echo "Install rsync pkg to proceed!" && exit 1

##--------BEGIN "problematic zone"------------
Answer=$(mktemp /tmp/RsyncSelectOpt.XXXXXXX | xargs echo $? | cut -c3-)
SystemBackupDir=$(cat "$(mktemp /tmp/RsyncSystemBackupDir.XXXXXXX | xargs echo $? | cut -c3-)")
HomeBackupDir=$(cat "$(mktemp /tmp/RsyncHomeBackupDir.XXXXXXX | xargs echo $? | cut -c3-)")
HomeDir=$(cat "$(mktemp /tmp/RsyncHomeDir.XXXXXXX | xargs echo $? | cut -c3-)")
ExcludeFrom=$(cat "$(mktemp /tmp/RsyncExcludeFromLocation.XXXXXXX | xargs echo $? | cut -c3-)")

#Options

function SelectOption() {
dialog --backtitle "Rsync System Backup and Restore" --title "What do you want to do? Backup...Restore?" --menu "Select your option:" 15 50 6 1 "Backup Home" 2 "Backup System" 3 "Backup System and Home" 4 "Restore System" 5 "Restore Home" 6 "Quit" 2>"$Answer"
}
SelectOption;
A=$(cat $Answer)
   case $A in
       1) # "Backup Home" /home/ only.
       dialog --backtitle "Backup Home" --title "ExcludeFrom File" --fselect $HOME/ 10 20 2>"$ExcludeFrom"
       dialog --backtitle "Backup Home" --title "Which User Dir to backup?" --dselect $HOME/ 20 40 2>"$HomeDir"
       dialog --backtitle "Backup Home" --title "Home Backup Destination" --dselect /run/media/ 20 40 2>"$HomeBackupDir"
       dialog --backtitle "Rsync System Backup&Restore" --title "Backup Home" --infobox "Transfering Home\nPlease wait..." 10 20; sleep 2
       rsync -aAXh --numeric-ids --info=progress2 --delete --exclude-from=$ExcludeFrom $HomeDir $HomeBackupDir
       dialog --backtitle "Rsync System Backup&Restore" --title "Backup Home" --infobox "Home Backup finished." 5 30
         break
          ;;
      2) # "Backup System"
       dialog --backtitle "System Backup" --title "System Destination" --dselect /run/media/ 10 50 2>"$SystemBackupDir"
       dialog --backtitle "Rsync System Backup&Restore" --title "Backup System" --infobox 'Backing up the rootfs\nPlease wait...' 10 20; sleep 2
       rsync -aAXHh --numeric-ids --info=progress2 --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found","/home/*"} / $SystemBackupDir
       dialog --backtitle "Rsync System Backup&Restore" --title "Backup System" --infobox 'System Backup finished!' 10 20
         break
          ;;
     
      3) # "Backup System AND Home"
       dialog --backtitle "Backup Home" --title "ExcludeFrom File" --fselect $HOME/ 10 20 2>"$ExcludeFrom"
       dialog --backtitle "Backup Home" --title "Which User Dir to backup?" --dselect /home/ 20 40 2>"$HomeDir"
       dialog --backtitle "Backup Home" --title "Home Backup Destination" --dselect /run/media/ 20 40 2>"$HomeBackupDir"
       dialog --backtitle "System Backup" --title "System Destination" --dselect /run/media/ 10 50 2>"$SystemBackupDir"
       dialog --backtitle "Rsync System Backup&Restore" --title "Backup System AND Home" --infobox 'Backing up System\nPlease wait...' 10 30; sleep 2
       rsync -aAXHh --numeric-ids --info=progress2 --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found","/home/*"} / $SystemBackupDir
       dialog --backtitle "Backup System AND Home" --title "Backup System" --infobox 'System Backup finished!' 10 20; sleep  2
       dialog --backtitle "Backup System AND Home" --title "Backup Home" --infobox 'Backing up Home\nPlease wait...' 10 20; sleep 2
       #Backup /home/ only.
	   rsync -aAXh --numeric-ids --info=progress2 --delete --exclude-from=$ExcludeFrom $HomeDir $HomeBackupDir
	   dialog --backtitle "Backup System AND Home" --title "Backup Home" --infobox "System and Home Backup finished" 5 40
         break
          ;;
    
       4) # "Restore System"
       dialog --backtitle "Restore System" --title "System Restore From" --dselect /run/media/ 10 50 2>"$SystemBackupDir"
       dialog --backtitle "Rsync System Backup&Restore" --title "Restore System" --infobox 'Restoring the rootfs\nPlease wait...' 10 30; sleep 2
       rsync -aAXHh --numeric-ids --info=progress2 --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found","/home/*"} $SystemBackupDir /
       dialog --backtitle "Rsync System Backup&Restore" --title "Restore System" --infobox 'Restoring the rootfs finished!' 10 30; sleep 2
         break
          ;;
    
       5) # "Restore Home"
        dialog --backtitle "Restore Home" --title "ExcludeFrom File" --fselect $HOME/ 10 20 2>"$ExcludeFrom"
        dialog --backtitle "Restore Home" --title "Home Backup Location To Restore From" --dselect /run/media 20 40 2>"$HomeBackupDir"
        dialog --backtitle "Restore Home" --title "Which User Dir to Restore?" --dselect /home/ 20 40 2>"$HomeDir"
        dialog --backtitle "Rsync System Backup&Restore" --title "Restore Home" --infobox "Restoring Home\nPlease wait..." 10 30; sleep 2
        #Restore /home/ only.
	    rsync -aAXh --numeric-ids --info=progress2 --delete --exclude-from=$ExcludeFile $HomeBackupDir $HomeDir #2>&1 >/dev/null
	    dialog --backtitle "Rsync System Backup&Restore" --title "Restore Home" --infobox 'Home Restore finished!' 5 40
          break
          ;;
        
       6) # "Quit"
        dialog --backtitle "Rsync System Backup&Restore" --title "Cancelling" --infobox 'Cancelled, Nothing done!' 5 40
          break
          ;;
    
      # *) dialog --backtitle "Rsync System Backup&Restore" --title "Not a Valid Option" --infobox 'That was not a valid option\nTry it again!' 5 40; sleep 2; SelectOption;;
    esac
 
dialog --backtitle "Rsync System Backup&Restore" --title "INFO" --infobox 'Task completed!' 5 30
find /tmp -maxdepth 1 -type f -iname "*Rsync*" -delete
##--------------- END "problematic zone"--------------------
##
################################################################################################################
## Here is the original version am still using
## based on: "https://de.manjaro.org/index.php?topic=1766.msg14473#msg14473" 

HomeDir='/home/username/'
HomeBackupDir='/external/drive/mountpoint/username/
SystemBackupDir='/externel/drive/mountpoint/systembackup'
ExcludeFile='/home/username/pathtofile/'

##Select option: 
PS3='Your choice: '
options=("Backup Home" "Backup System" "Backup System and Home" "Restore System" "Restore Home" "Quit")

select opt in "${options[@]}"
do
    case $opt in
        "Backup Home")
             echo --------------------------------------------------------------
	     echo Home Backup in progress.....
	     echo --------------------------------------------------------------
	    ##Backup $HOME only.
	     rsync -aAXh --numeric-ids --info=progress2 --delete --exclude-from=$ExcludeFile $HomeDir $HomeBackupDir
	    
	    echo --------------------------------------------------------------
	    echo '(°_°)' Home Backup finished.'(°_°)' 
	     
            break
            ;;
        "Backup System")
	    echo --------------------------------------------------------------
	    echo System Backup in progress.....
	    echo --------------------------------------------------------------
	   ## Backup System (/) only
	    rsync -aAXHh --numeric-ids --info=progress2 --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found","/home/*"} / $SystemBackupDir

	    echo --------------------------------------------------------------
	    echo '(°_°)'  System Backup finished   '(°_°)'
	    
            break
            ;;
        "Backup System and Home")
	    echo ----------------------------------------------------------
	    echo '===> System Backup in progress (1/2)....'
	    echo ----------------------------------------------------------
	    ##System backup, $HOME excluded
	    rsync -aAXHh --numeric-ids --info=progress2 --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found","/home/*"} / $SystemBackupDir
	    echo ---------------------------------------------------------- 
	    echo '===> System Backup finished.'
	    echo ----------------------------------------------------------
	    ##Backup $HOME directory
	    echo '===> Home Backup in progress (2/2)....'
	    echo ----------------------------------------------------------
	    rsync -aAXh --numeric-ids --info=progress2 --delete --exclude-from=$ExcludeFile $HomeDir $HomeBackupDir
	  
	    echo ----------------------------------------------------------
	    echo      Home Backup finished.
	    echo             '======='
	    echo      System and  home Backup finished.

	    break
            ;;
        "Quit")
	    echo ------------------------------------------------------------
	    echo "Cancelled, no changes made."
	    echo ------------------------------------------------------------
            break
            ;;            
            "Restore System")
	    echo ----------------------------------------------------------
	    echo Restoring System....
	    echo ----------------------------------------------------------

	    ##Restore System (/) $HOME excluded:
	    rsync -aAXHh --numeric-ids --info=progress2 --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found","/home/*"} $SystemBackupDir /

	    echo ===========================================================
	    echo  '(°_°)'   System restored.   '(°_°)'
	    
            break
            ;;
            "Restore Home")
            echo --------------------------------------------------------------
            echo      Restoring Home....
	    echo --------------------------------------------------------------
	    
	    ##Restore $HOME
	    rsync -aAXh --numeric-ids --info=progress2 --delete --exclude-from=$ExcludeFile $HomeBackupDir $HomeDir 
	    echo ===========================================================
	    echo 
	    echo '(°_°)'  Home directory restored. '(°_°)'
	    echo 
            break
            ;;
        *) echo Invalid choice;;
    esac
done
echo ===========================================================
echo                   '(°_°)' Task completed. '(°_°)' 
echo ===========================================================

#-------------------------------------------------[url]https://wiki.archlinux.org/index.php/Full_system_backup_with_rsync[/url]--------------------------------------------------------------

May be someone find it useful and want to use it too,  at least the working part, so feel free to do whatever you like with it wink

Last edited by 0devnull0 (2016-09-01 23:14:29)

Offline

#2 2016-09-02 19:32:06

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,412
Website

Re: Bash backup script | Eyecandy > Dialog/ncurses script

That manjaro section is written by someone with zero understanding of the Bash language. Using break statements with "case" is a dead giveaway. Don't base your script on it.

Anyway you can put it through shellcheck:

http://www.shellcheck.net/

Or better, outline what exactly you're trying to achieve.

P.S. http://mywiki.wooledge.org/BashGuide

Last edited by Alad (2016-09-02 19:35:22)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3 2016-09-06 02:26:06

0devnull0
Member
From: Germany
Registered: 2016-06-16
Posts: 4

Re: Bash backup script | Eyecandy > Dialog/ncurses script

Thanks Alad, the links were very helpful especially shellcheck.
What I want to do is to be able to choose what to backup and where to save the backup (locally). That was actually the problem, because I don't know
how to save source/destination and other variables to temporal files and use them later in the script...
This is what I come up with, am quite happy with it, the only problem (up till now) is when the dialog opens I select an item and hit enter, only the previous path
I set in the script is actually saved to the "tmpfile" not what I select. Example:

dialog --title "source" --dselect /home/ 10 15 2>"/tmp/UserDir_$(date +%d.%m.%Y)"

It shows on the top '.' '..' and 'myusername'. I select then my username and hit enter, but what is saved is /home/ not /home/myusername.


#!/bin/env bash
## USAGE  : SUDO PATH/TO/BACKUPD
## BACKUPD: A SYSTEM BACKUP AND RESTORE SCRIPT USING DIALOG 
## Note   : Some paths like path/to/file/toExcludeFrom and where/to/save/your/backups
##          should be changed before runing the script
## WIKI   : https://wiki.archlinux.org/index.php/Rsync
##          https://wiki.archlinux.org/index.php/Full_system_backup_with_rsync


# Set parameters
if ! which dialog &> /dev/null
  then echo "Install Dialog pkg to proceed!" && exit 1
fi
if ! which rsync &> /dev/null
  then echo "Install rsync pkg to proceed!" && exit 1
fi
Answer=/tmp/RsyncSelectOpt_$(date '+%Y-%m-%d-%H.%M.%S.%N'); touch "$Answer";
SystemBackupDir=/tmp/RsyncSystemBackupDir_$(date '+%Y-%m-%d-%H.%M.%S.%N'); touch "$SystemBackupDir";
HomeBackupDir=/tmp/RsyncHomeBackupDir_$(date '+%Y-%m-%d-%H.%M.%S.%N'); touch "$HomeBackupDir";
HomeDir=/tmp/RsyncHomeDir_$(date '+%Y-%m-%d-%H.%M.%S.%N'); touch "$HomeDir";
ExcludeFrom=/tmp/RsyncExcludeFrom_$(date '+%Y-%m-%d-%H.%M.%S.%N'); touch "$ExcludeFrom";
Args='-aAXHh --numeric-ids --info=progress2 --delete'

#Options
dialog --backtitle "RSYNC SYSTEM BACKUP AND RESTORE" --title "What do you want to do? Backup...Restore?" --menu "Select your option:" 15 50 6 \
1 "Backup Home" 2 "Backup System" 3 "Backup System and Home" 4 "Restore System" 5 "Restore Home" 6 "Quit" 2>"$Answer"

A=$(< "$Answer")
   case $A in
       1) # "Backup Home" (/home/ only).  
       dialog --backtitle "Backup Home" --title "ExcludeFrom File" --fselect /home/ 10 40 2>"$ExcludeFrom"
       dialog --backtitle "Backup Home" --title "Which User Dir to backup?" --dselect /home/ 20 40 2>"$HomeDir"
       dialog --backtitle "Backup Home" --title "Home Backup Destination" --dselect /run/media/$USER/ExtDrive/ 20 40 2>"$HomeBackupDir"
       dialog --backtitle "RSYNC SYSTEM BACKUP AND RESTORE" --title "Backup Home" --infobox "Transfering Home\nPlease wait..." 10 20; sleep 2
       rsync $Args --exclude-from="$(< $ExcludeFrom)" "$(< $HomeDir)" "$(< $HomeBackupDir)"
       dialog --backtitle "RSYNC SYSTEM BACKUP AND RESTORE" --title "BACKUP HOME" --infobox "Home Backup finished." 5 30
          ;;
      2) # "Backup System"
       dialog --backtitle "System Backup" --title "System Destination" --dselect /run/media/$USER/ExtDrive/ 10 50 2>"$SystemBackupDir"
       dialog --backtitle "RSYNC SYSTEM BACKUP&RESTORE" --title "Backup System" --infobox 'Backing up the rootfs\nPlease wait...' 10 30; sleep 2
       rsync $Args --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found","/home/*"} / "$(< $SystemBackupDir)"
       dialog --backtitle "RSYNC SYSTEM BACKUP&RESTORE" --title "Backup System" --infobox 'System Backup finished!' 10 20
          ;;
      3) # "Backup System AND Home"
       dialog --backtitle "Backup Home" --title "ExcludeFrom File" --fselect /home/  10 40 2>"$ExcludeFrom"
       dialog --backtitle "Backup Home" --title "Which User Dir to backup?" --dselect /home/ 20 40 2>"$HomeDir"
       dialog --backtitle "Backup Home" --title "Home Backup Destination" --dselect /run/media/$USER/ExtDrive/ 20 40 2>"$HomeBackupDir"
       dialog --backtitle "System Backup" --title "System Destination" --dselect /run/media/$USER/ExtDrive/ 10 50 2>"$SystemBackupDir"
       dialog --backtitle "RSYNC SYSTEM BACKUP AND RESTORE" --title "Backup System AND Home" --infobox "Backing up System\nPlease wait..." 10 40; sleep 2
       rsync $Args --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found","/home/*"} / "$(< $SystemBackupDir)"
       dialog --backtitle "Backup System AND Home" --title "Backup System" --infobox 'System Backup finished!' 10 20; sleep  2
       dialog --backtitle "Backup System AND Home" --title "Backup Home" --infobox "Backing up Home\nPlease wait..." 10 30; sleep 2
       #Backup /home/ only.
       rsync $Args --exclude-from="$(< $ExcludeFrom)" "$(< $HomeDir)" "$(< $HomeBackupDir)"
       dialog --backtitle "Backup System AND Home" --title "Backup Home" --infobox "System and Home Backup finished" 5 40
          ;;
       4) # "Restore System"
       dialog --backtitle "Restore System" --title "Restore System From" --dselect /run/media/$USER/ExtDrive/ 10 50 2>"$SystemBackupDir"
       dialog --backtitle "RSYNC SYSTEM BACKUP AND RESTORE" --title "Restore System" --infobox "Restoring the rootfs\nPlease wait..." 10 40; sleep 2
       rsync $Args --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found","/home/*"} "$(< $SystemBackupDir)" /
       dialog --backtitle "RSYNC SYSTEM BACKUP AND RESTORE" --title "Restore System" --infobox 'Restoring the rootfs finished!' 10 40; sleep 2
          ;;
       5) # "Restore Home"
       dialog --backtitle "Restore Home" --title "ExcludeFrom File" --fselect /home/ 10 40 2>"$ExcludeFrom"
       dialog --backtitle "Restore Home" --title "Home Backup Location To Restore From" --dselect /run/media/$USER/ExtDrive/ 20 40 2>"$HomeBackupDir"
       dialog --backtitle "Restore Home" --title "Which User Dir to Restore?" --dselect /home/ 20 40 2>"$HomeDir"
       dialog --backtitle "RSYNC SYSTEM BACKUP AND RESTORE" --title "Restore Home" --infobox "Restoring Home\nPlease wait..." 10 40; sleep 2
       #Restore /home/ only.
       rsync $Args --exclude-from="$(< $ExcludeFrom)" "$(< $HomeBackupDir)" "$(< $HomeDir)"
       dialog --backtitle "Rsync System Backup And Restore" --title "Restore Home" --infobox 'Home Restore finished!' 5 40
          ;;
       6) # "Quit"
       dialog --backtitle "RSYNC SYSTEM BACKUP AND RESTORE" --title "Cancelling" --infobox 'Cancelled, Nothing done!' 5 40
         ;;
    esac
 
dialog --backtitle "RSYNC SYSTEM BACKUP AND RESTORE" --title "INFO" --infobox 'Task completed!' 5 30

Offline

Board footer

Powered by FluxBB