Tuesday, March 23, 2010

Super simple rsync howto

This little howto contains pretty much everything you need to use rsync to back up and or synchronize two folders locally. I decided to do this writeup after after reading this article on lifehacker. I wasn't completely satisfied with the article so I did some digging to get the answers I needed. I know there's alot of rsync tutorials out there but I wrote this for my own personal use and also I tried to keep it as short as possible while explaining what's being done.

Oh, by the way rsync is really powerful and this only scratches the surface of what rsync can do. But you don't need more info to do local back ups.


Syntax:
rsync -options /source /destination 


Compare two folders
This command uses the "-v" and "-n" option to do nothing. Instead rsync prints out what would have been copied from a source folder to a destination folder. To delete files from the destination folder see notes on "--delete" below.

rsync -rvn /source /destination 

-r, --recursive recurse into directories
-v, --verbose increase verbosity
-n, --dry-run perform a trial run with no changes made


Back up one folder to another
This is all you need to back up a source directory to a destination directory. If unsure try it out with the "-n" dry run option first. To delete files from the destination folder see notes on "--delete" below.

rsync -av /source /destination 

-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
    -r, --recursive recurse into directories
    -l, --links copy symlinks as symlinks
    -p, --perms preserve permissions
    -t, --times preserve modification times
    -g, --group preserve group
    -o, --owner preserve owner (super-user only)
    -D same as --devices --specials
        --devices preserve device files (super-user only) 
        --specials preserve special files


Additional tips n tricks

Test with the "dry run" -n option first to see what files would be deleted and copied.
-n, --dry-run perform a trial run with no changes made

Delete files in the destination folder that does not exist in the source folder.
--delete delete extraneous files from dest dirs

Exclude files that you don't need for instance system files.
--exclude=PATTERN exclude files matching PATTERN
example: --exclude '.DS_Store'

Compress files during transfer to save bandwidth
-z, --compress compress file data during the transfer 



Credits
rsync - man page 
techgage.com - Backing Up Your Linux

Working with files and folders that has spaces in their names


I was in OSX's terminal yesterday trying to cd into a folder with a space in the name. That usually works really well with tab-completion but in the same place in the directory tree there were  another folder with the same name but without the space.

Here's a little illustration to clarify what I just wrote.  

root--+
      |
      +--Folder1 
      +--Folder1 With Space

So I want to cd into "Folder1 With Space" but I can't use tab-completion or type: 
cd Folder1 With Space 

However I can use \ as the escape character:
cd Folder1\ With\ Space

I can also use quotes, 'normal' or "double" (useful if the folder uses either one in it's name).
cd 'Folder1 With Space'
cd "Folder1 With Space"



Although I haven't tried it I'm pretty sure this works in any *nix terminal.