Tuesday, October 19, 2010

Simple Rsync GUI: easy backups from Nautilus

Most often, making backups of your important files is a manual process. Especially if you are dealing with large collections of photos.

In the meantime I have written a small and convenient Nautilus script (for Gnome users) for doing exactly that.

Features:
  • Syncs to any mounted location or over SSH (everything that rsync supports)
  • Remembers previously used locations
  • Preview of changes (any deletions are shown first, but performed the last)
  • Nice progress bar with upload speed display

Everything is written as a simple bash script using Zenity for GTK GUI - just drop it to ~/.gnome2/nautilus-scripts directory, and it appear in Nautilus right-click menu, under Scripts.




Don't forget - this all is just a frontend for rsync (that you are too lazy to run from command-line).

Dependencies: nautilus, zenity, rsync, bash

And now, here is the source (save to ~/.gnome2/nautilus-scripts/Sync):
#!/bin/bash
# Nautilus script to sync specified folder to another destination via rsync.
# Put this to ~/.gnome2/nautilus-scripts
# Written by Anton Keks (BSD license)

paths_file=$(readlink -f $0).paths
locations=`cat $paths_file`
sources=`cat $paths_file | awk -F'|' '{print $1}'`

if [ "$1" ]; then
  source=$1 
else
  # add current directory also to the list
  sources=`echo -e "$sources\\n$PWD" | sort -u`
  # ask user to chose one of the sources
  source=`zenity --list --title="Sync source" --text="No source was specified. Please choose what do you want to sync" --column=Source "$sources" Other...` || exit 1
  if [ "$source" = Other... ]; then
    source=`zenity --entry --title="Sync source" --text="Please enter the source path on local computer" --entry-text="$PWD"` || exit 1
  fi
fi

# normalize and remove trailing /
source=`readlink -f "$source"`
source=${source%/}

if [ ! -d "$source" ]; then
  zenity --error --text="$source is not a directory"; exit 2
fi

if [ $2 ]; then
  # TODO: support multiple sources
  zenity --warning --text="Only one directory can be synched, using $source"
fi

# find matching destinations from stored ones
destinations=""
for s in $sources; do
  if echo "$source" | fgrep $s; then
    dest=`fgrep "$s" $paths_file | awk -F'|' '{print $2}'`
    suffix=${source#$s}
    suffix=${suffix%/*}
    destinations="$destinations $dest$suffix" 
  fi
done

# ask user to chose one of the matching destinations of enter a new one
dest=`zenity --list --title="Sync destination" --text="Choose where to sync $source" --column=Destination $destinations New...` || exit 3
if [ $dest = New... ]; then
  basename=`basename "$source"`
  dest=`zenity --entry --title="Sync destination" --text="Please enter the destination (either local path or rsync's remote descriptor), omitting $basename" --entry-text="user@host:$(dirname $source)"` || exit 3
  echo "$source|$dest" >> $paths_file
fi

# check if user is not trying to do something wrong with rsync
if [ `basename "$source"` = `basename "$dest"` ]; then
  # sync contents of source to dest
  source="$source/"
fi

log_file=/tmp/Sync.log
rsync_opts=-rltEorzh
echo -e "The following changes will be performed by rsync (see man rsync for info on itemize-changes):\\n$source -> $dest\\n" > $log_file
( echo x; rsync -ni $rsync_opts --delete "$source" "$dest" 2>&1 >> $log_file; rsync_result=$? ) | zenity --progress --pulsate --auto-close --width=350 --title="Retrieving sync information" 

if [ $rsync_result -ne 0 ]; then
  zenity --error --title="Sync" --text="Rsync failed: `cat $log_file`"; exit 4
fi

num_files=`cat $log_file | wc -l`
num_files=$((num_files-3))

if [ $num_files -le 0 ]; then
  zenity --info --title="Sync" --text="All files are up to date on $dest"; exit
fi

zenity --text-info --title="Sync review ($num_files changes)" --filename=$log_file --width=500 --height=500 || exit 4

num_deleted=`fgrep delet $log_file | wc -l`
if [ $num_deleted -ge 100 ]; then
  zenity --question --title="Sync" --text="$num_deleted files are going to be deleted from $dest, do you still want to continue?" --ok-label="Continue" || exit 4
fi

rsync_progress_awk="{ 
 if (\$0 ~ /to-check/) {
  last_speed=\$(NF-3)
 }
 else {
  print \"#\" \$0 \" - \" files \"/\" $num_files \" - \" last_speed;
  files++;
  print files/$num_files*100 \"%\";
 }
 fflush();
}
END {
 print \"#Done, \" files \" changes, \" last_speed
}"

# note: delete-delay below means that any files will be deleted only as a last step
rsync $rsync_opts --delete-delay --progress "$source" "$dest" | awk "$rsync_progress_awk" | zenity --progress --width=350 --title="Synchronizing $source" || exit 4

28 comments:

  1. Have you tried unison or grsync? Are they inconvenient/not up to the job?

    ReplyDelete
  2. Интересная мысль, возьму на заметку.

    ReplyDelete
  3. Stydaymaf

    [url=http://healthplusrx.com/gotu-kola-facts]gotu kola facts[/url] GrertMekfef

    ReplyDelete
  4. одноклассники ru порно :-]







    порнокласники 623874
    629 Порно одноклассники
    порно одноклассники скачать 1693

    ReplyDelete
  5. www odnotrahniki :(







    odnolubovniki ru 765314
    293 odnotraxniki narod ru
    odnolubovniki ru 7983

    ReplyDelete
  6. Obtain and select some good things from you and it helps me to solve a problem, thanks.

    - Henry

    ReplyDelete
  7. Thans

    for the nice post.

    ReplyDelete
  8. Спасибо понравилось ! Thanks !

    ReplyDelete
  9. Hi, visualize me on lisachu

    http://www.teamsourcing.com/forum/profile.php?mode=viewprofile&u=91560 www.montrealaviation.com/phpBB2/profile.php?mode=viewprofile&u=71866

    ReplyDelete
  10. My cousin recommended this blog and she was totally right keep up the fantastic work!

    ReplyDelete
  11. If you are open to having a guest blog poster please reply and let me know. I will provide you with unique content for your blog, thanks.

    ReplyDelete
  12. Thanks for an idea, you sparked at thought from a angle I hadn’t given thoguht to yet. Now lets see if I can do something with it.

    ReplyDelete
  13. Está tudo, li a tua pagina e apreciei tanto,penso que estás a informar bem!
    Para a frente com o espectacular blogue que tens!
    Adeus

    ReplyDelete
  14. I’ll definitely be watching you!

    ReplyDelete
  15. It’s too cool and beautiful to develop and motivate, build up character.

    ReplyDelete
  16. Fantastic article as always, I am seriously considering something along the lines of an affiliate section on my own site… so very timely post.

    ReplyDelete
  17. That was a awesome read,You discover something new every day.

    ReplyDelete
  18. Well … all I can say is, wow. This is an impressive collection of resources, thank you for taking the time to put everything together.

    ReplyDelete
  19. It was really inspiring I loved it, thanks a ton to bring me back and more closer to my real self and my family.

    ReplyDelete
  20. I’ll definitely be watching you!

    ReplyDelete
  21. Hi all at blog.azib.net. What you thinking about chicken recipes? rnexample: rnApple brandy chicken, made with chicken breast halves, apple brandy, cream, onions, and butter, along with mushrooms. rn 4 chicken breast halves rn rn salt and pepper rn 8 ounces sliced mushrooms rn 2 teaspoons olive oil rn 2 teaspoons butter rn 1/3 cup apple brandy, such as Apple Jack or Calvados rn 4 green onions, chopped rn 1/2 cup whipping cream or heavy cream rn 1 teaspoon fresh thyme leaves or 1/4 teaspoon dried leaf thyme rn rnPreparation: rnFlatten chicken; place chicken breast halves between pieces of plastic wrap and gently pound until thinned out and uniform in size. Sprinkle with salt and pepper. In a large heavy skillet, heat olive oil and butter over medium heat. Add chicken breasts. Cook for about 5 minutes, until browned, then turn. Add mushrooms and cook for about 5 minutes longer. Add green onions and apple brandy and cook for another minute, until chicken is cooked through and mushrooms are tender. Add cream and thyme; simmer until thickened. Taste and add salt and pepper if needed. rnHave you else any ideas? [url=http://freerecipesforchicken.info/]recipes for chicken[/url]

    ReplyDelete
  22. www.blogger.com should be 1st in google search results, i can help you

    [url=http://www.youtube.com/watch?v=iXGkoP3YiwM]best pay per download sites[/url]
    [url=http://www.youtube.com/watch?v=o0Stq4HjtiY]pay per download site[/url]
    [url=http://www.youtube.com/watch?v=vfFaXhdfhaA]pay per download program[/url]

    ReplyDelete
  23. [url=http://buyaccutaneorderpillsonline.com/#14197]accutane no prescription[/url] - buy cheap accutane , http://buyaccutaneorderpillsonline.com/#11091 buy accutane online

    ReplyDelete
  24. [url=http://buyaccutaneorderpillsonline.com/#11483]buy accutane[/url] - buy accutane online , http://buyaccutaneorderpillsonline.com/#15029 cheap accutane

    ReplyDelete
  25. [url=http://buyaccutaneorderpillsonline.com/#21160]accutane online without prescription[/url] - accutane no prescription , http://buyaccutaneorderpillsonline.com/#2214 order accutane

    ReplyDelete
  26. [url=http://buyonlinelasixone.com/#7013]lasix without prescription[/url] - order lasix , http://buyonlinelasixone.com/#5578 lasix cost

    ReplyDelete