# Backup database from the application

**URL:** <https://www.ruby-forum.com/t/backup-database-from-the-application/194516>\
**Category:** Rails\
**Created:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/backup-database-from-the-application/194516 "2010-09-06T16:39:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/backup-database-from-the-application/194516/1 "2010-09-06T16:39:55Z")

</div>

Hi, I want to create an administration panel in the application that  
would allow to backup the database and some other stuff. How can I  
create a backup and send it to some place (s3, ftp) or download it as  
a file to the local computer.

Daniel

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [September 6, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/backup-database-from-the-application/194516/2 "2010-09-06T16:44:09Z")

</div>

On 6 September 2010 10:25, Daniel M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi, I want to create an administration panel in the application that  
> would allow to backup the database and some other stuff. How can I  
> create a backup and send it to some place (s3, ftp) or download it as  
> a file to the local computer.

I guess it will depend on your database. This is the code for a  
backup action in my app. I wrote it some time ago when I had little  
experience so it may well not be the best way.

def backup  
# get database name from one of the models  
# there must be a better way of doing this  
dbname = Act.configurations[RAILS\_ENV][‘database’]  
if dbname  
# delete backup files created on previous backup actions  
Dir.glob(“#{dbname}_\*.sql").each { |f| File.delete(f) }  
filename = "#{dbname}_” + Time.new.strftime(‘%Y%m%d\_%H%M%S’) +  
‘.sql’  
@answer = system “mysqldump -u xxxxx -pxxxxx #{dbname} \>  
#{filename}”  
if @answer  
send\_file “#{filename}”  
# note cannot delete the file here as the file is sent in  
-background  
else  
# dump failed  
flash[:error] = “Mysql dump failed”  
redirect\_to :action =\> :index  
end  
end  
end

Colin

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [September 7, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/backup-database-from-the-application/194516/3 "2010-09-07T16:40:02Z")

</div>

Daniel M. wrote:

> The project is on heroku and PostgreSQL is used there. I also use  
> PostgreSQL on my local. I thing creating/editing/deleting files on  
> heroku is forbidden

It is. But Heroku already provides Rake tasks to back up the DB.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [September 6, 2010, 8:01pm UTC](https://www.ruby-forum.com/t/backup-database-from-the-application/194516/4 "2010-09-06T20:01:32Z")

</div>

The project is on heroku and PostgreSQL is used there. I also use  
PostgreSQL on my local. I thing creating/editing/deleting files on  
heroku is forbidden
