Skip to content
SQLAlchemy backup/dump tool for Flask
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
flask_alchemydumps Fix bug in history command Jun 29, 2016
tests Use SQLite in memory database for tests Jul 3, 2016
.gitignore Re-add Python 3.3 Jul 5, 2016
.travis.yml Re-add Python 3.3 Jul 5, 2016 Put to adoption Nov 3, 2016 Re-add Python 3.3 Jul 5, 2016
tox.ini Re-add Python 3.3 Jul 5, 2016

AlchemyDumps Latest release

Development Status: Alpha Python Version Travis CI Coverage Status

Do you use Flask with SQLAlchemy and Flask-Script ? Wow, what a coincidence!

This package lets you backup and restore all your data using SQLAlchemy dumps() method.

It is an easy way (one single command, I mean it) to save all the data stored in your database.

You can save it locally or in a remote server via FTP.

WARNING @baumatron've found an important bug: at this time this package won't backup non-standard mappings, such as many-to-many association tables. This is still an open issue and I have no expectation to fix is in the following weeks — pull requests are more tham welcomed.

I can't maintain this package anymore and I'd be happy to donate it to anyone willing to take it further Just drop a line ; )


First install the package: pip install Flask-AlchemyDumps

Then pass your Flask application and SQLALchemy database to it.

For example:

  • The second line imports the objects from the package.
  • The last two lines instantiate and add AlchemyDumps to the Flask-Script manager.
from flask import Flask
from flask.ext.alchemydumps import AlchemyDumps, AlchemyDumpsCommand
from flask.ext.script import Manager
from flask.ext.sqlalchemy import SQLAlchemy

# init Flask
app = Flask(__name__)

# init SQLAlchemy and Flask-Script
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
db = SQLAlchemy(app)
manager = Manager(app)

# init Alchemy Dumps
alchemydumps = AlchemyDumps(app, db)
manager.add_command('alchemydumps', AlchemyDumpsCommand)

Remote backup (via FTP)

If you want to save your backup in a remote server via FTP, just make sure to set these environment variables replacing the placeholder information with the proper credentials:

export ALCHEMYDUMPS_FTP_PATH=/absolute/path/

If you want, there is a .env.sample inside the /tests folder. Just copy it to your application root folder, rename it to .env, and insert your credentials.

Using application factory

It is possible to use this package with application factories:

alchemydumps = AlchemyDumps()

def create_app(settings):
    alchemydumps.init_app(app, db)


You might want to add alchemydumps/ to your .gitignore. It is the folder where AlchemyDumps save the backup files.


Considering you have these models — that is to say, these SQLAlchemy mapped classes:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(140), index=True, unique=True)
    posts = db.relationship('Post', backref='author', lazy='dynamic')

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(140))
    content = db.Column(db.UnicodeText)
    author_id = db.Column(db.Integer, db.ForeignKey(''))

Just in case: this is a simple example, but you can use abstract mapped classes as well.

You can backup all your data

python alchemydumps create


==> 3 rows from User saved as /vagrant/alchemydumps/db-bkp-20141115172107-User.gz
==> 42 rows from Post saved as /vagrant/alchemydumps/db-bkp-20141115172107-Post.gz

You can list the backups you have already created

python alchemydumps history


==> ID: 20141114203949 (from Nov 15, 2014 at 17:21:07)

==> ID: 20141115140629 (from Nov 15, 2014 at 14:06:29)

You can restore a backup

python alchemydumps restore -d 20141115172107


==> db-bkp-20141115172107-User.gz totally restored.
==> db-bkp-20141115172107-Post.gz totally restored.

You can delete an existing backup

python alchemydumps remove -d 20141115172107


==> Do you want to delete the following files?
==> Press "Y" to confirm, or anything else to abort: y
    db-bkp-20141115172107-User.gz deleted.
    db-bkp-20141115172107-Post.gz deleted.

And you can use the auto-clean command

The autoclean command follows these rules to delete backups:

  • It keeps all the backups from the last 7 days.
  • It keeps the most recent backup from each week of the last month.
  • It keeps the most recent backup from each month of the last year.
  • It keeps the most recent backup from each remaining year.
python alchemydumps autoclean


==> 8 backups will be kept:

    ID: 20130703225903 (from Jul 03, 2013 at 22:59:03)

    ID: 20120405013054 (from Apr 05, 2012 at 01:30:54)

    ID: 20101123054342 (from Nov 23, 2010 at 05:43:42)

    ID: 20090708100815 (from Jul 08, 2009 at 10:08:15)

    ID: 20081208191908 (from Dec 08, 2008 at 19:19:08)

    ID: 20070114122922 (from Jan 14, 2007 at 12:29:22)

    ID: 20060911035318 (from Sep 11, 2006 at 03:53:18)

    ID: 20051108082503 (from Nov 08, 2005 at 08:25:03)

==> 11 backups will be deleted:

    ID: 20120123032442 (from Jan 23, 2012 at 03:24:42)

    ID: 20101029100412 (from Oct 29, 2010 at 10:04:12)

    ID: 20100526185156 (from May 26, 2010 at 18:51:56)

    ID: 20100423085529 (from Apr 23, 2010 at 08:55:29)

    ID: 20081006074458 (from Oct 06, 2008 at 07:44:58)

    ID: 20080429210254 (from Apr 29, 2008 at 21:02:54)

    ID: 20080424043716 (from Apr 24, 2008 at 04:37:16)

    ID: 20080405110244 (from Apr 05, 2008 at 11:02:44)

    ID: 20060629054914 (from Jun 29, 2006 at 05:49:14)

    ID: 20060329020048 (from Mar 29, 2006 at 02:00:48)

    ID: 20050324012859 (from Mar 24, 2005 at 01:28:59)

==> Press "Y" to confirm, or anything else to abort. y
    db-bkp-20120123032442-User.gz deleted.
    db-bkp-20120123032442-Post.gz deleted.
    db-bkp-20101029100412-User.gz deleted.
    db-bkp-20101029100412-Post.gz deleted.
    db-bkp-20100526185156-User.gz deleted.
    db-bkp-20100526185156-Post.gz deleted.
    db-bkp-20100423085529-User.gz deleted.
    db-bkp-20100423085529-Post.gz deleted.
    db-bkp-20081006074458-User.gz deleted.
    db-bkp-20081006074458-Post.gz deleted.
    db-bkp-20080429210254-User.gz deleted.
    db-bkp-20080429210254-Post.gz deleted.
    db-bkp-20080424043716-User.gz deleted.
    db-bkp-20080424043716-Post.gz deleted.
    db-bkp-20080405110244-User.gz deleted.
    db-bkp-20080405110244-Post.gz deleted.
    db-bkp-20060629054914-User.gz deleted.
    db-bkp-20060629054914-Post.gz deleted.
    db-bkp-20060329020048-User.gz deleted.
    db-bkp-20060329020048-Post.gz deleted.
    db-bkp-20050324012859-User.gz deleted.
    db-bkp-20050324012859-Post.gz deleted.

Requirements & Dependencies

AlchemyDumps is tested and should work with Python 3 and 2.7+.

AlchemyDumps was designed to work together with Flask applications that uses SQLAlchemy. It runs through the Flask-Script manager. Thus, these packages are essential requirements. AlchemyDumps also uses Unipath package. All these packages, if needed, will be installed once you install AlchemyDumps.


If you wanna run the tests:

pip install tox


You can report issues or:

  • Fork this repo
  • Create a new branch: git checkout -b my-new-feature
  • Commit your changes: git add -A . && commit -m 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • And create new pull request


Thanks for the pull requests, reported issues, feedback and support:


  • Version 0.0.10
    • Python 3.5 support.
    • Fix bug related to 250 status in FTP backups (with vsftpd).
  • Version 0.0.9
    • Fix bug related to directory paths in Windows.
  • Version 0.0.8
    • Python 3 support.
  • Version 0.0.7
    • Add support for abstract models.
  • Version 0.0.6
    • Remote backup/restore via FTP.
    • General code improvements.
  • Version 0.0.5
    • Re-written as a Flask extension.
    • Built-in Flask app within the test suite.
  • Version 0.0.4
    • Fix bug in the installation process.
  • Version 0.0.3
    • New command to auto-clean backup folder.
  • Version 0.0.2
    • New command: delete a single backup.
    • Proper message when ID is not found in restore and delete commands.
    • Avoid breaking the process when get_id() fails.
    • Minor code improvements.


Copyright (c) 2015 Eduardo Cuducos.

Licensed under the MIT License.

You can’t perform that action at this time.