Exporting Databases

Lando ships with a helper db-export script that is available in all our LAMP and LEMP based recipes. Used in the recipe context it should export a database dump DATABASE.TIMESTAMP.gz into the /app directory.

Postgres export not yet supported

Lando will only handle mysql or mariadb exports at this time.

Usage

At the command line execute:

lando db-export

Examples

# Export to a file named `DATABASE.TIMESTAMP.gz`
lando db-export

# Export to a file called dump.sql.gz
lando db-export dump.sql.gz

# Export a secondary database
lando db-export --host db2 --database dataz

# Dump the result to stdout
lando db-export --stdout

Options

Options:
  --host, -h      The database host
  --user, -u      The database user                            [default: "root"]
  --database, -d  The database name
  --password, -p  The database password
  --port, -P      The database port                              [default: 3306]
  --stdout        Dump database to stdout

Adding the db-export command

If you are not using one of our php-y recipes you can add the db-export command and default options to your .lando.yml as follows.

# The name of the app
name: mysql-export

# Give me http://mysql-export.lndo.site and https://mysql-export.lndo.site
proxy:
  nginx:
    - mysql-export.lndo.site

# Set up my services
services:

  # Set up a basic webserver running the latest nginx with ssl turned on
  appserver:
    via: nginx
    type: php:7.1
    ssl: true
    webroot: www

    # These environmental variables will set the default host/user/port/name/password options for
    # db-export
    overrides:
      services:
        environment:
          DB_HOST: db
          DB_USER: mysql
          DB_PASSWORD: mysql
          DB_NAME: data1
          DB_PORT: 3306

  # Spin up a mariadb container called "database"
  # NOTE: "database" is arbitrary, you could just as well call this "db" or "kanye"
  db:

    # Use the latest version of mysql
    type: mysql

    # Optionally forward out our port so we can access it at `localhost:someport`.
    # This port will change every time you restart this app
    #
    # You can also set this to a static port you want to use `portforward:3311`.
    # You will need to make sure the port is open and avialable
    portforward: 5309

    # Optionally change the default db credentials
    creds:
      user: mysql
      password: mysql
      database: data1

    # Optionally load in all the mysql config files in the config directory
    # This is relative to the app root
    # config:
    #  confd: config

tooling:
  db-export:
    service: appserver
    needs:
      - db
    description: Export a database from your lando app
    cmd: /helpers/mysql-export.sh
    options:
      host:
        description: The database host
        alias:
          - h
      user:
        description: The database user
        default: root
        alias:
          - u
      database:
        description: The database name
        alias:
          - d
      password:
        description: The database password
        alias:
          - p
      port:
        description: The database port
        default: 3306
        alias:
          - P
      stdout:
        description: Dump database to stdout
  'db-import ':
    service: appserver
    needs:
      - db
    description: Import  into database
    cmd: /helpers/mysql-import.sh
    options:
      host:
        description: The database host
        alias:
          - h
      user:
        description: The database user
        default: root
        alias:
          - u
      database:
        description: The database name
        alias:
          - d
      password:
        description: The database password
        alias:
          - p
      port:
        description: The database port
        default: 3306
        alias:
          - P
  mysql:
    service: appserver
    description: Drop into a mysql shell for the import target
    needs:
      - db
    cmd: mysql -h db -u mysql -pmysql data1

You will need to rebuild your app with lando rebuild to apply the changes to this file. You can check out the full code for this example over here.

results matching ""

    No results matching ""