<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rsync code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 00:27:49 GMT</pubDate>
    <description>DZone Snippets: rsync code</description>
    <item>
      <title>Use rsync to mirror directories between Virtual Hosts.</title>
      <link>http://snippets.dzone.com/posts/show/5561</link>
      <description>The following shell command will rsync the mysite.com directory 'feed' to mysite2.com&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rsync -a /var/www/mysite.com/feed /var/www/mysite2.com&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 29 May 2008 23:16:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5561</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Active Record YAML Backup Solution - Drop in rake task and config file to backup db and directorys of your choice (user uploaded files) 

</title>
      <link>http://snippets.dzone.com/posts/show/4777</link>
      <description>EZ drop in backup rake task for your rails projects - works well - 5 - 10 min set up, one rake file and one config file&lt;br /&gt;backs up db and any directory's to any number of servers with rsync&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;BSD License or whatever, but it would be cool if you told me your using it&lt;br /&gt;2007 ISS http://industrialstrengthinc.com&lt;br /&gt;&lt;br /&gt;this is a sample config file and a rake task you can drop into any rails project to do backups, easy to automate.&lt;br /&gt;Backs up your specified environment db to activerecord yml files (one per table) and zips them up in a human readable timestamped file&lt;br /&gt;syncs that and any other set of arbitrary directory's to any number of arbitrary servers with rsync&lt;br /&gt;be sure to set ssh key based logins&lt;br /&gt;to run: rake backup&lt;br /&gt;also: rake backup:db, rake backup:restoredb (promts for a file created by backup:db), rake backup:push (to push out what u got)&lt;br /&gt;note: this uses something like 30 lines of code from some blog site I got it from that I cant recall, yay for that guy, thanks&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;## example crontab entry:&lt;br /&gt;&lt;br /&gt;3 * * * * cd /rails_deployment_dir/current &amp;&amp; nice rake backup RAILS_ENV=production &gt;&gt; /rails_deployment_dir/production_backup_system.log&lt;br /&gt;&lt;br /&gt;## config file - goes in config/backup.yml&lt;br /&gt;&lt;br /&gt;production: &lt;br /&gt;  dirs: &lt;br /&gt;   - db/backups&lt;br /&gt;   - public/uploaded_images&lt;br /&gt;  servers: &lt;br /&gt;    -  name: backup server number 1&lt;br /&gt;       host: gridserver.com&lt;br /&gt;       port: 22&lt;br /&gt;       user: blah@whatever.com&lt;br /&gt;       dir: /home/blah/backups&lt;br /&gt;    -  name: backup server two&lt;br /&gt;       host: kradradio.com&lt;br /&gt;       port: 22&lt;br /&gt;       user: kraduser&lt;br /&gt;       dir: /home/kraduser/backups_from_my_rails_proj&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;development: &lt;br /&gt;  dirs: &lt;br /&gt;   - db/backups&lt;br /&gt;   - public/uploaded_images&lt;br /&gt;  servers: &lt;br /&gt;    -  name: local self&lt;br /&gt;       host: localhost&lt;br /&gt;       port: 5222&lt;br /&gt;       user: oneman&lt;br /&gt;       dir: /home/oneman/Documents/development_backup&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;## rake file lib/tasks/backup.rake&lt;br /&gt;&lt;br /&gt;desc "Backup Everything Specified in config/backup.yml"&lt;br /&gt;task :backup =&gt; [ "backup:db",  "backup:push"]&lt;br /&gt;&lt;br /&gt;namespace :backup do&lt;br /&gt; &lt;br /&gt;    RAILS_APPDIR = RAILS_ROOT.sub("/config/..","")&lt;br /&gt;    &lt;br /&gt;   def interesting_tables&lt;br /&gt;     ActiveRecord::Base.connection.tables.sort.reject! do |tbl|&lt;br /&gt;       ['schema_info', 'sessions', 'public_exceptions'].include?(tbl)&lt;br /&gt;     end&lt;br /&gt;   end&lt;br /&gt;  &lt;br /&gt;   desc "Push backup to remote server"&lt;br /&gt;   task :push  =&gt; [:environment] do &lt;br /&gt;      FileUtils.chdir(RAILS_APPDIR)&lt;br /&gt;      backup_config = YAML::load( File.open( 'config/backup.yml' ) )[RAILS_ENV]&lt;br /&gt;      for server in backup_config["servers"]&lt;br /&gt;       puts "Backing up #{RAILS_ENV} directorys #{backup_config['dirs'].join(', ')} to #{server['name']}"&lt;br /&gt;       puts "Time is " + Time.now.rfc2822 + "\n\n"&lt;br /&gt;         for dir in backup_config["dirs"]&lt;br /&gt;          local_dir = RAILS_APPDIR + "/" + dir + "/"&lt;br /&gt;          remote_dir = server['dir'] + "/" + dir.split("/").last + "/"&lt;br /&gt;          puts "Syncing #{local_dir} to #{server['host']}#{remote_dir}"&lt;br /&gt;          sh "/usr/bin/rsync -avz -e 'ssh -p#{server['port']} ' #{local_dir} #{server['user']}@#{server['host']}:#{remote_dir}"&lt;br /&gt;         end&lt;br /&gt;       puts "Completed backup to #{server['name']}\n\n"&lt;br /&gt;      end&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;    task :storedb =&gt; :environment do &lt;br /&gt;&lt;br /&gt;      backupdir = RAILS_APPDIR + '/db/backup'&lt;br /&gt;      FileUtils.mkdir_p(backupdir)&lt;br /&gt;      FileUtils.chdir(backupdir)&lt;br /&gt;      puts "Dumping database to activerecord yaml files in #{backupdir}"&lt;br /&gt;      interesting_tables.each do |tbl|&lt;br /&gt;&lt;br /&gt;        klass = tbl.classify.constantize&lt;br /&gt;        puts "Writing #{tbl}..."&lt;br /&gt;        File.open("#{tbl}.yml", 'w+') { |f| YAML.dump klass.find(:all).collect(&amp;:attributes), f }      &lt;br /&gt;      end&lt;br /&gt;      puts "Database Dumped.\n\n"&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc "Dump Current Environment Db to file"    &lt;br /&gt;    task :db =&gt; [:environment, :storedb ] do&lt;br /&gt;      backupdir = RAILS_APPDIR + '/db/backup'&lt;br /&gt;      archivedir = RAILS_APPDIR + '/db/backups'&lt;br /&gt;      backup_filename = "#{RAILS_ENV}_db_backup_#{Time.now.strftime("%B.%d.%Y_at_%I.%M.%S%p_%Z")}.tar.bz2"&lt;br /&gt;      FileUtils.mkdir_p(archivedir)&lt;br /&gt;      puts "Archiving #{backupdir} yaml files to #{backup_filename}\n\n"&lt;br /&gt;      `tar -C #{backupdir} -cjf #{backup_filename} *`&lt;br /&gt;      `mv #{backup_filename} #{archivedir}`&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc "Restore Current Environment Db from a file"    &lt;br /&gt;    task :restoredb =&gt; [:environment] do &lt;br /&gt;        backupdir = RAILS_APPDIR + '/db/backup'&lt;br /&gt;        archivedir = RAILS_APPDIR + '/db/backups'&lt;br /&gt;        print "Input a file to load into the db: #{archivedir}/"&lt;br /&gt;        backup_filename = STDIN.gets.chomp&lt;br /&gt;        puts "Loading backup file: #{backup_filename}"&lt;br /&gt;        FileUtils.chdir(archivedir)&lt;br /&gt;        `tar -xjf #{backup_filename}`&lt;br /&gt;        `mv *.yml #{backupdir}`&lt;br /&gt;        FileUtils.mkdir_p(backupdir)&lt;br /&gt;        FileUtils.chdir(backupdir)&lt;br /&gt;    &lt;br /&gt;        interesting_tables.each do |tbl|&lt;br /&gt;        puts "Clearing #{tbl} table.."&lt;br /&gt;        ActiveRecord::Base.connection.execute "TRUNCATE #{tbl}"&lt;br /&gt;        puts "Loading #{tbl} backup file..."&lt;br /&gt;        table = YAML.load_file("#{tbl}.yml")        &lt;br /&gt;&lt;br /&gt;        if table.length &gt; 0 &amp;&amp; table.first.key?("id")&lt;br /&gt;            highestid = 0&lt;br /&gt;            table.each do |fixture|&lt;br /&gt;             if fixture["id"] &gt; highestid&lt;br /&gt;                highestid = fixture["id"]&lt;br /&gt;             end&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;            ActiveRecord::Base.connection.execute "SELECT setval('#{tbl}_id_seq',#{highestid})"&lt;br /&gt;            puts "Setting #{tbl}_id sequence to #{highestid}"&lt;br /&gt;        end&lt;br /&gt;         &lt;br /&gt;        #klass = tbl.classify.constantize&lt;br /&gt;        ActiveRecord::Base.transaction do &lt;br /&gt;        &lt;br /&gt;          puts "Inserting #{table.length} values into #{tbl}"&lt;br /&gt;          table.each do |fixture|&lt;br /&gt;            ActiveRecord::Base.connection.execute "INSERT INTO #{tbl} (#{fixture.keys.join(",")}) VALUES (#{fixture.values.collect { |value| ActiveRecord::Base.connection.quote(value) }.join(",")})", 'Fixture Insert'&lt;br /&gt;          end        &lt;br /&gt;          puts "#{tbl} table restored.\n\n"&lt;br /&gt;        end&lt;br /&gt;       end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 16 Nov 2007 04:38:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4777</guid>
      <author>rawdod (David Richards)</author>
    </item>
    <item>
      <title>copy files using rsync and ssh</title>
      <link>http://snippets.dzone.com/posts/show/4717</link>
      <description>source: http://www.mikerubel.org/computers/rsync_snapshots/#Abstract ; switches: -a = archive mode -e specifies the remote shell to use&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rsync -a -e ssh source/ username@remotemachine.com:/path/to/destination/&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 30 Oct 2007 13:03:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4717</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>rsync backup of a Mac</title>
      <link>http://snippets.dzone.com/posts/show/2822</link>
      <description>&lt;code&gt;sudo /usr/local/bin/rsync -aREx --delete --exclude='.Spotlight-*' --exclude '/private/var/vm/*' [IP-address of Mac mini]::PowerBookBackup&lt;/code&gt;</description>
      <pubDate>Sun, 15 Oct 2006 08:37:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2822</guid>
      <author>willcodeforfoo ()</author>
    </item>
    <item>
      <title>Cheapest rsync replacement (with Ruby)</title>
      <link>http://snippets.dzone.com/posts/show/1812</link>
      <description>I often use rsync to keep a local copy of some HTTPD logs (around ~200MB atm.). Since they are append-only, having rsync compute and compare the checksums for the parts I already have seems wasteful: both my box and the one I'm copying from would be happier if they didn't have to process a couple hundred MBs for nothing. (...)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;&lt;br /&gt;REMOTE_RUBY = "ruby"&lt;br /&gt;# TODO: allow REMOTE_RUBY to be specified via a cmdline opt&lt;br /&gt;&lt;br /&gt;if ARGV.size != 2 || ARGV[0][/:/].nil? || !File.exist?(ARGV[1])&lt;br /&gt;  puts &lt;&lt;EOF&lt;br /&gt;  ruby logfetcher.rb host:path/to/src dst&lt;br /&gt;EOF&lt;br /&gt;  exit&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;FILE = ARGV[1]&lt;br /&gt;REMOTE_HOST, REMOTE_FILE = ARGV[0].split(/:/)&lt;br /&gt;BLOCK_SIZE = 8192&lt;br /&gt;&lt;br /&gt;osize = File.size(FILE)&lt;br /&gt;#FIXME: cheap escaping&lt;br /&gt;command = "File.open(#{REMOTE_FILE.inspect}){|f| " + &lt;br /&gt;          "f.pos = #{osize}; print f.read(#{BLOCK_SIZE}) until f.eof? }"&lt;br /&gt;&lt;br /&gt;command.gsub!(/"/){'\\"'}&lt;br /&gt;fetched = 0&lt;br /&gt;t = nil&lt;br /&gt;$stdout.sync = true&lt;br /&gt;print "Establishing connection\r"&lt;br /&gt;File.open(FILE, "a") do |os|&lt;br /&gt;  IO.popen(%{ssh #{REMOTE_HOST} ruby -e '"#{command}"'}) do |is|&lt;br /&gt;    until is.eof?&lt;br /&gt;      data = is.read(BLOCK_SIZE)&lt;br /&gt;      t ||= Time.new # ignore the time it takes to establish the SSH connection&lt;br /&gt;      fetched += data.size&lt;br /&gt;      print "Read #{fetched}                          \r"&lt;br /&gt;      os.write(data)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;print(" " * 50  + "\r")&lt;br /&gt;&lt;br /&gt;dt = Time.new - t&lt;br /&gt;puts "Fetched #{fetched} bytes."&lt;br /&gt;puts "Total size #{osize + fetched}."&lt;br /&gt;puts "Needed %4.1f seconds." % dt&lt;br /&gt;puts "Average speed %d bytes/sec." % (fetched / dt)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Source: &lt;a href="http://eigenclass.org/hiki.rb?cheap+rsync"&gt;Cheapest rsync replacement&lt;/a&gt;</description>
      <pubDate>Fri, 31 Mar 2006 15:06:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1812</guid>
      <author>413x ()</author>
    </item>
    <item>
      <title>Resuming an aborted scp transfer</title>
      <link>http://snippets.dzone.com/posts/show/201</link>
      <description>Assume you have copied a file across the network using something like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;scp host:/path/to/file .&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then assume something goes wrong, so the transfer is interrupted. To continue the transfer, do this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;rsync --partial host:/path/to/file .&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;(Or, rather, always use rsync as above when transferring very large files.)&lt;br /&gt;&lt;br /&gt;(Also note: I don't know how frequently rsync flushes its buffer, but don't assume something is wrong just because the file you're downloading doesn't increase in size in the file system. When you interrupt rsync using Ctrl-C, it will flush its buffers.)</description>
      <pubDate>Thu, 21 Apr 2005 20:58:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/201</guid>
      <author>plindberg (Peter Lindberg)</author>
    </item>
  </channel>
</rss>
