backing up database with cron job in Ubuntu and parallel plesk - php

I am using a php script to backup my database. Below is the given script:
$datestamp = date("Y-m-d_h-i-s"); // Current date to append to filename of backup file in format of YYYY-MM-DD
/* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR SETUP */
$dbuser = "xyz"; // Database username
$dbpwd = "*****"; // Database password
$dbname = "mydatabase"; // Database name. Use --all-databases if you have more than one
$filename = "backup-$datestamp.sql.gz"; // The name (and optionally path) of the dump file
$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = system($command);
Below is the command line
php5 /var/www/vhosts/cc.astra1641.startdedicated.de/httpdocs/cron/backup.php
I have output the $result variable using var_dump to check it returns empty string. also I have tried using other methods like exec($command) or passthru($command), it doesn't work.

Related

How can I find out, check and change my $db_host, user, pass, name in mysql workbench?

$db_host = "";
$db_user = "";
$db_pass = "";
$db_name = "";
I have to fill out these, so I wanted to check but I used 'ampps' to install mysql.
so I couldn't find info in my database.
where should I check in below pic?
mysql wrokbench
wrokbench2
Detault mysql login credentials in AMPPS are :
Username : root
Pass : mysql
You can create a new database using phpMyAdmin :
http://localhost/phpmyadmin/

MySQL PHP: PDO connection not working when getting credentials from file()

I've already searched for an answer to this questions, but all that I found didn't work for me. I've a problem with the pdo connection in php.
When I enter the mysql datas directly into the pdo statement, it works properly. But when I use variables instead, it doesn't. I already looked up how to include variables into a pdo statement, but it didn't work.
This is the code, when it works:
$host = 'localhost';
$username = 'root';
$password = 'password';
$dbname = 'Database';
$con = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
And here is the code, when it doesn't work:
$file = "mysqldatas.txt";
$lines = file($file);
$host = $lines[1];
$username = $lines[2];
$password = $lines[3];
$dbname = $lines[4];
$con = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
As you can see, I try to read the datas from a text file the second time. But I don't see where the difference between 'localhost', and $lines[1] is, because when I echo the $lines[1], the output is localhost, as it should be.
Please help me guys, this is really annoying. It would be great, if you could also explain why it makes a difference between directly entering the hostname, or using a variable, that holds the hostname (as I said, I used echo, and it said localhost).
Thank you for your help guys!
Bye.
The actual issue is that the file() will also get newlines with each line, unless the FILE_IGNORE_NEW_LINES is used (or some sort of trim() function). So you'll need to use
$lines = file($file, FILE_IGNORE_NEW_LINES);
instead. From the manual of file(), it tells us that
Note:
Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.
trim() is also a possibility instead of this flag.
There's a few other, different possibilities to separate credentials and connections, the most common one is to either use a file connection.php which creates the object, where you just include that, and use that wherever you need a connection, or creating your own .ini file with the credentials, and getting it via parse_ini_file().
connection.php
$host = 'localhost';
$username = 'root';
$password = 'password';
$dbname = 'Database';
$con = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
Other files
require "path/to/connection.php";
$con->prepare(...);
Then there's usage of .ini files. You can create a config.ini file, and get the values from parse_ini_file()
config.ini
host = localhost
dbname = Database
user = root
password = password
type = mysql
Connection
$config = parse_ini_file("config.ini");
$con = new PDO($config['type'].":host=".$config['host'].";dbname=".$config['dbname'], $config['username'], $config['password']);
References
http://php.net/manual/en/function.file.php
http://php.net/manual/en/function.trim.php or http://php.net/manual/en/function.rtrim.php
http://php.net/manual/en/function.parse-ini-file.php
pretty sure you are getting newlines when you use the file function. try this:
$file = "mysqldatas.txt";
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$host = $lines[1];
$username = $lines[2];
$password = $lines[3];
$dbname = $lines[4];
$con = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
From http://php.net/manual/en/function.file.php :
Return Values ¶
Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file() returns FALSE.

windows/php The system cannot find the path specified error

I have this script to access my mysql database and do some maintenance.
I am on windows platform.
Mysqldump is installed at C:\Program Files\MySQL\MySQL Server 5.7\bin
and PHP.exe is installed at C:\TCAFiles\xampp\php
I have a database.php with
<?php
// Database Connection Setup
// -------------------------------------------------------
$dbhost = '127.0.0.1';
$dbname = 'dbname';
$dbuser = 'username';
$dbpass = 'password';
// -------------------------------------------------------
$db_local = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
?>
I have a cleanup.php
<?php
// Set the protection period
$ProtectionPeriod = 97;
echo "================================\nDatabase Cleanup Started:\n================================\n";
include 'database.php';
// Do db backup
// -------------------------------------------------------
// if mysqldump is on the system path you do not need to specify the full path
// simply use "mysqldump --add-drop-table ..." in this case
$date = date("Y-m-d_H-i-s");
$dumpfname = 'D:\\Tinboye\\backups\\'.$dbname.'_'.$date.'.sql';
$command = "C:\\Program%20Files\\MySQL\MySQL%20Server%205.7\\bin\\mysqldump --add-drop-table --host=$dbhost --user=$dbuser --password=$dbpass exile > $dumpfname";
system($command);
echo "backup $dumpfname made\n\n";
// delete push bikes
$sql = "DELETE FROM vehicle WHERE class = 'Exile_Bike_OldBike' OR class = 'Exile_Bike_MountainBike'";
$result = mysqli_query($db_local, $sql);
// Delete players not logged in for $ProtectionPeriod days with less than 10 total_connections
$sql = "DELETE FROM player WHERE account_uid IN (SELECT uid FROM account WHERE last_connect_at < NOW() - INTERVAL $ProtectionPeriod DAY)";
$result = mysqli_query($db_local, $sql);
// Remove empty containers not used in 48 hours
$sql = "DELETE FROM container WHERE last_updated_at <= NOW() - INTERVAL 48 HOUR AND cargo_items = '[[],[]]' AND cargo_magazines = '[]' AND cargo_weapons = '[]' AND cargo_container = '[]'";
$result = mysqli_query($db_local, $sql);
?>
and a bat file to run the php script
#echo off
::=====================================================================================================================================
:: Database Housekeeping backup & clean
::=====================================================================================================================================
C:\TCAFiles\xampp\php\php.exe D:\Tinboye\servers\jnjexile\ExileCleanup\exile_cleanup.php
pause
but when i execute the script I get an error
================================
Database Cleanup Started:
The system cannot find the path specified.
backup D:\Tinboye\backups\exile_dayz_2016-04-09_23-30-31.sql made
Press any key to continue . . .
Terminate batch job (Y/N)?
when i go to the folder D:\Tinboye\backups there is the file created, but no data, which leads me to believe that the mysqldump is not found.
anyone see the problem here?

PHPmyadmin umlaut is going to be '?'

Im trying to write into my database through android (PHP script).Now... When I want to write a sentence like "Ich bin Müde" (German) it saves it like "Ich bin M?de". I already tried to change the collation of my column from latin1_general_ci to latin1_german1_ci but then I don't get any inputs. So the script is not saving my data into my tables. Is there a easy for a solution? Thanks to everyone
Php script
<?php
$hostname = "host";
$username = "admin";
$password = "pass";
$localhost = mysql_connect($hostname,$username,$password)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db("db");
$sql = mysql_query("INSERT INTO tbas VALUES(23,NULL,22,'Ich bin Müde','Ich bin Müde 2'));";
...
?>

mysqldump create empty file in win7 wamp

Here is my code
<?php
$dbuser = "root";
$dbpass = "";
$dbname = "test";
$backupfile = $dbname .time()."-".date("Y-M-D h:i:s");
//$query= "C:\\wamp\\bin\\mysql\\mysql5.5.16\\bin\\mysqldump.exe " ."-u ". $dbuser ." -p ". $dbname.">". $backupfile.".sql";
$query= "C:\wamp\bin\mysql\mysql5.5.16\bin\mysqldump.exe " ."-u ". $dbuser ." -p ". $dbname.">". $backupfile.".sql";
echo $query;
system($query);
?>
But i am always getting blank file. I am using wamp server in windows 7. I have tried with double slashes and single slashes but same result. Please help me with this essue.
N.B- One things i have not mentioned yet that, when i open this in browser, the empty backup file created, but page loading not stop,its show loading....
$query= "C:\\wamp\\bin\\mysql\\mysql5.5.16\\bin\\mysqldump.exe --user root test > upload/". time()."_backup.sql";
Finally it works. I have use --user instead of -u, as i don't have any password for wamp server, i removed --password field. Now its working fine.
The command is asking for password, you must remove --password parameter or -p flag if you mysql root user password is blank.

Categories