Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Script responsible for making essential backups onto USB thumb drive
- # when mounted. Will be executed by udev .rules file.
- #
- # [ USB drive info: sudo udevadm info -a -p $(sudo udevadm info -q path -n /dev/sdb) ]
- ########################################################################
- mnt="/media/USB_mountpoint" # Mountpoint of USB drive
- src="$HOME/Documents" # Dir containing files to be backed up
- dest="$mnt/.backups" # Backup destination on the USB drive
- rules_file="$dest/.rsync_USB_pendrive.dat" # Rsync --include-from rulefile
- ########################################################################
- sleep 5
- zero=${0##*/} # (same as running `basename $0`)
- lockdir="/tmp/$zero.lock" # If existing, other instances won't get started
- export DISPLAY=:0.0
- count=0
- # Start the script only if it's the only instance running:
- if ! mkdir "$lockdir"; then
- notify-send -t 3000 -i gtk-dialog-info \
- "more than 1 instance running, skipping this one"
- clear
- echo "Error: another instance of this script is running. If you're not aware of any"
- echo " other processes, run 'killall $zero'"; exit 0
- else
- trap 'rm -rf "$lockdir"' EXIT # remove lockdir on exit
- fi
- # Check whether required programs are installed:
- for i in notify-send rsync; do
- if ! command -v $i >/dev/null; then
- printf "ERROR\n$i does not appear to be installed on your system. Abort.\n" >&2
- notify-send -t 0 -i gtk-dialog-error \
- "ERROR from $zero script: $i does not appear to be installed on your system. Abort."; exit 1
- fi
- done
- # Wait for some time until drive gets mounted:
- until [ -w $mnt ]; do
- if [[ "$count" -ge "5" ]]; then
- notify-send -t 0 -i gtk-dialog-error "Error from $zero:" \
- "TOSHIBA USB script was started, but mountpoint at $mnt was not found!"
- echo -e "Error from $zero:\nTOSHIBA USB script was started, but"
- echo "mountpoint at $mnt wasn't found. Abort."; exit 1
- fi
- sleep 3
- let count=$count+1
- done
- # Display warning message if rsync rules file is not found:
- if [ ! -f $rules_file ]; then
- echo "Error: rsync rules file ($rules_file) not found!"
- notify-send -t 0 -i gtk-dialog-error "Error from $zero:" \
- "rsync rules file ($rules_file) not found. Abort."; exit 1
- fi
- # Give feedback...:
- notify-send -t 5000 -i gtk-dialog-info "Backing up $src to $dest..."
- # First make backups of small text files (extensions included in $rules_file):
- rsync -ravh --delete --delete-before --max-size=50K \
- --include-from="$rules_file" --exclude="*.*" $src $dest
- syncstat1=$?
- # Now sync .odt, .doc*, .ods and .xls (up to 20megs this time):
- rsync -ravh --delete --delete-before --max-size=20M --include=*/ --include=*.odt \
- --include=*.doc* --include=*.ods --include=*.xls --exclude="*" $src $dest
- syncstat2=$?
- # ...and finally back up KeePass password containers:
- rsync -ravh --include=*/ --include=*.kdbx --exclude="*" $src $dest
- syncstat3=$?
- # Check whether any of the syncs finished with an error:
- count=1
- for i in $syncstat1 $syncstat2 $syncstat3; do
- if [[ "$i" != "0" ]]; then
- notify-send -t 5000 -i gtk-dialog-error "FYI: rsync error '$i' from $zero" \
- "(error came from $count. sync command)"
- error_tag="True"
- fi
- let count=$count+1
- done
- # Final feedback:
- if [[ "$error_tag" == "True" ]]; then
- notify-send -t 0 -i gtk-dialog-error "Backup to $dest completed with" \
- "rsync error(s)"; exit 1
- else
- notify-send -t 5000 -i gtk-dialog-info "Backup to $dest completed successfully!"; exit 0
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement