Advertisement
Guest User

filesync.sh

a guest
Feb 14th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.79 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ##### Script to sync / backup my media downloads to a USB hard drive
  4. ##### 2016 Jeremy Sparks
  5.  
  6. clear
  7.  
  8. datevar=$( date +%r )
  9.  
  10. echo
  11. echo "Filesync script started at"$datevar" by user: "$USER""
  12.  
  13. ##### Performs a dry run of the rsync commands using a separate BASH script and counts the lines of output.
  14. ##### If this output is more than 50 then there must be files to transfer.
  15.  
  16.     dryrun=$( ./filesync/dry-run.sh | wc -l )
  17.  
  18.     if [ $dryrun -le 50 ]; then
  19.  
  20.     printf "\nAll files are already synced and up to date.\n\n"
  21.  
  22. ##### Prompts user for input.
  23.  
  24.     else
  25.  
  26.     printf "\n\nPlease select from the following file types to sync:\n"
  27.  
  28.     printf "\n--- documents\n--- scripts\n--- pictures\n--- music\n--- videos\n--- downloads\n--- all\n"
  29.  
  30.     printf "\nType documents, scripts, pictures, music, videos, downloads or all:\n\n"
  31.  
  32. ##### This script also takes input from command line using an "all" flag (useful for running on a Crontab job).
  33.  
  34.     if [ $# -lt 1 ]; then
  35.  
  36.     read input
  37.  
  38.         case $input in
  39.  
  40. ##### Runs rsync to sync everything from the USB HDD to the PC, then the other way.
  41. ##### Files will be updated if they are larger in file size but timestamps are ignored (with the exception of documents and scripts).
  42.  
  43.             documents)
  44.             printf "\nDocuments option selected\n\n"
  45.             rsync -havp --progress --update /media/jeremy/SAMSUNG/Documents/ /home/jeremy/Documents/
  46.             rsync -havp --progress --update /home/jeremy/Documents/ /media/jeremy/SAMSUNG/Documents/
  47.             ;;
  48.             scripts)
  49.             printf "\bScripts option selected\n\n"
  50.             rsync -havp --progress --update /media/jeremy/SAMSUNG/bin/ /home/jeremy/bin/
  51.             rsync -havp --progress --update /home/jeremy/bin/ /media/jeremy/SAMSUNG/bin/
  52.             ;;
  53.             pictures)
  54.             printf "\nPictures option selected\n\n"
  55.             rsync -havp --progress --size-only /media/jeremy/SAMSUNG/Pictures/ /home/jeremy/Pictures/
  56.             rsync -havp --progress --size-only /home/jeremy/Pictures/ /media/jeremy/SAMSUNG/Pictures/
  57.             ;;
  58.             music)
  59.             printf "\nMusic option selected\n\n"
  60.             rsync -havp --progress --size-only /media/jeremy/SAMSUNG/Music/ /home/jeremy/Music/
  61.             rsync -havp --progress --size-only /home/jeremy/Music/ /media/jeremy/SAMSUNG/Music/
  62.             ;;
  63.             videos)
  64.             printf "\nVideos option selected\n\n"
  65.             rsync -havp --progress --size-only /media/jeremy/SAMSUNG/Videos/ /home/jeremy/Videos/
  66.             rsync -havp --progress --size-only /home/jeremy/Videos/ /media/jeremy/SAMSUNG/Videos/
  67.             ;;
  68.             downloads)
  69.             printf "\nDownloads option selected\n\n"
  70.             rsync -havp --progress --size-only  /media/jeremy/SAMSUNG/Downloads/ /home/jeremy/Downloads/
  71.             rsync -havp --progress --size-only /home/jeremy/Downloads/ /media/jeremy/SAMSUNG/Downloads/
  72.             ;;
  73.             all)
  74.             printf "\nSync all option selected\n\n"
  75.             rsync -havp --progress --update /media/jeremy/SAMSUNG/Documents/ /home/jeremy/Documents/
  76.             rsync -havp --progress --update /home/jeremy/Documents/ /media/jeremy/SAMSUNG/Documents/
  77.  
  78.             rsync -havp --progress --update /media/jeremy/SAMSUNG/bin/ /home/jeremy/bin/
  79.             rsync -havp --progress --update /home/jeremy/bin/ /media/jeremy/SAMSUNG/bin/
  80.  
  81.             rsync -havp --progress --size-only /media/jeremy/SAMSUNG/Pictures/ /home/jeremy/Pictures/
  82.             rsync -havp --progress --size-only /home/jeremy/Pictures/ /media/jeremy/SAMSUNG/Pictures/
  83.  
  84.             rsync -havp --progress --size-only /media/jeremy/SAMSUNG/Music/ /home/jeremy/Music/
  85.             rsync -havp --progress --size-only /home/jeremy/Music/ /media/jeremy/SAMSUNG/Music/
  86.  
  87.             rsync -havp --progress --size-only /media/jeremy/SAMSUNG/Videos/ /home/jeremy/Videos/
  88.             rsync -havp --progress --size-only /home/jeremy/Videos/ /media/jeremy/SAMSUNG/Videos/
  89.  
  90.             rsync -havp --progress --size-only  /media/jeremy/SAMSUNG/Downloads/ /home/jeremy/Downloads/
  91.             rsync -havp --progress --size-only /home/jeremy/Downloads/ /media/jeremy/SAMSUNG/Downloads/
  92.             ;;
  93.             *)
  94.             printf "\nSorry, your input was not recognised.\n\n"
  95.             ;;
  96.  
  97.         esac
  98.  
  99.     printf "\nThank you. Your file sync has been completed.\n\n"
  100.  
  101. exit 0
  102.  
  103.     else
  104.  
  105. ##### Code to run if the "all" argument is passed from the command line.
  106.  
  107.         case "$1" in
  108.  
  109.             all)
  110.             rsync -havp --progress --update /media/jeremy/SAMSUNG/Documents/ /home/jeremy/Documents/
  111.             rsync -havp --progress --update /home/jeremy/Documents/ /media/jeremy/SAMSUNG/Documents/
  112.  
  113.             rsync -havp --progress --update /media/jeremy/SAMSUNG/bin/ /home/jeremy/bin/
  114.             rsync -havp --progress --update /home/jeremy/bin/ /media/jeremy/SAMSUNG/bin/
  115.  
  116.             rsync -havp --progress --size-only /media/jeremy/SAMSUNG/Pictures/ /home/jeremy/Pictures/
  117.             rsync -havp --progress --size-only /home/jeremy/Pictures/ /media/jeremy/SAMSUNG/Pictures/
  118.  
  119.             rsync -havp --progress --size-only /media/jeremy/SAMSUNG/Music/ /home/jeremy/Music/
  120.             rsync -havp --progress --size-only /home/jeremy/Music/ /media/jeremy/SAMSUNG/Music/
  121.  
  122.             rsync -havp --progress --size-only /media/jeremy/SAMSUNG/Videos/ /home/jeremy/Videos/
  123.             rsync -havp --progress --size-only /home/jeremy/Videos/ /media/jeremy/SAMSUNG/Videos/
  124.  
  125.             rsync -havp --progress --size-only  /media/jeremy/SAMSUNG/Downloads/ /home/jeremy/Downloads/
  126.             rsync -havp --progress --size-only /home/jeremy/Downloads/ /media/jeremy/SAMSUNG/Downloads/
  127.             ;;
  128.  
  129.         esac
  130.  
  131.     printf "\nThank you. Your file sync has been completed.\n\n"
  132.  
  133.     fi
  134.  
  135.     fi
  136.  
  137. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement