# rsync without synching # use -n rsync -avn /usbdisk/ /localdisk/ # sync 2 folders rsync -av /usbdisk/ /localdisk/ # #Lets say we wanted to copy all of the files under our local directory, # "/usbdisk/" and place a copy on the machine "somemachine" under the remote directory "/backups/". # The name of the user on the remote machine is "calomel". # Notice we added the "-z" argument so all data will be # compressed over the network. We could use the following line: rsync -avz /usbdisk/ calomel@somemachine:/backups/ # What if you wanted to delete any files on the _remote_ machine # that are no longer on the source machine? We can use the same command above # and simply add the "--del" argument. This will tell rsync to delete # any files from "/backups/" that are no longer on the source directory, "/usbdisk/". rsync -avz --del /usbdisk/ calomel@somemachine:/backups/ # What if you wanted to not just copy all of the files under /usbdisk/, # but also the /usbdisk/ directory name. Just leave off the last "/" after the "/usbdisk" directory # name. This will tell rsync to copy the directory name and all files under that # directory. On the remote server you will then see the directory structure /backups/usbdisk/. rsync -avz /usbdisk calomel@somemachine:/backups/ # You can also rsync multiple files or directories using a single rsync line. This is # useful when coping over the network as you only need to enter your ssh password # once. Here we will copy the source directories /data1 and /data2 to the remote machine # "somemachine" in the /backups/ directory. rsync -avz /data1 /data2 calomel@somemachine:/backups/ # Here we are pulling multiple files from a remote server to our local machine. We # have to use curly braces to tell rsync we want only the files "file1", "file2" # and "file3." Using this method you only have to enter your ssh password once to # collect all the files. rsync -avz calomel@somemachine:/remote_machine/{file1,file2,file3} /local/disk/ # You can also pull the data from a remote machine to your local box. To # rsync all of the files from the same "somemachine" to our local box just reverse # the target and destination of the previous example: rsync -avz calomel@somemachine:/backups/ /usbdisk/ # RSYNC WHEN REMOTE FILES OR DIRECTORIES CONTAIN SPACES #----------------------------------------------------------- # Spaces cause all sorts of problems. To rsync when you have spaces in the files # and/or directories you need to use a single tick to encompass the entire string # and then escape the spaces. The following line shows the remote directory name "/I Hate Spaces" # and the file name "some File.avi", both contain spaces. We will be rsync'ing # the file to our local directory "/current_dir/". rsync -av calomel@somemachine:'/I\ Hate\ Spaces/some\ File.avi' /current_dir/ # EXECUTE REMOTE SHELL COMMAND TO RSYNC FILES # ------------------------------------------------------------- # It is important to note rsync can also execute commands on the remote machine to # help you generate a list of files copy. The shell command is expanded by your # remote shell before rsync is called. The following line will run the find command # on the remote machine in the video directory and rsync all "avi" files it finds # to our machine in the /download directory rsync -avR calomel@somemachine:'`find /data/video -name "*.[avi]"`' /download/ # PULL DATA FROM A REMOTE MACHINE TO LOCAL SERVER USING SSH # ------------------------------------------------------------- # The following command will pull the data from "remote_machine" in /stuff/data/ and place # it on the local system in /BACKUP/remote_machine/. The arguments "-avx" will set archive # mode (-a) equivalent to -rlptgoD, be verbose (-v) and will not cross file system boundaries # (-x) like NFS or samba. The timeout command makes sure rsync will not hang if # the remote system is unreachable after 30 seconds. We will be deleting any files on # the target directory (/BACKUP/remote_machine/) that are _not_ found in the source directory (data/). # If you do not want to allow rsync to delete any files then take out "--delete-excluded". # # The directory structure of the target machine will look like /BACKUP/remote_machine/data/ and # this is considered a non-relative path option. Notice /stuff/ is not in the path. rsync -avx --timeout=30 --delete-excluded backupuser@remote_machine:/stuff/data/ /BACKUP/remote_machine/ # If you wanted the target directory structure to be relative you can add the argument # "-R". The directory structure would then look like /BACKUP/remote_machine/stuff/data/ as the # sync path name starts / on the source machine. The command with "-R" added looks like: rsync -Ravx --timeout=30 --delete-excluded backupuser@remote_machine:/stuff/data/ /BACKUP/remote_machine/ # INCREMENTAL BACKUPS USING RSYNC # ------------------------------------------------------------- # The following example will make an incremental backup of the directory /data/working/ and put # a copy of any file that changes into a dated directory in /BACKUP/ . This can # be used to keep a daily backup tree of any changed files and not have # to overwrite the previous days files. Note that this method does need to copy the # entire file if it changes as the new files are made in the directory named under current day. rsync --backup --backup-dir=`date +%Y.%m.%d` -a /data/working/ /BACKUP/ # If you have a file under /BACKUP/ called file1 and that file has changed on # the source machine in /data/working/ then a new directory will be made. The incremental # dir would be named `date +%Y.%m.%d` or the numerical values for YEAR.MONTH.DAY # and put under /BACKUP/. The changed data "file1" would be put under that directory.