Sqlite3 dump database through PHP - php

I want to be able to dump a whole sqlite database to a file (database.sql) so as to use it as a backup. The problem is that exec() in PHP doesn't seem to run correctly the ".dump" command as it gives only "COMMIT;" as a result and not the whole text:
$db = new SQLite3('checks_db.db');
$results=$db->exec('.dump');
$content=$results->fetchArray();
If I run it from sqlite3 it gives both the schema and the insert insrtuctions with the data.
Perhaps I should also mention that the database has 777 permissions.
Can anyone write an example of the syntax of the '.dump' command in php-sqlite3?

The SQLite database does not have a .dump command.
The sqlite3 command-line shell has a .dump command.
To be able to use it, you would have to execute that tool; something like this:
exec('sqlite3 /some/where/checks_db.db .dump', $output);
The easiest way to make a backup would be to copy the database file itself, but using the shell's .backup command would be safer.

Related

PHP mysqldump creates empty file

I'm trying run exec with mysqldump in my PHP file. It doesn't work, I'm even trying to use it without variables like this:
exec('mysqldump --user=xx --password=xx --host=xx db settings > /output/dump5.sql');
It creates empty file.
But when I run this command in ssh, it works. Why?
Do you need to do this from php script ?
I assume that you want to do the backup frequently, so maybe adding it to cron job will better solution.

Restore a postgresql dump (.sql file) without the command line?

Scenario:
I have built a PHP framework that uses a postgresql database. The framework comes shipped with a .sql file which is a dump of the default tables and data that the framework requires.
I want to be able to run the sql file from the client (PHP), rather than the command line, in order to import the data. This is because I have come across some server setups where accessing the command line is not always a possibility, and/or running certain commands isn't possible (pg_restore may not be accessible to the PHP user for example).
I have tried simply splitting up the .sql file and running it as a query using the pg_sql PHP extension, however because the dump file uses COPY commands to create the data, this doesn't seem to work. It seems to be that because COPY is used, the .sql file expects to be imported using the pg_restore command (unless I am missing something?).
Question:
So the question is, how can I restore the .sql dump, or create the .sql dump in a way that it can be restored via the client (PHP) rather than the command line?
For example:
<?php pg_query(file_get_contents($sqlFile)); ?>
Rather than:
$ pg_restore -d dbname filename
Example of the error:
I am using pgAdmin III to generate the .sql dump, using the "plain" setting. In the .sql file, the data that will be inserted into a table looks like this:
COPY core_classes_models_api (id, label, class, namespace, description, "extensionName", "readAccess") FROM stdin;
1 data Data \\Core\\Components\\Apis\\Data The data api Core 310
\.
If I then run the above sql within a pgAdmin III query window, I get the following error:
ERROR: syntax error at or near "1"
LINE 708: 1 data Data \\Core\\Components\\Apis\\Data The data api Core...
This was a bit tricky to find, but after some investigation, it appears that pg_dump's "plain" format (which generates a plain-text SQL file) generates COPY commands rather than INSERT commands by default.
Looking at the specification for the pg_dump here, I found the option for --inserts. Configuring this option will allow the dump to create INSERT commands where it would normally create COPY commands.
The specification does state:
This will make restoration very slow; it is mainly useful for making dumps that can be loaded into non-PostgreSQL databases. However, since this option generates a separate command for each row, an error in reloading a row causes only that row to be lost rather than the entire table contents.
This works for my purposes however, and hopefully will help others with the same problem!

How would I perform an sqlite backup through php

I am attempting to use the sqlite backup command through php.
This requires 3 statements
Opening the db sqlite3 testing.sqlite
Backup the DB .backup testing_backup.sqlite
Close sqlite .exit
The exec command doesn't like this as it hangs when the process stays open, I have tried running all three commands together using && to join them but this doesn't work either.
Can anyone help me run the 3 commands through php?
This is an attempt to create a backup file to solve a database locking issue.
The sqlite3 command-line shall can also receive command(s) as parameters.
Just execute the following command:
sqlite3 testing.sqlite ".backup testing_backup.sqlite"

Dumping Microsoft SQL database to file using command line

I am attempting to dumping a Microsoft SQL (MSSQL) database to a file similar to how i would dump from a MySQL database using php:
$cmd = "mysql --user=".$db_user." --password=".$db_password." --host=".$hostname." --database=".$database." --execute='SELECT * FROM ".$database." '> file.txt";
exec($cmd);
But i am trying to figure out the similarities in the command line as to how it is done above with MySQL
What is the proper command line syntax to execute this?
Thanks.
First, you need to write and test your BACKUP command in TSQL. Second, you need to execute it, either using sqlcmd.exe (Windows only) or by connecting directly to the database with whatever MSSQL drivers you use in PHP.
Since the SQL Server documentation is very complete and has numerous examples, you should be able to write the commands yourself. If you still have problems, please post what you have tried so far and exactly what you can't figure out.
Finally, you should always mention the version and edition of SQL Server, because it's often important to know when answering questions.

How can i use php to export a mysql table

I am trying to export just the rows of a mysql table without the table information to an xml file. I read that the mysqldump command will get the job done but I cant manage to get the correct syntax. Can someone post an example code for mysqldump command? Thank you.
$command="mysqldump --xml ";
Try the script on this page: http://www.chriswashington.net/tutorials/export-mysql-database-table-data-to-xml-file
By any chance you are not trying to run that command inside mysql_query are you ? It wont work that way. mysqldump is a command line utility.
To run it from php you would need to use the system() function, documentation - http://php.net/manual/en/function.system.php
If you are on a shared host with PHP in safe mode, or the system function is explicitly disabled in php.ini, then you will not be able to do this.
In that case you would need to read the data from your table using a SELECT query and iterating on all rows and potting it into an XML file, using XMLWriter or DOMDocument

Categories