mrchucho (owner)

Revisions

gist: 152181 Download_button fork
public
Public Clone URL: git://gist.github.com/152181.git
Embed All Files: show embed
s3backup.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'digest/md5'
require 'aws/s3'
 
include AWS::S3
 
class S3Backup < S3Object
  # set_current_bucket_to `hostname`.strip
 
  def initialize
    current_bucket = `hostname`.strip
    unless Service.buckets.include?(current_bucket)
      Bucket.create(current_bucket)
    end
    S3Backup.set_current_bucket_to current_bucket
    super
  end
 
  def backup(filename)
    @filename = filename
    if should_upload_file?
      begin
        STDOUT.puts "Uploading #{@filename} to #{S3Backup.current_bucket}:#{self.s3_key}"
        S3Backup.store(self.s3_key,File.open(@filename,'r'))
      rescue => e
        STDERR.puts "Error storing file: #{e}"
      end
    else
      STDOUT.puts "#{@filename} is up-to-date."
    end
  end
 
protected
  def get_s3_file
    begin
      S3Backup.find(self.s3_key)
    rescue NoSuchKey
      nil
    end
  end
 
  def should_upload_file?
    if last_s3_backup = get_s3_file and last_s3_backup.etag == self.etag
      false
    else
      true
    end
  end
 
  def s3_key
    "backup/#{Time.now.strftime('%Y-%m-%d@%H:%M:%S')}/#{File.basename(@filename)}"
  end
 
  def etag
    Digest::MD5.hexdigest(File.read(@filename))
  end
end
 
class RepositoryBackup
  REPOSITORIES = %w() # your subversion repo names here, space-separated
  BACKUP_DIR = "."
 
  def RepositoryBackup.backup(repo)
    begin
      backup_dest = File.join(RepositoryBackup::BACKUP_DIR,"#{File.basename(repo)}.dmp.gz")
      `svnadmin dump -q #{repo} | gzip > #{backup_dest}`
 
      S3Backup.new.backup(backup_dest)
    rescue => e
      STDERR.puts "Error backing up repository #{repo}: #{e}"
    end
  end
end
 
class MySQLBackup
  DATABASES = %w() # your mysql DBs names here, space-separated
  BACKUP_DIR = "."
 
  def MySQLBackup.backup(db)
    begin
      backup_dest = File.join(MySQLBackup::BACKUP_DIR,"#{db}.sql.gz")
      `sudo mysqldump --defaults-file=/root/.my.cnf -u root #{db} | gzip -qf > #{backup_dest}`
 
      S3Backup.new.backup(backup_dest)
    rescue => e
      STDERR.puts "Error backing up database #{db}: #{e}"
    end
  end
end
 
class PostgresBackup
  DATABASES = %w() # your postgres DBs names here, space-separated
  BACKUP_DIR = "."
 
  def PostgresBackup.backup(db)
    begin
      backup_dest = File.join(PostgresBackup::BACKUP_DIR,"#{db}.sql.gz")
      `pg_dump --user=postgres #{db} | gzip -qf > #{backup_dest}`
 
      S3Backup.new.backup(backup_dest)
    rescue => e
      STDERR.puts "Error backing up database #{db}: #{e}"
    end
  end
end
 
 
if __FILE__ == $0
  begin
    Base.establish_connection!(:access_key_id => '<YOUR AWS KEY HERE>',
                               :secret_access_key => '<YOUR AWS SECRET HERE>'
                              )
 
    RepositoryBackup::REPOSITORIES.each do |repo|
      RepositoryBackup.backup(repo)
    end
    PostgresBackup::DATABASES.each do |db|
      PostgresBackup.backup(db)
    end
    MySQLBackup::DATABASES.each do |db|
      MySQLBackup.backup(db)
    end
  rescue => e
    STDERR.puts "Error: #{e}"
    STDERR.puts e.backtrace
  ensure
    Base.disconnect!
  end
end