Advertisement
layr

USB_pendrive_sync

Dec 28th, 2012
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.56 KB | None | 0 0
  1. #!/bin/bash
  2. # Script responsible for making essential backups onto USB thumb drive
  3. # when mounted. Will be executed by udev .rules file.
  4. #
  5. # [ USB drive info:     sudo udevadm info -a -p $(sudo udevadm info -q path -n /dev/sdb) ]
  6. ########################################################################
  7. mnt="/media/USB_mountpoint"       # Mountpoint of USB drive
  8. src="$HOME/Documents"   # Dir containing files to be backed up
  9. dest="$mnt/.backups"    # Backup destination on the USB drive
  10.  
  11. rules_file="$dest/.rsync_USB_pendrive.dat" # Rsync --include-from rulefile
  12. ########################################################################
  13. sleep 5
  14. zero=${0##*/}   # (same as running `basename $0`)
  15. lockdir="/tmp/$zero.lock" # If existing, other instances won't get started
  16. export DISPLAY=:0.0
  17. count=0
  18.  
  19. # Start the script only if it's the only instance running:
  20. if ! mkdir "$lockdir"; then
  21.     notify-send -t 3000 -i gtk-dialog-info \
  22.     "more than 1 instance running, skipping this one"
  23.     clear
  24.     echo "Error: another instance of this script is running. If you're not aware of any"
  25.     echo "       other processes, run 'killall $zero'"; exit 0
  26. else
  27.     trap 'rm -rf "$lockdir"' EXIT # remove lockdir on exit
  28. fi
  29.  
  30. # Check whether required programs are installed:
  31. for i in notify-send rsync; do
  32.     if ! command -v $i >/dev/null; then
  33.         printf "ERROR\n$i does not appear to be installed on your system. Abort.\n" >&2
  34.         notify-send -t 0 -i gtk-dialog-error \
  35.         "ERROR from $zero script: $i does not appear to be installed on your system. Abort."; exit 1
  36.     fi
  37. done
  38.  
  39. # Wait for some time until drive gets mounted:
  40. until [ -w $mnt ]; do
  41.     if [[ "$count" -ge "5" ]]; then
  42.         notify-send -t 0 -i gtk-dialog-error "Error from $zero:" \
  43.         "TOSHIBA USB script was started, but mountpoint at $mnt was not found!"
  44.         echo -e "Error from $zero:\nTOSHIBA USB script was started, but"
  45.         echo "mountpoint at $mnt wasn't found. Abort."; exit 1  
  46.     fi  
  47.     sleep 3
  48.     let count=$count+1
  49. done
  50.  
  51. # Display warning message if rsync rules file is not found:
  52. if [ ! -f $rules_file ]; then
  53.     echo "Error: rsync rules file ($rules_file) not found!"
  54.     notify-send -t 0 -i gtk-dialog-error "Error from $zero:" \
  55.     "rsync rules file ($rules_file) not found. Abort."; exit 1
  56. fi
  57.  
  58. # Give feedback...:
  59. notify-send -t 5000 -i gtk-dialog-info "Backing up $src to $dest..."
  60.  
  61. # First make backups of small text files (extensions included in $rules_file):
  62. rsync -ravh --delete --delete-before --max-size=50K \
  63. --include-from="$rules_file" --exclude="*.*" $src $dest
  64. syncstat1=$?
  65. # Now sync .odt, .doc*, .ods and .xls (up to 20megs this time):
  66. rsync -ravh --delete --delete-before --max-size=20M --include=*/ --include=*.odt \
  67. --include=*.doc* --include=*.ods --include=*.xls --exclude="*" $src $dest
  68. syncstat2=$?
  69. # ...and finally back up KeePass password containers:
  70. rsync -ravh --include=*/ --include=*.kdbx --exclude="*" $src $dest
  71. syncstat3=$?
  72.  
  73. # Check whether any of the syncs finished with an error:
  74. count=1
  75. for i in $syncstat1 $syncstat2 $syncstat3; do
  76.     if [[ "$i" != "0" ]]; then
  77.         notify-send -t 5000 -i gtk-dialog-error "FYI: rsync error '$i' from $zero" \
  78.         "(error came from $count. sync command)"
  79.         error_tag="True"
  80.     fi
  81.     let count=$count+1
  82. done
  83. # Final feedback:
  84. if [[ "$error_tag" == "True" ]]; then
  85.     notify-send -t 0 -i gtk-dialog-error "Backup to $dest completed with" \
  86.     "rsync error(s)"; exit 1
  87. else
  88.     notify-send -t 5000 -i gtk-dialog-info "Backup to $dest completed successfully!"; exit 0
  89. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement