mysqldump - Export structure only without autoincrement - export

I have a MySQL database and I am trying to find a way to export its structure only, without the auto increment values. mysqldump --no-data would almost do the job, but it keeps the auto_increment values. Is there any way to do it without using PHPMyAdmin (that I know it can do it)?

You can do this :
mysqldump -u root -p -h <db-host> --opt <db-name> -d --single-transaction | sed 's/ AUTO_INCREMENT=[0-9]*\b//' > <filename>.sql
As mentioned by others, If you want sed to works properly, add the g (for global replacement) parameter like this :
mysqldump -u root -p -h <db-host> --opt <db-name> -d --single-transaction | sed 's/ AUTO_INCREMENT=[0-9]*\b//g' > <filename>.sql
(this only works if you have GUI Tools installed: mysqldump --skip-auto-increment)
New UPDATE thanks to comments.
The \b is useless and sometimes will break the command. See this SO topic for explanations.
So the optimized answer would be :
mysqldump -u root -p -h <db-host> --opt <db-name> -d --single-transaction | sed 's/ AUTO_INCREMENT=[0-9]*//g' > <filename>.sql

JoDev's answer worked perfectly for me with a small adjustment to the sed regular expression:
mysqldump -d -h localhost -u<user> -p<password> <databaseName> | sed 's/ AUTO_INCREMENT=[0-9]*//g' > databaseStructure.sql

It is --create-options, which is included with --opt, by default, which generates the AUTO_INCREMENT table definitions.
If you only want the base tables,
mysql -hlocalhost -uuser -ppass --skip-column-names --batch \
-e "select table_name from tables where table_type = 'BASE TABLE' and table_schema = 'schemaname'" INFORMATION_SCHEMA \
| xargs mysqldump -hlocalhost -uuser -ppass \
--no-data --skip-triggers --skip-opt --no-create-db \
schemaname
If you want views, triggers and routines too,
mysqldump -hlocalhost -uuser -ppass \
--skip-opt --events --routines --no-data \
schemaname

Thanks to this post, I was able to answer my question:
How can I do version control on my db?
Then I just created this script: db_bkp.sh
#!/bin/sh
filename="db_structure.sql"
backupfolder="/var/www/"
fpath="$backupfolder/$filename"
usr="DBUSER"
pass="DBPASS"
db="DBNAME"
mysqldump --user=$usr --password=$pass --no-data $db | sed 's/ AUTO_INCREMENT=[0-9]*//g' > "$fpath"
Then I added this to crontab:
30 5 * * * sh /home/scripts/db_bkp.sh
Then in my repo I added the result, db_structure.sql to git and before pushing changes to prod I always check if there's any structural changes I forgot to do on all dbs.

mysqldump -u [USER] -p [PASSWORD] -d --skip-opt --single-transaction [DB_SCHEMA] > [FILE.ESTENSIONE]

Related

Update Local SQL Server with a Bash Script

Question:
What is the correct format to use in my bash script to be able to run the -Q option?
Case: Update local database from S3 every night to run reports on our on-premise server
Code:
#!/bin/bash
#get latest file from S3
BACKUP_MARKETING=`aws s3 ls [some_folder]/[some_subfolder]/ --recursive | sort | tail -n 1 | awk '{print $4}'`
#download the file locally
aws s3 cp s3://[some_folder]/$BACKUP_MARKETING /var/opt/mssql/backup/marketing
#get the file name
BAK_MARKETING=`find [folder]/ -type f -name "*.bak"`
#drop the database to avoid conflicts from not backing it up
/opt/mssql-tools/bin/sqlcmd -S localhost -U [username] -P '[password]' -Q 'DROP DATABASE [db_name]'
#restore the database
/opt/mssql-tools/bin/sqlcmd -S localhost -U [username] -P '[password]' -Q RESTORE DATABASE "[db_name]" FROM DISK = "/var/opt/mssql/backup/$BAK_MARKETING" WITH MOVE "[db_name]" TO "/var/opt/mssql/data/[db_name].MDF", MOVE "[db_name]_log" TO "/var/opt/mssql/data/[db_name].LDF"
Error
Sqlcmd: 'DATABASE" "[db_name]" "FROM" "DISK" "=" "/var/opt/mssql/backup/marketing/[db_name].bak" "WITH" "MOVE" "[db_name]" "TO" "/var/opt/mssql/data/[db_name].MDF," "MOVE" "[db_name]_log" "TO" "/var/opt/mssql/data/[db_name].LDF': Unexpected argument. Enter '-?' for help.
Apparently I had to concatenate my variables on the SQL command. Here is the working version plus I added the REPLACE option to it
/opt/mssql-tools/bin/sqlcmd -S localhost -U [username] -P '[password]' -Q 'RESTORE DATABASE [db_name] FROM DISK = "/var/opt/mssql/backup/'**$BAK_FILE**'" WITH REPLACE, MOVE "[db_name]" TO "/var/opt/mssql/data/[db_name].MDF", MOVE "[db_name]_Log" TO "/var/opt/mssql/data/[db_name].LDF"'
Could you not use the -i Option instead?
I had some problems as well using Q, so i replaced it with -i and placed the code within a .sql file instead.
I ended up with;
SET SQLusername=sa
SET SQLpassword=password
SET SQLserver=dnsnameorIp
SET SQLdatabase=databasename
sqlcmd -U %SQLusername% -P %SQLpassword% -S %SQLserver% -d %SQLdatabase% -i mycode.sql -o outputResult.txt

Retrieve rows in DB corresponding to particular ID using kubectl

I am trying to fetch the no. of rows for a particular ID using kubectl but instead getting some extra data.
Command:
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]"
Actual Output of above command:
Defaulting container name to abc-db.
Use 'kubectl describe pod/abc-db-0 -n cicd' to see all of the containers in this pod.
(0 rows)
Expected Output:
(0 rows)
Could anyone please let me know what I am doing wrong here?
Note:
The first 2 lines always comes when we login to the DB manually but in output I only want (0 rows)
The first two lines are output by kubectl exec because the Pod has multiple containers. It is sort of a warning that it picked the first one, which might not be the one you wanted use.
You can specify the target container in your command (-c containername):
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -c abc-db -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]"
Or you can redirect the standard error with kubectl ... 2>/dev/null (os specific):
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -c -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]" 2>/dev/null

-X option not disable sql commands in sqlcmd

I use sqlcmd to execute large sql file which insert multiple of records into database. So when sqlcmd run it display a error message like "syntax error near 'Ed'..." . I known Ed is sql commands and i found -X will disable that kind of commands but it is not work.
My command like this:
sqlcmd -S "tcp:ip,1433" -U "sa" -P "pass" -d "dbname" -c "GO" -k 1 -f 65001 -i "C:\sql.sql" -X -x
My sql file content:
Go
insert into tablename (title) values (N'title
Ed some more data
some more data
some more data')
Go
insert into tablename (title) values (N'title
Ed some more data
some more data
some more data')
Go
Could you give me some help? please.
Thanks you.
It use -X[1] instead of -X.
sqlcmd -S "tcp:ip,1433" -U "sa" -P "pass" -d "dbname" -c "GO" -k 1 -f 65001 -i "C:\sql.sql" -X[1]

Linux SQL Server query with dynamic data?

I am trying to run a query from a shell script
SELECT count(*) FROM MyTable where sessionid = 123
I can do:
bsqldb -U myname -P mypass -S myserv -i getcount.sql
But that means the sessionid is hardcoded to 123
I need to have the sessionid pulled in from the shell script calling the bsqldb comment
How can I pass the sessionid as a variable?
echo "SELECT ${foo} FROM ${bar}" | bsqldb -U myname -P mypass -S myserv -i -
Watch out for SQL injection though.
Seems the best way was to just create a tmp.sql file from the bash script itself and use that as my input:
echo "SELECT * FROM $TABLE WHERE SessionID = $SESS_ID" > tmp.sql
result=$(bsqldb -U $USER -S $SERV -P $PASS -i tmp.sql -q)
rm -f tmp.sql
Thanks all!

MySQL Dump All Databases and Create (or Recreate) them on Import?

I was wondering how I can get the following file,
mysqldump -h server -u root -p --all-databases > all_dbs.sql
to automatically create any databases that are not yet created, when imported. Also, if a database is already present, it should recreate it (overwriting the old one).
Is this possible? Thanks!
Export: mysqldump -u root -p --all-databases > all_dbs.sql
Import: mysql -u root -p < all_dbs.sql
Export:
mysqldump -uroot -p --all-databases > alldb.sql
Look up the documentation for mysqldump. You may want to use some of the options mentioned in comments:
mysqldump -uroot -p --opt --all-databases > alldb.sql
mysqldump -uroot -p --all-databases --skip-lock-tables> alldb.sql
Import:
mysql -u root -p < alldb.sql
I Just found a new solution:
Create a bash script. It backs up each database into a different file
#!/bin/bash
USER="zend"
PASSWORD=""
#OUTPUT="/Users/rabino/DBs"
#rm "$OUTPUTDIR/*gz" > /dev/null 2>&1
databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
echo "Dumping database: $db"
mysqldump -u $USER -p$PASSWORD --databases $db > `date +%Y%m%d`.$db.sql
# gzip $OUTPUT/`date +%Y%m%d`.$db.sql
fi
done
do not use "mysql" command to export data. Please use "mysqldump" instead.
I have to administrate a server that saves only:
\n
Exiting...
after executing "mysql --user=username --password=passord > somefile.sql"
/applications/MAMP/library/bin/mysqldump -u root -p --all-databases > all_dbs.sql

Resources