Difference between revisions of "Rsync"

From ArchWiki
Jump to: navigation, search
m
(Substantial cleanup; more complex examples should be left to Google/man pages)
Line 1: Line 1:
{{stub}}
 
 
[[Category:Utilities (English)]]
 
[[Category:Utilities (English)]]
 
[[Category:HOWTOs (English)]]
 
[[Category:HOWTOs (English)]]
= Introduction =
 
The rsync protocol can be used to make quick and easy backups, as it only transfers files that have changed, which means backups are much quicker, than if you just copied all your files over every time.
 
This tutorial aims to show you how to make a scheduled backup, using rsync, typically to a removable media.
 
  
= Prerequisites =
+
''[http://samba.anu.edu.au/rsync/ rsync] is an open source utility that provides fast incremental file transfer.''
Obviously, to use rsync, you need rsync:
+
 
 +
==Installation==
 +
 
 
  # pacman -S rsync
 
  # pacman -S rsync
  
= Configuration =
+
==Usage==
We need to make a script that tells rsync to backup your data.
+
 
 +
===As a cp alternative===
 +
 
 +
rsync can readily be used as an advanced cp alternative, specifically useful for copying larger files:
 +
 
 +
$ rsync -P src dest
 +
 
 +
The {{Codeline|-P}} option is the same as {{Codeline|--partial -- progress}}, which keeps partially transferred files and shows a progress bar during transfer.
  
Open a text editor, for example:
+
===As a backup utility===
# nano /etc/cron.daily/rsync.backup
+
In this location, it will be run daily, without any further configuration.
+
  
You need to paste this script into the text editor:
+
The rsync protocol can easily be used for backups, only transferring files that have changed since the last backup. This tutorial describes how to configure scheduled backups using rsync, typically to some removable media.
  
#!/bin/bash
+
First, create a script containing the appropriate command options:
rsync -ar --delete /folder_to_backup/ /location_to_backup/ &> /dev/null
+
  
-a means files are archived, -r means files are copied recursively, and --delete means files deleted on the source are deleted on the backup
+
{{Note|For the sake of this example, the script is created in the {{Filename|/etc/cron.daily}} directory, and will be run once daily if a cron daemon is installed and properly configured. Configuring/using cron is outside the scope of this article.}}
  
Here, "folder_to_backup" needs to be changed to what you want to backup (/home/ , for example) and "location_to_backup" is where you want to back it up to (/media/disk , for example).
+
{{File|name=/etc/cron.daily/backup|content=
 +
#!/bin/bash
 +
rsync -a -r --delete /folder/to/backup /location/to/backup &> /dev/null}}
  
If you want to backup to a remote host using [[SSH]], use this script instead:
+
; {{Codeline|-a}} : means files are archived
 +
; {{Codeline|-r}} : means files are copied recursively
 +
; {{Codeline|--delete}} : means files deleted on the source are deleted on the backup
  
#!/bin/bash
+
Here, {{Filename|/folder/to/backup}} should to be changed to what needs to be backed-up ({{Filename|/home}}, for example) and {{Filename|/location/to/backup}} is where the backup should be saved ({{Filename|/media/disk}}, for example).
rsync -ar --delete -e ssh /folder_to_backup/ remoteuser@remotehost:/location_to_backup/ &> /dev/null
+
  
-e ssh tells rsync to use ssh, remoteuser is the user on the host remotehost
+
If backing-up to a remote host using [[SSH]], use this script instead:
  
We now need to make it executable:
+
{{File|name=/etc/cron.daily/backup|content=
#chmod +x /etc/cron.daily/rsync.backup
+
#!/bin/bash
 +
rsync -a -r --delete -e ssh /folder/to/backup remoteuser@remotehost:/location/to/backup &> /dev/null}}
  
And that's it. It'll now run daily, as long as your cron daemon is configured correctly (which is out of the scope of this document).
+
; {{Codeline|-e ssh}} : tells rsync to use SSH
 +
; {{Codeline|remoteuser}} : is the user on the host {{Codeline|remotehost}}
  
To check if rsync actually has preformed a backup you can use a log, here's a simple example:
+
Finally, the script must be executable:
#echo  beginning backup `date` >> ~/RsyncBackupLog
+
Put this line in your backup script and it will write "beginning backup 02-01-2009" in the text file RsyncBackupLog now you can see when the last backup is made.
+
  
You can also log the output of rsync:
+
  # chmod +x /etc/cron.daily/rsync.backup
  #rsync -ar --delete /folder_to_backup/ /location_to_backup/ >> ~/Rsync.log
+
The log can be find in your home directory and it's called Rsync.log
+
  
When you are making a backup to a server you can build in a check if the server is online:
+
For more examples, search Google for 'rsync backup script' or search the [http://bbs.archlinux.org/viewforum.php?id=27 Community Contributions] and [http://bbs.archlinux.org/viewforum.php?id=33 General Programming Forum] forums.
#if ping -c 1 -w 5 10.0.0.102 > /dev/null ;
+
then
+
mount the share
+
rsync -ar --delete -e ssh /folder_to_backup/ remoteuser@remotehost:/location_to_backup/ &> /dev/null
+
else
+
echo echo  backup failed `date` >> ~/RsyncBackupLog
+
fi
+
+
For more examples of really great bash scripting you should just google for 'rsync backup script' or go to the 'General Programming Forum' of the Arch forum
+

Revision as of 17:08, 8 November 2009


rsync is an open source utility that provides fast incremental file transfer.

Installation

# pacman -S rsync

Usage

As a cp alternative

rsync can readily be used as an advanced cp alternative, specifically useful for copying larger files:

$ rsync -P src dest

The Template:Codeline option is the same as Template:Codeline, which keeps partially transferred files and shows a progress bar during transfer.

As a backup utility

The rsync protocol can easily be used for backups, only transferring files that have changed since the last backup. This tutorial describes how to configure scheduled backups using rsync, typically to some removable media.

First, create a script containing the appropriate command options:

Note: For the sake of this example, the script is created in the Template:Filename directory, and will be run once daily if a cron daemon is installed and properly configured. Configuring/using cron is outside the scope of this article.

Template:File

Template:Codeline 
means files are archived
Template:Codeline 
means files are copied recursively
Template:Codeline 
means files deleted on the source are deleted on the backup

Here, Template:Filename should to be changed to what needs to be backed-up (Template:Filename, for example) and Template:Filename is where the backup should be saved (Template:Filename, for example).

If backing-up to a remote host using SSH, use this script instead:

Template:File

Template:Codeline 
tells rsync to use SSH
Template:Codeline 
is the user on the host Template:Codeline

Finally, the script must be executable:

# chmod +x /etc/cron.daily/rsync.backup

For more examples, search Google for 'rsync backup script' or search the Community Contributions and General Programming Forum forums.