Search code examples
bashopenssl

How can I provide a password to openssl enc in a bash script?


i have a bash script to dump mysql and making tar and protect it with openssl

tar -cf ${DB}_${DATE}.tar *.sql | openssl enc -aes-256-cbc -pbkdf2 -e > ${DB}_${DATE}.tar.gz.enc  > /dev/null 2>&1

but my bash script will stop because of ask password how can i fill the passwords in bash script ?


Solution

  • As the manual tells you, -pass source specifies a location from which openssl will read the password to use.

    Assuming this is a bash script instead of a sh script, you can use process substitution:

    tar -czf "${DB}_${DATE}.tar" *.sql |
      openssl enc -aes-256-cbc -pbkdf2 -e -pass file:<(echo "password") \
      >"${DB}_${DATE}.tar.gz.enc" 2>/dev/null
    

    Note that redirecting stderr to /dev/null is a bad idea -- I'm doing it because it's what your original code did, but it makes it impossible to troubleshoot failures.