I wrote a script that I want to execute every minute
I wanna save my database every minute.
My script:
#!/bin/sh
DB_USER="root"
DB_PASS="root"
DB_HOST="localhost"
OUTDIR=`date +%Y-%m-%d/%H:%M:%S`
mkdir -p /Users/mess/backup_DB/$OUTDIR
DATABASES=`MYSQL_PWD=$DB_PASS mysql -u $DB_USER -e "SHOW DATABASES;" | tr -d "| " | grep -v -e Database -e _schema -e mysql`
for DB_NAME in $DATABASES; do
MYSQL_PWD=$DB_PASS mysqldump -u $DB_USER --single-transaction --skip-lock-tables $DB_NAME -h $DB_HOST > /Users/mess/backup_DB/$OUTDIR/$DB_NAME.sql
done
for DB_NAME in $DATABASES; do
gzip /Users/mess/backup_DB/$OUTDIR/$DB_NAME.sql
done
When I execute my script it’s work but when I use cron to execute it automatically it’s doesn’t work, crontab create the folder but not the file (database.sql.zip)
Here my cron :
PATH=/bin:/usr/bin:/usr/sbin:/Users/mess/scripts/bin
* * * * * backup
Related
My bash script queries a mysql database 3 times and redirects the standard out of each query to a file (3 different files in total with different columns structure ).
I want it to ask for the mysql password as it's important for me not to have the password in the script or on disk.
How can I include all queries and stdout redirection in the same mysql session in order to avoid asking for the password 3 times?
This is what I have now:
#!/bin/bash
mysql -h database.com -u user -p -e "USE database; mysql query1" > file1
mysql -h database.com -u user -p -e "USE database; mysql query2" > file2
mysql -h database.com -u user -p -e "USE database; mysql query3" > file3
You could use tee and notee commands and write a single query file, say queries.sql and invoke it in a single shot:
use database
tee file1
query1
notee
tee file2
query2
notee
tee file3
query3
notee
Then invoke it:
mysql -h database.com -u user -p -e "source queries.sql" > /dev/null
Related:
What is the equivalent of the spool command in mysql
How can I run an SQL script in MySQL?
You could use bash to prompt for the password, and then supply it to each of the mysql commands:
#!/bin/bash
echo "enter the password for MySQL:"
read -s PASSWD
mysql -h database.com -u user -p$PASSWD -e "USE database; mysql query1" > file1
mysql -h database.com -u user -p$PASSWD -e "USE database; mysql query2" > file2
mysql -h database.com -u user -p$PASSWD -e "USE database; mysql query3" > file3
Here is a POSIX-compliant version of silent prompting for non-bash shells:
stty -echo
printf "enter the password for MySQL:"
read PASSWD
stty echo
printf "\n"
I'm trying to make a cronjob script that will take entries in a MS Access database (*.mdb) and update the sql database on the server. The script I found drops the tables and replaces them with the ones in the mdb file.
#!/bin/bash
TABLES=$(mdb-tables -1 $1)
MUSER="cloyd"
MPASS="******"
MDB="$2"
MYSQL=$(which mysql)
for t in $TABLES
do
$MYSQL -u $MUSER -p$MPASS $MDB -e "DROP TABLE IF EXISTS $t"
done
mdb-schema $1 mysql | $MYSQL -u $MUSER -p$MPASS $MDB
for t in $TABLES
do
mdb-export -D '%Y-%m-%d %H:%M:%S' -I mysql $1 $t | $MYSQL -u $MUSER -p$MPASS $MDB
done
I tried adding unique keys to the table on the IDs and remove the drop table part but it just wasn't updating the table.
I have figured it out. I am using mysqlimport instead and it seems to be working much better.
#!/bin/bash
TABLES=$(mdb-tables -1 $1)
MUSER="cloyd"
MPASS="************"
MDB="$2"
MYSQL=$(which mysql)
for t in $TABLES
do
mdb-export -D '%Y-%m-%d %H:%M:%S' -Q $1 $t > $t.csv
mysqlimport --fields-terminated-by=, --silent --local --replace --ignore-lines=1 --user=cloyd --password=**** timesystem $t.csv
done
$ mysql -h foobar.com -u user -p salesdb -e "SET #arg='date';" < "myscript.sql"
did not work. I wonder if using -e and < "myscript.sql" at the same time is allowed at all?
$ echo "SET #arg='date';" > input_script.sql
$ cat myscript.sql >> input_script.sql
$ mysql -h foobar.com -u user -p salesdb < input_script.sql
probably won't work at the same time, but you don't need that...
(echo "SET #arg='date';"; cat "myscript.sql") |
mysql -h foobar.com -u user -p salesdb
Found on http://www.cyberciti.biz/faq/using-mysql-in-shell-scripts/
#!/bin/bash
mysql dbnane<<EOFMYSQL
SELECT * from table;
EOFMYSQL
How can I dump each table in database in separate file with that table name?
You may want to check out the shell script suggested in the following article:
How do I dump all tables in a database into separate files? by Sunny Walia
Script:
#!/bin/bash
db=$1
if [ "$db" = "" ]; then
echo "Usage: $0 db_name"
exit 1
fi
mkdir $$
cd $$
clear
for table in `mysql $db -e 'show tables' | egrep -v 'Tables_in_' `; do
echo "Dumping $table"
mysqldump --opt -Q $db $table > $table.sql
done
if [ "$table" = "" ]; then
echo "No tables found in db: $db"
fi
Here is a Linux command line to backup all tables in YOURDATABASENAME to separate files in a specific path:
You will have to replace YOURDATABASENAME and YOURPATH with appropriate values.
for I in $(mysql --database=YOURDATABASENAME -e 'show tables' -s --skip-column-names); do mysqldump YOURDATABASENAME $I | gzip > "/YOURPATH/YOURDATABASENAME/$I.sql.gz"; done
I use this as a scheduled cron job to backup all tables daily.
NOTE: if you are using this from a Linux command line, you will have to have to add a user with global privileges to the database for YOURUSERNAME#localhost with no password. Otherwise, you will have to add user and password options to the script as follows, but this will require a password for each table!
for I in $(mysql -u MYSQLUSERNAME -p --database=YOURDATABASENAME -e 'show tables' -s --skip-column-names); do mysqldump -u MYSQLUSERNAME -p YOURDATABASENAME $I | gzip > "/YOURPATH/YOURDATABASENAME/$I.sql.gz"; done
How can I use mysqldump to dump certain tables that start with common prefix?
Hehe, this is kind of a hack, but it works (using bash):
mysqldump -u USER -p DATABASE $(mysql -u USER -p -D DATABASE -Bse "show tables like 'PREFIX%'") > /tmp/DATABASE.out
Change the ALLCAPS words as needed.
You can create backup.sh script:
BACKUP_DIR="/path/to/backups"
DB_HOST="domain.com"
DB_USER="user"
DB_PASS="pass"
PREFIX="phpbb"
TMP_LIST = mysql --host=$DB_HOST --user=$DB_USER --password=$DB_PASS -e "show databases;" | grep $PREFIX
# just get to database name column (may be $1, don't remember)
TMP_LIST = cat $TMP_LIST | awk '{print $2}'
# Getting dbs
mkdir $BACKUP_DIR/tmp
for db in $TMP_LIST ; do
mysqldump --host=$DB_HOST --user=$DB_USER --password=$DB_PASS --opt $db > $BACKUP_DIR/tmp/$db.sql
zip -mj $BACKUP_DIR/tmp/$db.sql.zip $BACKUP_DIR/tmp/$db.sql 2>&1
done
Hope it help.