Skip to main content

SQLAlchemy backup/dump tool for Flask

Project description

AlchemyDumps

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

This package let you backup and restore all your data using SQLALchemy dumps() method.

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

Examples

Considering you have these models (SQLAlchemy mapped classes):

class User(Model):
    id = Column(db.Integer, primary_key=True)
    email = Column(db.String(140), index=True, unique=True)
    ...

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

You can backup all your data:

$ python manage.py alchemydumps create

Output:

==> 3 rows from User post 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 manage.py alchemydumps history

Output:

==> ID: 20141114203949 (from Nov 15, 2014 at 17:21:07)
    /vagrant/alchemydumps/db-bkp-20141115172107-User.gz
    /vagrant/alchemydumps/db-bkp-20141115172107-Post.gz

==> ID: 20141115140629 (from Nov 15, 2014 at 14:06:29)
    /vagrant/alchemydumps/db-bkp-20141115140629-User.gz
    /vagrant/alchemydumps/db-bkp-20141115140629-Post.gz

And, surely, you can restore backuped data:

$ python manage.py alchemydumps restore -d 20141115172107

Output:

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

If you want, you can delete an existing backup:

$ python manage.py alchemydumps remove -d 20141115172107

Output:

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

Install

First install the package: $ pip install Flask-AlchemyDumps

Then configure it in your Flask application:

from alchemydumps import AlchemyDumps, AlchemyDumpsCommand
from flask import Flask
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)

The first line import the methods from the package, the last two lines instantiate and add AlchemyDumps to the Flask-Script manager).

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

Requirements

As AlchemyDumps was designed to work together with Flask applications that uses SQLAlchemy. And it runs within the Flask-Script manager. Thus, be sure to have these packages installed and in use. AlchemyDumps also uses Unipath package.

In sum, if your requirements.txt looks something like this, probably you will be fine:

Flask>=0.10.1
Flask-Script>=2.0.5
Flask-SQLAlchemy>=0.16
SQLAlchemy>=0.7.9
Unipath>=1.0

AlchemyDumps is not ready for Python 3 yet (but pull requests are more than welcomed).

Changelog

Version 0.0.2
  • Implement command to delete backups.

  • Proper message when ID is not found in restore and delete commands.

  • Avoid breaking the code when get_id() fails.

  • Minor code improvements.

License

Copyright (c) 2014 Eduardo Cuducos.

Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-AlchemyDumps-0.0.2.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file Flask-AlchemyDumps-0.0.2.tar.gz.

File metadata

File hashes

Hashes for Flask-AlchemyDumps-0.0.2.tar.gz
Algorithm Hash digest
SHA256 34bd0c8d408d1855063d2c56c9791ed8bceff8d48dc25f4cfb0ead8de5f8be09
MD5 f1857fb75d7179c72d57c8b6d9d8d7f7
BLAKE2b-256 66c682e025cfd11166abb1ab33816fc5f2e88834ed1548451d95c5078bf5cf5e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page