Select Page

Synchronize data in Ubuntu using rsync

by | 29 May 2017 | Linux | 0 comments

Last time I bought a desktop PC to my small office. I wanted to have a PC which will be always in my office, because getting notebook every time is not comfortable. So I bought Dell OptiPlex 760 Form Factor (FF) with this configuration:

Intel® Core™2 Duo Processor E8400 2 x 3,0 Ghz, 6MB Cache, 4 GB RAM, 160 GB HDD.

I deleted Windows and I installed Ubuntu 16.04, of course.

Usually I use my notebook to work but from now I have two machines I’m working on. So there is a new problem connected with synchronization data. Sometimes I work on notebook, sometimes on desktop… Which files are current? BTW! I didn’t consider Dropbox and etc. because I don’t trust cloud – end of discussion.
First solution was Unison. It is a small application which can synchronize files between local and remote locations. For three days it worked perfectly. But suddenly I noticed that my files are corrupted, actually there were empty (0 bytes). First time I thought that it was connected with Windows (I plugged in my flash drive into the Windows machine). But I formatted flash drive again and started one more time to use Unison. After a few days – the same, there were files with 0 bytes.

Then I decided to say “I’m sorry” to the rsync.

So, now I use rsync to synchronize my data.
Pros:
1. It is universal app which is present in every Ubuntu and other Linux.
2. It is very fast.
3. Rather doesn’t make mistakes.

Cons:
1. You should operate in terminal.
2. I have to remember about synchronize with my flash drive every time before and after a work session.

I wrote a small bash script, I use it to synchronize my data between desktop, notebook and flash drive.

#!/bin/bash
# set an infinite loop

while :
do
	clear
        # display menu
        echo "Server Name - $(hostname)"
	echo "-------------------------------"
	echo "      RSYNC FLASHDRIVE"
	echo "     M A I N - M E N U"
	echo "-------------------------------"
	echo "p. from PC to FLASH"
	echo "f. from FLASH to PC" 
	echo ""
	echo "q. Exit"
	echo ""
	echo ""
	
        # get input from the user 
	read -p "Enter your choice [ 1 -4 ] " choice
        # make decision using case..in..esac 
	case $choice in

		f)	rsync -rtvu --delete --ignore-errors --stats --progress /media/dio/SANDISK/SanDisk-WP/ /home/dio/Documents/OptiPlex-WP/
			read -p "Press [Enter] key to continue..." readEnterKey
			;;

		p)	rsync -rtvu --delete --ignore-errors --stats --progress /home/dio/Documents/OptiPlex-WP/ /media/dio/SANDISK/SanDisk-WP/
			read -p "Press [Enter] key to continue..." readEnterKey
			;;
			
		q)
			echo "Bye!"
			exit 0
			;;
		*)
			echo "Error: Invalid option..."	
			read -p "Press [Enter] key to continue..." readEnterKey
			;;
	esac		
 
done

Now as you can see in the code above, I only have to plug in the flash drive (in my case SANDISK) and select an option. Before working session I select “f” from flash to PC. After a work I select “p” – from PC to flash drive. Maybe it is uncomfortable to run every time this script but I don’t see other solution at this moment.

OK, a quick explanation to the rsync options:

rsync -rtvu --delete --ignore-errors --stats --progress /home/dio/Documents/OptiPlex-WP/ /media/dio/SANDISK/SanDisk-WP/
  • r – recursive, I do not comment it
  • t – preserver timestamp, this means that the “accessed” parameter will not be changed
  • v – verbose, means “show me what are you doing now” on the screen
  • u – update, means update only (don’t overwrite newer files)
  • – -delete, delete the files in the destination, if I deleted them in the source
  • – -ignore-errors, no comments
  • – -stats, show me stats (how many files copied etc.)
  • – -progress, means show progress in percentage (rather used to see progress when you copy big files)

Follow us