Class: CloudBackup::Dumper
- Inherits:
-
Object
- Object
- CloudBackup::Dumper
- Defined in:
- lib/cloud_backup/dumper.rb
Instance Method Summary collapse
- #authorize ⇒ Object
- #dump_db ⇒ Object
-
#initialize ⇒ Dumper
constructor
A new instance of Dumper.
- #upload_file(file_name) ⇒ Object
Constructor Details
#initialize ⇒ Dumper
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cloud_backup/dumper.rb', line 7 def initialize settings_path = File.join(File.dirname(__FILE__), '../settings.yml') settings = YAML.load_file(settings_path) @app_key = settings['dropbox']['app_key'] @app_secret = settings['dropbox']['app_secret'] @db_name = settings['database']['db_name'] @db_password = settings['database']['db_password'] @db_user = settings['database']['db_user'] if File::exists?(".dump_db") data = File.read(".dump_db") @access_token = Marshal.load(data) end if @access_token puts 'Your access token is ' + @access_token else end end |
Instance Method Details
#authorize ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cloud_backup/dumper.rb', line 28 def flow = DropboxOAuth2FlowNoRedirect.new(@app_key, @app_secret) = flow.start() # Have the user sign in and authorize this app puts '1. Go to this link in your web browser: ' + puts '2. Click "Allow" (you might have to log in first)' puts '3. Copy the authorization code' print 'Enter the authorization code here: ' code = gets.strip # This will fail if the user gave us an invalid authorization code @access_token, user_id = flow.finish(code) data = Marshal.dump(@access_token) open('.dump_db', 'wb') { |f| f.puts data } end |
#dump_db ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/cloud_backup/dumper.rb', line 53 def dump_db date = Time.now.strftime('%d%m%y') file_name = "#{@db_name}_#{date}.sql" puts "Backing up the db to #{file_name}" `mysqldump -u #{@db_user} -p#{@db_password} #{@db_name} > #{file_name}` upload_file file_name end |
#upload_file(file_name) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/cloud_backup/dumper.rb', line 45 def upload_file(file_name) client = DropboxClient.new(@access_token) file = open(file_name) puts 'Uploading file!! Please wait.' response = client.put_file("/#{file_name}", file) puts "uploaded:", response.inspect end |