Re: One Button DB backup [Solved]
| From: | Tedd Sperling | Date: | Thu, 24 Nov 2016 16:30:48 +0000 |
| Subject: | Re: One Button DB backup [Solved] | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-325838@lists.php.net to get a copy of this message | ||
> On Nov 5, 2016, at 4:15 PM, Adam Jon Richardson <adamjonr@gmail.com> wrote:
>
>> Something like this little bit of code works:
>>
>> $command = "mysqldump --add-drop-table -h localhost " .
>> "-u USERNAME -p PASSWORD DBNAME > FILETOPIPETO";
>> $ret_val = null;
>> // create current backup
>> $output = system($command, $ret_val);
>
> Just a few quick follow-up points on this method:
>
> 1) This approach shouldn't be used on a shared server, as the command-line
> logs could be viewed by others to gain the DB credentials (not an issue in
> the cases I've used this approach, as the server was only hosting one
> website.)
>
> 2) I forgot that this approach requires no space between the password and
> the -p argument, so it should read:
> $command = "mysqldump --add-drop-table -h localhost " .
> "-u USERNAME -pPASSWORD DBNAME > FILETOPIPETO";
> $ret_val = null;
> // create current backup
> $output = system($command, $ret_val);
Adam:
It worked, but I modified the code a bit:
//=== start of code ===
$filename = "backup-" . date("d-m-Y") . ".sql";
$cmd = "mysqldump --opt -h $db_host -u $db_user -p$db_pass $db_name > $filename";
$null = null;
$content = system($cmd, $null);
exit(0);
//=== end of code ===
The “opt" does a lot of things including locking the db down.
Thanks!
Cheers,
tedd
_______________
tedd sperling
tedd@sperling.com