48

I want to do one way synchronization.

I am having Folder A on my computer which is constantly updated with content.
Another Folder B is used for backup purpose which is on external HDD.

Now what I expect is that whatever extra which is present in folder A should go to folder B. However something which is present in B and NOT in A ""shall NOT be copied to A"".

In a nutshell, the backup folder may copy everything from the source folder, however nothing should be copied form backup folder to the source.

Braiam
  • 67,791
  • 32
  • 179
  • 269
BhaveshDiwan
  • 11,026

9 Answers9

65

Sounds like a perfect task for rsync

sudo rsync -az /path_to/A /path_to/B

-a archive mode (implies recursive, copy symlinks as symlinks, preserve owner, modification times, group, owner, special and device files)

-z compresses the data

If you wish to remove files deleted in A from files in B, use the --delete option

For additional information see:

https://help.ubuntu.com/community/rsync

You can run rsync from cron

sudo crontab -e

Add in an hourly task

@hourly rsync /path_to/A /path_to/B

https://help.ubuntu.com/community/CronHowto

Byte Commander
  • 107,489
Panther
  • 102,067
  • how can I make a bash script to this so that everytime i connect my portable HDD containing folder B (backup folder), the script runs and copies from A whatever is missing in B. – BhaveshDiwan Jun 06 '12 at 12:48
  • alternatively to the above comment... some script which i may manually execute after connecting the drive is also reasonable.. – BhaveshDiwan Jun 06 '12 at 12:48
  • 7
    If there are folders within folders you will need to use the -r option. – John S Gruber Jun 06 '12 at 13:32
  • You can make a script by entering it in a file, precede the command with the line: #!/bin/bash and add the execute permission to the file with the chmod command or using the Ubuntu graphic user interface (File properties--Permission tab). If the file name is 'rs' you can then execute it with ./rs – John S Gruber Jun 06 '12 at 13:38
  • 2
    I'm a bit late to the party, but to run the synchronisation script when the hard drive is connected you should use udev. Check out this question from a while ago. http://askubuntu.com/questions/25071/how-to-run-a-script-when-a-distinct-flash-drive-is-mounted – jackweirdy Mar 02 '13 at 13:32
  • 1
    @JohnSGruber the -a option implies recursive, I'll edit the answer to point this out – Tomas May 31 '13 at 00:01
  • @Tomas, absolutely correct. Thanks for pointing that out and for editing the answer. – John S Gruber Jun 27 '13 at 16:44
  • 1
    if the files are on the same machine -z just slow down the process – Postadelmaga May 06 '16 at 06:17
  • If i delete something in the source directory, will it get deleted in the backup directory? If it does. Is it possible to disable these deletes? – Jo Smo Oct 14 '16 at 15:53
  • 1
    @JoSmo - http://askubuntu.com/questions/476041/how-do-i-make-rsync-delete-files-that-have-been-deleted-from-the-source-folder and http://askubuntu.com/questions/609968/rsync-delete-option-doesnt-delete-files-in-target-directory and man rsync and ask a new question if needed rather then asking in comments. – Panther Oct 14 '16 at 15:56
15

I'd suggest using rsync for this purpose. Rsync is extremely fast, stable, and versatile. There's a good introduction at http://help.ubuntu.com/community/rsync

If you wish, there is an optional graphic front end: grsync

 sudo rsync -azv --exclude 'dir1' /home/path/folderA/ /home/path/folderB

The command above will copy from folderA to folderB excluding dir1. The flags are

-a preserves time stamps
-z is to enable compression
-v verbose

There are many more options available.

JIm
  • 521
8

I always found Unison to be very helpful. It has a text based or GUI based interface, and quite a few different options to tweak it to what you want (with a little fiddling). It takes quite a bit of time to do the first sync, but after that it's brilliant. You can make it sync one-way, as you want, but it will pretty much get that automatically. It can also delete from the backup or not as you choose.

You also may find issues with permissions which are supported in the ubuntu file format, but maybe not in the external hard drive (depending whether the external hard drive is going to be used in a windows machine, this may be a good thing), so you'll want to sync without the permissions potentially.

Anyway, the nice thing is that with the tutorial it's relatively straightforward to set it up once, and thereafter it's a GUI interface whenever you want to do it.

Here's some info about it: http://www.ubuntugeek.com/unison-file-synchronization-tool.html

And here's the tutorial: http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#tutorial

4
rsync -avPr --ignore-existing /home/username/Research/ /path/to/other/folder/on/hdd/

This will sync and copy only that data that doesn't exist in the /other/folder/on/hdd/

dearN
  • 2,179
2

I agree with the other answers, you can use in rsync in Terminal or the interfaces Grsync, luckyBackup, Conduit or the famous Unison.

Another great app is Krusader (a Twin-Panel File Manager for KDE), in the Tools menu you can find "Syncronize Directories", is very useful.

In last instance you can install Wine and install another great twin-pane file manager like Total Commander.

Anyway you have plenty of options and all of them are present in the Ubuntu Software Center.

1

I would recommend Conduit for simple synchronization. It's available the software system. It does exactly what you are looking for

jasonwert
  • 375
0

There is a quite handy shell tool called rsnapshot - http://www.rsnapshot.org/ - filesystem snapshot utility for making backups of local and remote systems. that uses rsync and hard links which makes it possible to keep multiple, full file system backups instantly available. Just do sudo apt-get install rsnapshot and check info rsnapshot

0

If you want a graphical interface on a system that is highly configurable, give FreeFileSync a try. See, for example: http://linuxnorth.wordpress.com/2011/11/29/file-and-folder-synchronization/ In particular, you want the "Update" option for synchronization that will "Copy new or updated files to right folder", i.e. copy from the left folder to the right folder in a two-window display.

CentaurusA
  • 2,672
0

You can sync files inside two direcotries by:

rsync -rv /path/to/directory1/ /path/to/directory2

Doing rsync -rv /path/to/directory1 /path/to/directory2 will create directory1 inside directory2, like this /path/to/directory2/directory1/[files]

You can dry run using -n switch, like this rsync -rnv /path/to/directory1/ /path/to/directory2

Reference: https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories-on-a-vps

subhojit777
  • 182
  • 1
  • 8