Ubuntu – How to make silent backups when plugging in a USB disk and when using Back In Time

backupbashudevusb

Actually, I already answered that question, though I don't know if the answer is a good one. So what do you think about this:

  • Task: let the computer start a backup using Back In Time (BIT) as silently and as easy as possible to obtain the highest possible WAF*
  • Solution: use udev to start a backup script like this one when a specific USB disk is plugged in:

.

#!/bin/bash
# usage: backup_userdata.sh username

export _USER=$1

# config file, Xauthoriy and DISPLAY    
export BIT_CONFIG=/home/$_USER/.config/backintime/config
if ! test -e $BIT_CONFIG ; then exit 1;fi

# set DISPLAY
export XAUTHORITY=/home/$_USER/.Xauthority
export $(cat /proc/$(pgrep -o -u $_USER unity)/environ | tr '\0' '\n' | grep '^DISPLAY=').0

# dont auto-open backup USB disk - couldnt find a better solution for this  
su $_USER -c 'gsettings set org.gnome.desktop.media-handling automount-open false'          
{ sleep 10; su $_USER -c 'gsettings set org.gnome.desktop.media-handling automount-open true' ; }&

# send the rest to the background so udev doesnt wait for the backup to end
{   
    export BIT_LOG=/home/$_USER/.local/share/backintime/takesnapshot_.log

    # strings for pop ups
    export STARTTEXT="Backup started"
    export SUCCESSTEXT="Backup completed"
    export WARNINGTEXT="Backup completed with errors\nRevise results\nExternal disk still mounted"
    export ERRORTEXT="Backup aborted\nRevise results\nExternal disk still mounted"
    export BIT="Back In Time"   

    # back up media     
    export BIT_MEDIA="/media/backup"


    # start backup
    su $_USER -c 'notify-send "$BIT" "$STARTTEXT"'
    if su $_USER -c 'backintime -b --config $BIT_CONFIG'; then
        if ! grep -iq "error: " $BIT_LOG; then 
            # no errors in log file
            umount $BIT_MEDIA # umount before confirmation
            su $_USER -c 'notify-send "$BIT" "$SUCCESSTEXT"'
            su $_USER -c 'zenity --no-wrap --info --title "$BIT" --text "\n$SUCCESSTEXT"'
        else    
            # errors in log file
            su $_USER -c 'notify-send "$BIT" "$WARNINGTEXT"'
            su $_USER -c 'zenity --no-wrap --warning --title "$BIT" --text "\n$WARNINGTEXT"'
            su $_USER -c backintime-gnome # open BIT for error analysis 
        fi
    else    
        # backup aborted
        su $_USER -c 'notify-send "$BIT" "$ERRORTEXT"'
        su $_USER -c 'zenity --no-wrap --error --title "$BIT" --text "\n$ERRORTEXT"'
        su $_USER -c backintime-gnome # open BIT for error analysis
    fi  
}&

The scipt works. The only problem left, as far as I can see, is that the version of BIT that I'm running on my 12.04 doesn't always use an exit status. But the latest version should do (I can't test it at the moment).

Any comments?

"*" Woman Acceptance Factor

Best Answer

Starting with version 1.0.28 BackInTime support udev schedules by its own. Just select When drive get connected (udev) in Settings > Schedule and you're done...

Disclaimer: I'm the current main Dev of BIT

Related Question