namespace :dbdo desc "Fetch the latest backup from heroku" task :fetch => [:environment] do app = "<YOU PRODUCTION APP HERE>" url = `heroku pgbackups:url -a #{app}` db_prefix = app.gsub('-','_') fname = "#{db_prefix}_#{Time.now.strftime("%Y%m%d")}.postgres" puts "curl -o #{fname} '#{url}'" `curl -o #{fname} "#{url}"` end
desc "Import a dbfile into the development database on the local system" task :import => [:environment] do dbfile = ENV['dbfile'] if dbfile.blank? || !File.exists?(dbfile) puts "You need to specify the database file to import with dbfile=<filename>" else `pg_restore --verbose --clean --no-acl --no-owner -h localhost -d <YOUR LOCAL DB HERE> #{dbfile}` end end
desc "Sanitize user data" task :sanitize_user_data => [:environment] do User.all.each do|u| u.email = "#{u.email}.fake" u.password = 'password' u.password_confirmation = 'password' u.save end end end
However, whenever I ran it I kept getting errors like
I could NOT figure out what was happening. I stumbled upon an issue on the Heroku github page in which it is suggested to wrap the calls in Bundler.with_clean_env block
The change was to make the fetch task look like this:
1 2 3 4 5 6 7 8 9 10 11
desc "Fetch the latest backup from heroku" task :fetch => [:environment] do Bundler.with_clean_env do app = "<YOU PRODUCTION APP HERE>" url = `heroku pgbackups:url -a #{app}` db_prefix = app.gsub('-','_') fname = "#{db_prefix}_#{Time.now.strftime("%Y%m%d")}.postgres" puts "curl -o #{fname} '#{url}'" `curl -o #{fname} "#{url}"` end end
All is well now and I can backup my files successfully from Heroku.