RSync quick reference
I always forget what rsync commands I actually need to use.
Backups (one-way)
Quick Backup from one folder to another
rsync -a directory1/ directory2/
Notes: remember the forward slashes on each directory. This does a simple one-way archive
Quick Backup from one folder to another ensuring DELETIONS
This ensures that the dest directory has extra files DELETED
rsync -a --delete directory1/ directory2/
Notes: remember the forward slashes on each directory. This does a simple one-way archive
Dry Run
rsync -a -n -v directory1/ directory2/
- -n = –dry-run
- -v = verbose (you need both)
Synch (two-way)
h3. Sync between two folders
rsync -a -u directory1/ directory2/ rysnc -a -u directory2/ directory1/
This one moves all the files from the first to the second and then does the reverse. I can see the problem with this is that if there are files that you deleted in the first (that are there in the second) that are really no longer needed then you will end up with them again. At that point you probably want to think about another system. Eg git or bazaar!
Archive folders to a server
Use the ruby script to do the job: http://github.com/RichGuk/rrsync/tree/master. Although I have now rewritten this script see the new version here: http://github.com/toddb/rrsync/tree/master
#!/usr/bin/ruby
require 'rubygems'
require 'Logger'
require 'benchmark'
require 'ping'
require 'FileUtils'
require 'open3'
#============================= OPTIONS ==============================#
# == Options for local machine.
SSH_APP = 'ssh'
RSYNC_APP = 'rsync'
EXCLUDE_FILE = '/path/to/.rsyncignore'
DIR_TO_BACKUP = '/test'
LOG_FILE = '/var/log/rrsync.log'
LOG_AGE = 'daily'
EMPTY_DIR = '/tmp/empty_rsync_dir/' #NEEDS TRAILING SLASH.
# == Options for the remote machine.
SSH_USER = 'user'
SSH_SERVER = 'x.dreamhost.com'
SSH_PORT = '' #Leave blank for default (port 22).
BACKUP_ROOT = '/home/.machine/user/backup'
BACKUP_DIR = BACKUP_ROOT + '/' + Time.now.strftime('%A').downcase
RSYNC_VERBOSE = '-v'
RSYNC_OPTS = "--force --ignore-errors --delete-excluded --exclude-from=#{EXCLUDE_FILE} --delete --backup --backup-dir=#{BACKUP_DIR} -a"
# == Options to control output
DEBUG = true #If true output to screen else output is sent to log file.
SILENT = false #Total silent = no log or screen output.
#========================== END OF OPTIONS ==========================#
if DEBUG && !SILENT
logger = Logger.new(STDOUT, LOG_AGE)
elsif LOG_FILE != '' && !SILENT
logger = Logger.new(LOG_FILE, LOG_AGE)
else
logger = Logger.new(nil)
end
ssh_port = SSH_PORT.empty? ? '' : "-e 'ssh -p #{SSH_PORT}'"
rsync_cleanout_cmd = "#{RSYNC_APP} #{RSYNC_VERBOSE} #{ssh_port} --delete -a #{EMPTY_DIR} #{SSH_USER}@#{SSH_SERVER}:#{BACKUP_DIR}"
rsync_cmd = "#{RSYNC_APP} #{RSYNC_VERBOSE} #{ssh_port} #{RSYNC_OPTS} #{DIR_TO_BACKUP} #{SSH_USER}@#{SSH_SERVER}:#{BACKUP_ROOT}/current"
logger.info("Started running at: #{Time.now}")
run_time = Benchmark.realtime do
begin
raise Exception, "Unable to find remote host (#{SSH_SERVER})" unless Ping.pingecho(SSH_SERVER)
FileUtils.mkdir_p("#{EMPTY_DIR}")
Open3::popen3("#{rsync_cleanout_cmd}") { |stdin, stdout, stderr|
tmp_stdout = stdout.read.strip
tmp_stderr = stderr.read.strip
logger.info("#{rsync_cleanout_cmd}\n#{tmp_stdout}") unless tmp_stdout.empty?
logger.error("#{rsync_cleanout_cmd}\n#{tmp_stderr}") unless tmp_stderr.empty?
}
Open3::popen3("#{rsync_cmd}") { |stdin, stdout, stderr|
tmp_stdout = stdout.read.strip
tmp_stderr = stderr.read.strip
logger.info("#{rsync_cmd}\n#{tmp_stdout}") unless tmp_stdout.empty?
logger.error("#{rsync_cmd}\n#{tmp_stderr}") unless tmp_stderr.empty?
}
FileUtils.rmdir("#{EMPTY_DIR}")
rescue Errno::EACCES, Errno::ENOENT, Errno::ENOTEMPTY, Exception => e
logger.fatal(e.to_s)
end
end
logger.info("Finished running at: #{Time.now} - Execution time: #{run_time.to_s[0, 5]}")