• This repository has been archived on 07/Sep/2023
  • Stars
    star
    116
  • Rank 302,103 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SQLAlchemy backup/dump tool for Flask

AlchemyDumps Latest release

Development Status: Alpha Python Version GitHub Actions Coverage Status

Do you use Flask with SQLAlchemy? 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.

Install

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 object from the package.
  • The last line instantiates AlchemyDumps for your app and database.
from flask import Flask
from flask_alchemydumps import AlchemyDumps
from flask_sqlalchemy import SQLAlchemy

# init Flask
app = Flask(__name__)

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

# init Alchemy Dumps
alchemydumps = AlchemyDumps(app, db)

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_SERVER=ftp.server.com
export ALCHEMYDUMPS_FTP_USER=johndoe
export ALCHEMYDUMPS_FTP_PASSWORD=secret
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)
    …

.gitignore

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

Examples

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('user.id'))

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

You can backup all your data

python manage.py alchemydumps create

Output:

==> 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 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

You can restore a backup

python manage.py alchemydumps restore -d 20141115172107

Output:

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

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: 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 manage.py alchemydumps autoclean

Output:

==> 8 backups will be kept:

    ID: 20130703225903 (from Jul 03, 2013 at 22:59:03)
    /vagrant/alchemydumps/db-bkp-20130703225903-User.gz
    /vagrant/alchemydumps/db-bkp-20130703225903-Post.gz

    ID: 20120405013054 (from Apr 05, 2012 at 01:30:54)
    /vagrant/alchemydumps/db-bkp-20120405013054-User.gz
    /vagrant/alchemydumps/db-bkp-20120405013054-Post.gz

    ID: 20101123054342 (from Nov 23, 2010 at 05:43:42)
    /vagrant/alchemydumps/db-bkp-20101123054342-User.gz
    /vagrant/alchemydumps/db-bkp-20101123054342-Post.gz

    ID: 20090708100815 (from Jul 08, 2009 at 10:08:15)
    /vagrant/alchemydumps/db-bkp-20090708100815-User.gz
    /vagrant/alchemydumps/db-bkp-20090708100815-Post.gz

    ID: 20081208191908 (from Dec 08, 2008 at 19:19:08)
    /vagrant/alchemydumps/db-bkp-20081208191908-User.gz
    /vagrant/alchemydumps/db-bkp-20081208191908-Post.gz

    ID: 20070114122922 (from Jan 14, 2007 at 12:29:22)
    /vagrant/alchemydumps/db-bkp-20070114122922-User.gz
    /vagrant/alchemydumps/db-bkp-20070114122922-Post.gz

    ID: 20060911035318 (from Sep 11, 2006 at 03:53:18)
    /vagrant/alchemydumps/db-bkp-20060911035318-User.gz
    /vagrant/alchemydumps/db-bkp-20060911035318-Post.gz

    ID: 20051108082503 (from Nov 08, 2005 at 08:25:03)
    /vagrant/alchemydumps/db-bkp-20051108082503-User.gz
    /vagrant/alchemydumps/db-bkp-20051108082503-Post.gz

==> 11 backups will be deleted:

    ID: 20120123032442 (from Jan 23, 2012 at 03:24:42)
    /vagrant/alchemydumps/db-bkp-20120123032442-User.gz
    /vagrant/alchemydumps/db-bkp-20120123032442-Post.gz

    ID: 20101029100412 (from Oct 29, 2010 at 10:04:12)
    /vagrant/alchemydumps/db-bkp-20101029100412-User.gz
    /vagrant/alchemydumps/db-bkp-20101029100412-Post.gz

    ID: 20100526185156 (from May 26, 2010 at 18:51:56)
    /vagrant/alchemydumps/db-bkp-20100526185156-User.gz
    /vagrant/alchemydumps/db-bkp-20100526185156-Post.gz

    ID: 20100423085529 (from Apr 23, 2010 at 08:55:29)
    /vagrant/alchemydumps/db-bkp-20100423085529-User.gz
    /vagrant/alchemydumps/db-bkp-20100423085529-Post.gz

    ID: 20081006074458 (from Oct 06, 2008 at 07:44:58)
    /vagrant/alchemydumps/db-bkp-20081006074458-User.gz
    /vagrant/alchemydumps/db-bkp-20081006074458-Post.gz

    ID: 20080429210254 (from Apr 29, 2008 at 21:02:54)
    /vagrant/alchemydumps/db-bkp-20080429210254-User.gz
    /vagrant/alchemydumps/db-bkp-20080429210254-Post.gz

    ID: 20080424043716 (from Apr 24, 2008 at 04:37:16)
    /vagrant/alchemydumps/db-bkp-20080424043716-User.gz
    /vagrant/alchemydumps/db-bkp-20080424043716-Post.gz

    ID: 20080405110244 (from Apr 05, 2008 at 11:02:44)
    /vagrant/alchemydumps/db-bkp-20080405110244-User.gz
    /vagrant/alchemydumps/db-bkp-20080405110244-Post.gz

    ID: 20060629054914 (from Jun 29, 2006 at 05:49:14)
    /vagrant/alchemydumps/db-bkp-20060629054914-User.gz
    /vagrant/alchemydumps/db-bkp-20060629054914-Post.gz

    ID: 20060329020048 (from Mar 29, 2006 at 02:00:48)
    /vagrant/alchemydumps/db-bkp-20060329020048-User.gz
    /vagrant/alchemydumps/db-bkp-20060329020048-Post.gz

    ID: 20050324012859 (from Mar 24, 2005 at 01:28:59)
    /vagrant/alchemydumps/db-bkp-20050324012859-User.gz
    /vagrant/alchemydumps/db-bkp-20050324012859-Post.gz

==> 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.6+.

Tests

If you wanna run the tests for the current Python version:

poetry install
poetry run nosetests

We also use some style and quality checkers:

poetry run black . --check
poetry run flake8 flask_alchemydumps/ tests/

If you wanna cover all supported Python version, you need them installed and available via pyenv. Then just poetry run tox.

More Repositories

1

minha-receita

🏢 Sua API web para consulta de informações do CNPJ da Receita Federal
Go
1,204
star
2

twitter-cleanup

🛁 Clean-up inactive accounts and bots from your Twitter
Python
288
star
3

yaml.nvim

🍒 YAML toolkit for Neovim users
Lua
169
star
4

calculadora-do-cidadao

💵 Tool for Brazilian Reais monetary adjustment/correction
Python
149
star
5

my-internet-speed

🛎️ Monitor the speed your ISP is delivering
Python
135
star
6

bot-followers

🍊 Find out how many bots follow any given Twitter acount
Python
115
star
7

getgist

🖥️ Easily download any file from a GitHub Gist, with one single command.
Python
103
star
8

docs-cpi-pandemia

😷 Dowload dos documentos da CPI da Pandemia
Go
99
star
9

whiskyton

Whiskyton, find whiskies that you like!
Python
84
star
10

fio-de-ariadne

🪁 Structuiring data on missing kids in Brazil
Python
77
star
11

elm-format-number

✨Format numbers as pretty strings
Elm
60
star
12

chunk

🧱 Chunk is a download manager for slow and unstable servers
Go
53
star
13

pwned-antifas

HTML
51
star
14

brazilian-cities

A script to generate list with all Brazilian cities and states
Python
48
star
15

createnv

🧞‍♀️Automagically creates .env files
Python
40
star
16

django-public-admin

🔓 A public and read-only version of the Django Admin
Python
37
star
17

webassets-elm

📦Filter for compiling Elm files using webassets
Python
36
star
18

cunhajacaiu

Brazilian website counting the days to the fall of Cunha
Elm
27
star
19

django-ajax-contacts

Web app to discuss responsabilities of backend and frontend code
Python
26
star
20

raspadorlegislativo

Testes de código para integrar, futuramente, o Radar Legislativo
Python
19
star
21

go-cpf

CPF validation in Go
Go
17
star
22

go-cnpf

🇧🇷 CPF and CNPJ validation in Go
Go
16
star
23

fuckoff-twitter

Fuck off @twitter, let me have "latest tweets" as a default.
Rust
14
star
24

cara-de-nordestino

Você tem cara de nordestino?
Elm
14
star
25

grupy-python-tdd-flask

Palestra para encontro do Grupy-SP em 12 de março de 2016.
Python
14
star
26

PhD

My PhD writings
CSS
14
star
27

dotfiles

🥰 My dot files
Lua
13
star
28

em-nome-da-lai

🧑‍⚖️ Em nome da LAI! Gerador de petições com base na LAI.
Elm
13
star
29

hangouts-django-ajax

Source for a Hangouts On Air about Django & AJAX
Python
11
star
30

UOLEsportesSexista

Chrome Extension that redacts sexist contents from UOL sports coverage.
CoffeeScript
11
star
31

ezz

🎦 ezz is a simple CLI tool to schedule Zoom meetings
Rust
10
star
32

scrapy-memcached-cache

🗂Memcached HTTP cache storage backend for Scrapy
Python
10
star
33

hangouts-mock

Source code for a live coding session about Python's unittest.mock
Python
9
star
34

findaconf

Mock-up for Find a Conference
Python
7
star
35

vamos-aprender-elm-api

API created for educational purposes while teaching Elm
Python
7
star
36

wed

🌤 Track the weather for event days
Rust
7
star
37

lista-de-compras

🛒 Bot de lista de compras compartilhada para o Telegram
Python
6
star
38

its-wednesday

Captain, it's Wednesday!
Go
5
star
39

vamos-aprender-elm

Source codes from a series of live coding in Elm
Elm
5
star
40

pagamentos-alesc

Python
5
star
41

from-my-ex

💕 Sending all the love from my ex to my new happy and promisucous social media life
Python
4
star
42

spellfile.nvim

🌕 Port of spellfile.vim to Lua without depending on netrw
Lua
4
star
43

go-cnpj

CNPJ validation in Go
Go
4
star
44

boteco

CLI to create redirections in CloudFlare to Zoom meetings.
Rust
4
star
45

grrs

This is me studying Rust having Command Line Applications in Rust book as a starting point.
Rust
4
star
46

triathlon-live-calendar

📅 Calendar file generator for triathlonlive.tv upcoming events
Python
4
star
47

grupy-porque-elm

Código fonte para a palestra que ofereci no encontro do Grupy-SP, em 17 de setembro de 2016.
Elm
3
star
48

sortimages

Python
3
star
49

twitter-filtered-stream-client

🏞️ Twitter Filtered Stream API Client
Go
3
star
50

hangouts-django-react

Source for a Hangouts On Air about Django & ReactJS
Python
3
star
51

filterss

FilteRSS - More power over your feeds
HTML
3
star
52

palestra-relampago-git-bisect

Mini-calculadora escrita como exemplo para uma palestra relâmpago sobre `git bisect`
Python
3
star
53

ironbot

🚵 CLI to get information about Ironman professional races
Python
2
star
54

repo-birthday

🎂 Know when your repos are celebrating their birthdays!
Rust
2
star
55

MeDontLikeFB

Make your Facebook better, stop liking things.
JavaScript
2
star
56

astronomer

⭐️ Astronomer looks at the stars. At all the stars you’ve got on GitHub.
Rust
1
star
57

not-my-ex

🐝 Tiny CLI to post simultaneously to Mastodon and Bluesky
Python
1
star
58

minha-receita-mirror

Simple UI for R2 objects
1
star
59

ftp-based-zones

⚡ Cycling FTP-based training zones
Elm
1
star
60

cuducos.me

Source files of my personal webpage
HTML
1
star
61

manifestacoes-no-twitter

Python
1
star
62

airnope

☔ AirNope keeps Telegram groups clean from ”airdrop” spam
Rust
1
star