Jump to content

How to sync files and folders on Ubuntu


wildweaselmi
 Share

Recommended Posts

So on my mac I use chronosync to sync files and folders between two drives and it does a great job of it but what do I use on a Linux build like Ubuntu or CentOS?



Research is pointing me to:



Rsync


Unison


  • Rsync is more complicated but works on every flavor of unix known to man and is what I should concentrate learning.
  • Unison is the lazy man (or woman) way of accomplishing a sync of files and folders between drives and is probably what I'll end up doing


I may try both and see what the results are. If/when I figure out rsync, I'll post the command I use to sync two external drives. The data was placed on the external drive with my mac and includes about 750GB of iTunes Music, Videos and Karaoke which I want available in the event the primary drive ever crashes, I want to be able to fire up the backup drive and use it to restore the data back to the primary drive.


Link to comment
Share on other sites

rsync attempt



The simplest method for backing up over a network is to use rsync via SSH (using the -e ssh option). Alternatively, you can use the rsync daemon (see Rsync Daemon which requires much more configuration. Local backup only requires rsync and read/write access to the folders being synchronized. Below you will find examples of commands that can be used to backup in either case. It should be noted, that a network sync can be performed locally so long as the folder is shared (say by Samba) and then mounted to the machine with folder1. This process gets around having to use ssh but is less secure and should only be used in secure private networks, like at your home.





Local Backup



sudo rsync -azvv /home/path/folder1/ /home/path/folder2










Backup Over Network



sudo rsync --dry-run --delete -azvv -e ssh /home/path/folder1/ remoteuser@remotehost.remotedomain:/home/path/folder2



An explanation of above options to commands:



  • --dry-run This tells rsync to not actually do anything. It will just write a log of what it would do to the screen. Once you've made sure everything will work as you expect, you have to remove this option, and run the command again to perform the actual backup.
  • --delete deletes files that don't exist on the system being backed up.(Optional)
  • -a preserves the date and times, and permissions of the files (same as -rlptgoD).
  • With this option rsync will:
    • Descend recursively into all directories (-r),
    • copy symlinks as symlinks (-l),
    • preserve file permissions (-p),
    • preserve modification times (-t),
    • preserve groups (-g),
    • preserve file ownership (-o), and
    • preserve devices as devices (-D).
    • -z compresses the data
    • -vv increases the verbosity of the reporting process
    • -e specifies remote shell to use
    • /folder1 and folder2 In the examples above, folder1 and 2 are placeholders for the directories to be synchronized. Folder1 is the original folder, and 2 is the new folder, or existing one to be brought in sync with the first. Replace them with the folders you'd like. A / was added after folder1 so that only the contents, rather than whole folder, would be moved into the second.

A complete synopsis of all the options with the rsync command can be found in the man pages under "Options Summary".







Link to comment
Share on other sites

RSYNC update
Okay here is what I did to get this to BACKUP LOCAL to a REMOTE server.

ON THE REMOTE SERVER:

  1. go into root mode: su
  2. create a user on the server (if you don't have one). I created one called backupdude. useradd backupdude then: passwd backupdude
  3. create a directory you want to store backups in (i created /home/backups/servername1) mkdir /home/backups/servername1
  4. give ownership of that backup directory to your backup username (example: backupdude) chown -R backupdude /home/backups


ON THE LOCAL MACHINE

  1. go into root mode: su
  2. copy files from /var/www to remote server at 192.168.1.77 type: rsync -r -a -v -e "ssh -l backupdude" --delete /var/www 192.168.1.77:/home/backups/servername1

NOTE: it goes from where to where so the restore will be the same way so you are really just swapping the last part of this command
NOTE: If you create any other directories under /home/backups you need to rerun the chown -R backupdude /home/backups to be able to write to the new directory


Okay here is what I did to RESTORE REMOTE server to LOCAL


ON THE LOCAL MACHINE
go into root mode: su
copy files from remote server at 192.168.1.77 to local server type: rsync -r -a -v -e "ssh -l backupdude" --delete 192.168.1.77:/home/backups/servername1/www /var

NOTE: if you find you keep finding directory in the wrong place on your local machine and want to delete it and try again use this command to delete everything in that directory and below: rm -rf /var/www/www

 

Link to comment
Share on other sites

RSYNC EXAMPLE:





I wanted to copy the contents of an external drive connected locally to my Ubuntu laptop to a CentOS Server. This is what I did...





# su



# rsync -r -a -v -e "ssh -l backupdude" --delete /media/crates-17/ 192.168.1.77:/home/backups/crates





external drive volume is labeled crates-17 and I wanted everything under that directory to be backed up to a folder on the server called crates. If I didn't include the / after crates-17 then on the server it would of created a directory called crates-17 under crates and thats annoying to me so the / exists to stop that.





Now I have another external drive labeled crates-15 that was suppose to be a backup of crates-17. So what my next step is run the following command which will just look for differences and upload just the differences (hopefully)





# su



# rsync -r -a -v -e "ssh -l backupdude" /media/crates-15/ 192.168.1.77:/home/backups/crates







NOTE: I removed the --delete option which deletes the files if it doesn't exist on the source



Link to comment
Share on other sites

You may also want to try the following command... its much faster for copying so its great for that initial copy and then use your rsync command afterwards.



tar -c /media/crates-17 | ssh 192.168.1.77 'tar -xvf - -C /home/backups/crates'










Try this for rsync



rsync -avW -e ssh /media/crates-17 192.168.1.77:/home/backups/crates


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...