cron job replace (to restore) a database - database

I am setting up a test site to allow users to make changes and I need to restore the database every hour.
I have found this script for the cron that works fine but just for the first time. This script will not restore an existing database because of duplicate entries, so is not good for my test site.
mysql -u user -ppassword databasename < /path/to/backup.sql
All I want to do is to restore my dump database (and possibly all the files as well) but I can't find how to do it.
This is the error that I receive via email when I run this script:
ERROR 1062 (23000) at line 62: Duplicate entry '1' for key 'PRIMARY'
I guess I should have a way of deleting the database first and immediately after restore the original dump.

There are two options that I can think of here:
You can create another backup of your original database with an option that DELETEs the tables before restoring the database. This effectively adds DELETE lines to the generated .sql file. The command to do this is:
mysqldump --add-drop-table -u [username] -p [password] [database] > backup.sql
Use mysqlimport to import a backup for a database that already exists like so:
mysqlimport -u [username] -p [password] [database] backup.sql
Hope this helps.

Related

Do not drop and create database when scripting drop/create database objects and data with mssql-scripter

I'm trying to script the database objects and data of my database to later move it to a server where I don't have backup/restore rights. Instead I'm using the Generate Scripts method and I use mssql-scripter to generate the scripts.
I have a .bat file with the following script code to generate my SQL script file.
set
timevar=%date:~4,2%%date:~7,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%
mssql-scripter --server 10.100.8.8 -d Dev_db -f .\%timevar%.sql
--schema-and-data --script-drop-create --target-server-version 2016 --target-server-edition Standard --check-for-existence --include-dependencies --constraint-names --collation -U ScriptingUser -P 1234 --exclude-use-database
The problem is that it's also scripting DROP DATABASE and CREATE DATABASE, which I don't want. I would only like to DROP and CREATE database objects and later populate tables with the scripted data.
Has anyone faced this problem and have you found a solution?
After fiddling around with the options for longer, I managed to find the right parameter and work-around to solve my problem.
The exact code that I ran is:
set
timevar=%date:~4,2%%date:~7,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%
mssql-scripter --server 10.100.8.8 -d Dev_db -f .\%timevar%.sql --schema-and-data
--script-drop-create --target-server-version 2016 --target-server-edition Standard --check-for-existence --constraint-names --collation -U ScriptingUser -P 1234 --exclude-use-database --include-objects "dbo." --display-progress
The key change I added the --include-objects parameter, with a twist. The way I changed by scripts is by adding code snippet:
--include-objects "dbo."
This tells mssql-scripter to only script out objects that contain the "dbo." keyword(substring) in the fully qualified name.
Also I remove this parameter from my initial command:
--include-dependencies
since I script out everything in my database under the dbo schema.
This scripts out:
all of the objects in my database
it includes a IF EXISTS check
it issues a DROP query to drop the existing
it issues a CREATE query to create the new one
it issues multiple INSERT statements to also populate the database with data

Postgres backup not copying contents

I am working on PostgreSQL database and we have a test server which needs to have the same data set as the production one. For this, I plan to start a daily CRON job in linux and copy the production database along with its contents like tables, rows, columns, sequences.
I checked how to copy databases from one to another, and I used the pg_dump command as I will write it below, but it only copied the database tables, sequences, but not the contents.
What should I do to copy the contents?
pg_dump -C databaseName | ssh -C username#removeHost.com "psql databaseName"
Edit
So, What I did was I deleted the database which was on test server,
created a new empty database and then used the command above, and it
worked. So I guess I need to delete the database then only it will
overwrite it.
What should I do to circumvent this behaviour and do a force update
of the database, or delete the test server database even if it is use
and create a new empty database.
Have you tried to use pg_restore instead of psql ? pg_restore has special arguments for your case: -c -C.
Details here:http://www.postgresql.org/docs/current/static/app-pgrestore.html
An example of a command to dump/transfer/restore a db:
pg_dump -F c databaseName | ssh -C username#removeHost.com 'pg_restore --clean --create -d postgres'
For this command you need an empty db on target instance to connect to. (postgres in example).
database named with -d is used only to issue the initial DROP DATABASE
and CREATE DATABASE commands. All data is restored into the database
name that appears in the archive.
If you already have a db on target instance:
pg_dump -F c databaseName | ssh -C username#removeHost.com 'pg_restore --clean -d databaseName'
Similar question: Use pg_dump result as input for pg_restore

initlocation not working in postgresql

I am using postgresql 9.2 on a RHEL 6.5 machine and would like to set up a database in a non-standard location as it is filling up the root partition. The instructions tell me to use the initlocation command from the command line but I get the error 'command not found' on using it. I have searched for it in case it is a shell script but cannot find it anywhere.
How do I run this command? thanks for any help.
Right, so you want to leave the existing installation where it is and create a new database in a directory that has more space.
First, a disclaimer, I haven't done exactly this before, but, I have a 9.4b2 installation here on my home computer and I did walk through these steps to verify that is seems to work :-)
#Mike Sherrill 'Cat Recall' has a pretty good write up on database storage (Where does PostgreSQL store the database?). I don't think it specifically answers your question, but it does have all of the information necessary to figure it out.
In your case, you will want to use tablespaces. First, create a new tablespace in the postgres installation. I will use my machine as an example. The first thing I would do is identify where the new database will reside. A directory name, and it has to be empty. For me, I'll create an empty directory in my home directory (I have to do it as the root user) with the shell commands:
mkdir /home/gfausak/pg_tablespace
chown postgres /home/gfausak/pg_tablespace
chgrp postgres /home/gfausak/pg_tablespace
chmod 700 /home/gfausak/pg_tablespace
Then I log in to psql as the postgres user. Like:
psql -Upostgres
Your incantation might be different. When you get to the prompt just verify where your current database(s) are via:
postgres=# show data_directory;
data_directory
-----------------------
/var/local/pgsql/data
(1 row)
Now, create your new tablespace.
postgres=# create tablespace newspace location '/home/gfausak/pg_tablespace';
CREATE TABLESPACE
The docs for this command can be views here: http://www.postgresql.org/docs/9.2/static/manage-ag-tablespaces.html
Once created it can be used for a variety of different objects. In your case, createdb, can be done from the command line or from the postgres psql prompt. I'll create a test database in that tablespace:
postgres=# create database newdb with tablespace = newspace;
CREATE DATABASE
Just for grins, open it and create a table:
postgres=# \c newdb
You are now connected to database "newdb" as user "postgres".
newdb=# create table testme (t text);
CREATE TABLE
newdb=#
If I go back to my home directory and take a peek in that newly created directory I'll see some postgres files:
ls /home/gfausak/pg_tablespace
PG_9.4_201407151
Anything you create in the newdb database will go in to the new directory unless you override the creation of it. There is a default_tablespace variable. As long as it is blank, the default is to put objects in the current database. Same goes for another setting temp_tablespace. The default for both of these is blank, meaning the current database.
You can even create objects in your old database in the new tablespace by specifying the tablespace when you create the table.
-g
Try this.
vi /etc/init.d/postgresql-XX
Change this variable with:
PGDATA=/var/lib/pgsql/$PGMAJORVERSION/data
PGLOG=/var/lib/pgsql/$PGMAJORVERSION/pgstartup.log
on:
PGDATA=/new_pgdata/pgsql/${PGMAJORVERSION}/data
PGLOG=/new_pgdata/pgsql/${PGMAJORVERSION}/pgstartup.log
than:
mkdir -p /new_pgdata/pgsql/XX/data/
chown -R postgres:postgres /pgdata/pgsql
/etc/init.d/postgresql-XX initdb -D /new_pgdata/pgsql/XX/data/
Where XX is equal to 9.2

Dropping a postgres database in cmdline, still seeing the database when \list

I'm trying to drop my database and create a new one through the command line.
I login using psql postgres and then do a \list, see a list of the two databases i created which i now want to delete. so i tried using a DROP DATABASE databasename;
I don't see any error while executing that statement but when i try to \list again to see if that DB are deleted, i still see that that the DB exists. Can someone please tell me why this could happen? and how to surely delete those DB.
There are a couple caveats to DROP DATABASE:
It can only be executed by the database owner.
It cannot be executed while you or anyone else are connected to the target database.
I generally use the dropdb command-line tool to do this, since it's a wrapper around DROP DATABASE which doesn't require you to explicitly connect first. It still has the caveat that there can't be any users currently connected to the database, but it's generally quicker/easier to use.
I would recommend you try issuing a command like this:
dropdb -h <host> -U <user> -p <port> <name of db to drop>
Similarly, you can use the createdb command-line tool to create a database.
More info on DROP DATABASE: http://www.postgresql.org/docs/current/static/sql-dropdatabase.html
Edit:
Also, it is worth looking in the Postgres log (likely in /var/log/postgresql by default) to see if perhaps there is anything in there that wasn't surfaced in the results.

How to set up a cron job to delete old database records using cPanel?

I set up a CRM, and enabled logging in the CRM. The logs are stored in a specific database called crm and a table called crm_logging. I want any records older than a certain date (say 7 days) to be deleted so that the logs table doesn't become too large.
I have no experience doing cron jobs before, and I'm wondering how I would go about setting up a cron job to do this using cPanel's built in Cron Jobs page?
I've read that it would look something like:
DELETE FROM [table] WHERE [column] < DATE_SUB(NOW(), INTERVAL 7 DAY);
But I'm not sure whether I need to include something like this before I put that bit of code in:
mysql -u <username> -p<password> -h <name-of-mysql-server> <databasename> -e "<YOUR-QUERY-HERE>"
Any help you could give is greatly appreciated.
mysql -u <username> -p<username_password> -h <name-of-mysql-server> <databasename> -e "<YOUR-QUERY-HERE>"
Replace correct:
for user with permission to access to Host MySQL for his location
password of this user
You see, ypou must use -p or --password=

Resources