0

I have a server running Ubuntu 10, it mostly has PHP websites with MySQL databases.

I need to write a backup script

  1. to backup the database
  2. to zip up all the php files.

Is it better security to do write my backup script in PHP or shell? Or does it not matter?

I like PHP more, because that lets me have the database password in only one place, a PHP file.

If I write this in shell, then Ill need to put the database password in the shell script. When I change the password, then Ill need to change it in two places.

user9517
  • 115,471
  • 20
  • 215
  • 297
davidjhp
  • 700
  • 3
  • 7
  • 14

2 Answers2

1

Speaking form the security point of view, best would be if your mysql password would not be exposed at all / be hardcoded in the script. Both php and shell scripts will need the password decrypted if you plan to execute an external tool, such as mysqldump, to backup up the database so - no, there's no extra security, whichever solution you choose. Concerning the last question, it's very easy for a shell script to grab a password from a php configuration file (hint: grep and sed are your friends here), so you don't have to modify in a lot of places. Concerning php versus shell - the cli of php does pretty well, the only difference is that you may have to write larger amounts of code in php for the same thing where shell scripting would fit in one line.

O G
  • 874
  • 4
  • 6
1

Write the script as root, put the password into root's .my.cnf (with permissions 600). You can then run mysql commands without ever having to pass the password.

I can hear a million people screaming already - but my logic is simple. If someone breaks into your server and manages to escalate their privileges to root, you've already lost and they already have access to everything.

If only root can read .my.cnf then it's as secure as putting a password into any other file, but it means you never run scripts passing the password on the command line which can be read by any user using ps -ef.

EightBitTony
  • 9,311
  • 1
  • 34
  • 46
  • Anyone who screams at your suggestion doesn't know what they're talking about. It's the *best* possible method. – womble Aug 22 '11 at 00:01