Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dotenv in sync #287

Closed
BenBao93 opened this issue Jan 25, 2021 · 4 comments
Closed

Dotenv in sync #287

BenBao93 opened this issue Jan 25, 2021 · 4 comments
Assignees

Comments

@BenBao93
Copy link
Contributor

BenBao93 commented Jan 25, 2021

I am having trouble to use env vars in the sync configuration. When deploying backups with a config like the following it works for the local backup but all env vars in the sync section return empty:

<?xml version="1.0" encoding="UTF-8"?>
<phpbu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://schema.phpbu.de/5.0/phpbu.xsd">
    <adapters>
        <adapter type="dotenv" name="env">
            <option name="file" value=".env"/>
        </adapter>
    </adapters>
    <backups>
        <backup name="database">
            <!-- source -->
            <source type="mysqldump">
                <option name="databases" value="adapter:env:DB_DATABASE"/>
                <option name="user" value="adapter:env:DB_USER"/>
                <option name="password" value="adapter:env:DB_PASSWORD"/>
            </source>
            <target dirname="/tmp"
                    filename="mysqldump-%Y%m%d-%H%i.sql"
                    compress="bzip2"/>
            <sync type="amazons3">
                <option name="key" value="adapter:env:S3_BACKUP_KEY"/>
                <option name="secret" value="adapter:env:S3_BACKUP_SECRET"/>
                <option name="bucket" value="adapter:env:S3_BACKUP_BUCKET"/>
                <option name="region" value="adapter:env:S3_BACKUP_REGION"/>
                <option name="path" value="/"/>
            </sync>
        </backup>
    </backups>
</phpbu>

I checked the output in the S3 sync files and indeed all config vars are empty so I am getting errors like:

array(6) {
  ["key"]=>
  string(0) ""
  ["secret"]=>
  string(0) ""
  ["bucket"]=>
  string(0) ""
  ["region"]=>
  string(0) ""
  ["path"]=>
  string(1) "/"
}
Time: 1 second, Memory: 4.00MB

Exception 'phpbu\App\Backup\Sync\Exception' with message 'key is mandatory'
in src/Backup/Sync/AmazonS3.php:148

The env is correctly set and loaded tho. Are environment variables limited or should it work anywhere in the configuration including the sync sections?

@sebastianfeldmann
Copy link
Owner

sebastianfeldmann commented Jan 25, 2021

As far as I can see it should not be limited in any way.
All configuration options are handled by getAdapterizedValue meaning they all get checked for placeholders.

In fact I tried to use a dotenv adapter variable in a AmazonS3 sync configuration and it worked just fine.

  <adapters>
    <adapter type="dotenv" name="dotenv">
      <option name="file" value=".env"/>
    </adapter>
  </adapters>
  ...
  <sync type="amazons3">
    <option name="key" value="adapter:dotenv:TEST" />

Simulate output shows something like this.

sync backup to Amazon S3
  region:   eu-central-1
  key:      dotenvtest

So the .env adapter would fail if the .env file would not be readable.
The Configuration would fail if the name of the adapter would be wrong.
So the issue must be somewhere else 🤔
Maybe dotenv isn't loading the file correctly or can you verify that the environment variables are loaded properly by dotenv? Maybe some upper lower case issues?

Which version of phpbu are you using? PHAR, Composer? And if you are using Composer, which version of dotenv are you using?

@BenBao93
Copy link
Contributor Author

BenBao93 commented Feb 1, 2021

I did some more tests and always had the same result. Variables from env are empty in sync, even using the same vars that work 3 lines above in the source block resolve empty in sync.

When I load the same .env in PHP and dump it all vars show correctly.

I installed phpbu via composer and use version 6.0.14, vlucas/phpdotenv is v5.3.0

@BenBao93
Copy link
Contributor Author

BenBao93 commented Feb 1, 2021

This seems to be related to vlucas/phpdotenv. I downgraded to version 4.2 and everything works.

@sebastianfeldmann
Copy link
Owner

There is a problem with dotenv >= 5.0
It seems the current implementation using getenv() isn't working anymore.

I changed the implementation to $_ENV and it seems to work.
I will release a patch with the next release.

Until then you should be able to use dotenv version 4.X.

sebastianfeldmann added a commit that referenced this issue Feb 1, 2021
Since version 5 the usage of 'getenv' is not
working anymore.

This fixes issue #287
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants