I run a mongodump command from php:
$cmd = "mongodump --host=" . $host . " --port=" . $port . " --db=" . $db . " --username=". $username . " --collection=" . $collectionName . " --out=" . $path . " -p " . $password;
$res = shell_exec($cmd);
Sometimes the connection to the mongo server is down and the dump operation fails, or even the authorisation to the server fails, but I have no way of knowing it through my code.
I have found a temporary solution, which is far from being a good one, by checking the presence of both .bson and .metadata.json files, while the metadata.json file is the more important check, as if the dump fails in the middle, the .bson file still exists, but the metadata.json file is not present
Is there any other way to verify the result of the dump operation ?
Related
I'm testing this script on the local server
$dump_path = "backups";
$host = "localhost";
$user = "root";
$pass = "";
$command=$dump_path.'mysqldump -h '.$host.' -u '.$user.' dental > dental_b.sql';
if (system($command)) {
echo "YES" ;
} else {
echo "Error" ;
}
But why isn't that script executed and shows an error although all data from the DB is true.
I'm pretty sure you don't need the host option if you're using mysqldump on localhost. The way you are concatenating your command looks messed up to me. Let me show you what works for me. I use this all the time:
<?php
$user = "root";
$pass = "";
$database = "dental";
// File name
$file_name = 'dental_b.' . date('mdY.Hia') . '.sql.gz';
// Storage directory
$storage_dir = __DIR__ . '/backups';
if( ! is_dir( $storage_dir ) )
mkdir( $storage_dir, 0777, TRUE );
// Absolute path to new file
$absolute_path = $storage_dir . '/' . $file_name;
// Create the backup file
exec( 'mysqldump -u ' . $user .
' -p' . $pass .
' ' . $database .
' | gzip > ' . $absolute_path );
Yes, I am gzipping the file. I think you'll find this is a good option for storage, emailing, etc.
EDIT ---
If you are not using a password, you should not use -p. You'd want to alter the command in that case.
I am using function based database connection and select query also plz help me.
FIRST CODE:
function connectDb(){
//my postgresql db connection
$link=pg_pconnect('host=' . __DB_HOST__ . ' port=' . __DB_PORT__ . ' dbname=' . __DB_NAME__ . ' user=' . __DB_USER__ . ' password=' . __DB_PWD__) or die('connection failed');
AllDelete();// Audit Logs Auto Deletion
}
SECOND CODE:
function AllDelete(){
$MysqlForLogs=pg_query("select audit.*,users.username from audit,users where audit.user_id=users.id and DATE_PART('day',now()-audit.date) > 7") or die(pg_last_error());
$CountRows=pg_num_rows($MysqlForLogs);
}
I am executing the browser it will show on error like 'No database selected' but really i am giving the database name.
What is the problem?
I aleady have a code here and it works when I use it on the command prompt directly using this cd C:\wamp...... to point out the bin and use the following code below to dump:
<?php
DEFINE("HOST","localhost"); // Host Name
DEFINE("USER","root"); // Database Username
DEFINE("PASSWORD",""); // Database Password
DEFINE("DATABASE","db_osgamdrp"); // Database Name
DEFINE("TIMEZONE","Asia/Manila"); // Server Timezone
DEFINE("BIN","C:\wamp\bin\mysql\mysql5.6.12\bin") // MySQLi BIN
$Status = 1;
$Message = "";
$Command = "mysqldump --opt -h " . HOST . " -u " . USER . " -p " . PASSWORD . "" . DATABASE . " > ../../../../../resources/database_backup/" . DATABASE . date("Y-m-d-H-i-s") . ".gz";
if (!exec($Command)) {
$Status = 0;
$Message = "Something Went Wrong. Please Try Again";
}
?>
Whats wrong with this code? Its not working.
I am Using Postgresql for my PHP project which is built in Codeigniter, the size of this database is very large there are upto 500 tables in my project i have used the utility class for backup but it failed due to size of database now in google search i found a solution pg_dump , but can't find a suitable example for my problem. Here is my code but it only generate an empty sql file.
$username = $this->db->username;
$password = $this->db->password;
$hostname = $this->db->hostname;
$dbname = $this->db->database;
$dataDir = DB_BACKUP;
$pgDumpDir = "/usr/bin/pg_dump";
$dated = date('d-m-Y');
$fileName = $dataDir . "backup_" . $dated . ".sql";
$command = $pgDumpDir;
$command .=" --username ".$username;
$command .= " ".$dbname." > ".$fileName;
echo "ok " . shell_exec($command);
I have had written the following snippet that generates the mysql database dump and saves it on the server:
public function save_db_backup()
{
$DBUSER=$this->db->username;
$DBPASSWD=$this->db->password;
$DATABASE=$this->db->database;
$filename = $DATABASE . "-" . date("Y-m-d_H-i-s") . ".sql.gz";
$save_path = $_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/' . $filename;
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > " . $save_path;
exec( $cmd );
}
It has been working fine on my other server. But after moving my site to this new server, it has suddenly stopped working i.e. database backup file is not being saved at the path specified. Also, I have checked exec is enabled on the server plus the directory is readable and also writeable:
is_readable($_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/') // true
is_writable($_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/') // true
I have checked and the database credentials are alright. I have tried with the path to mysqldump and that didn't work either:
$cmd = "/usr/bin/mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > " . $save_path;
What problem could there possibly be?
You should check what user launches the command, and then if the dir is readable and also writeable by THAT user.
If you are launching it from a PHP script, it's the probable cause.