I can backup all my db in separate files using the following script:
#!/bin/bash
MYSQL_USER="USER"
MYSQL_PASS="PASSWORD"
if [ -z "$1" ]
then
echo "Dumping all DB ..."
for I in $(mysql -u $MYSQL_USER --password=$MYSQL_PASS -e 'show databases' -s --skip-column-names);
do
echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > "$I.sql"
mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $I >> "$I.sql";
echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;commit;" >> "$I.sql"
gzip "$I.sql"
done
echo "END."
else
echo "Dumping $1 ..."
echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > "$1.sql"
mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $1 >> "$1.sql";
echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;commit;" >> "$1.sql"
gzip "$1.sql"
fi
I'm looking for the reverse command:
create a DB with the same file name
uncompress/import the sql.gz backup
You can use this script:
for f in *.sql.gz; do
db="${f%%.*}"
echo "creating database $db"
mysql -h localhost -u root -p mysql --password=passwd <<< "create database $db"
echo "restoring database $db"
gunzip "$f"
mysql -h localhost -u root -ppasswd "$db" < "$db.sql"
done
Related
I have a script that search in a DB for a single column and store them in an array, then I use that column values in another query to get multiples columns, and store them in a variable.
Im trying to echo the variable message but when I run the script it doesnt echo anything.
#!/bin/bash
mapfile result < <(mysql -ugroot -p<pass> -D <db> -h <host-ip> -P 3306 -s -N -e "SELECT <query> ;")
for row in "${result[#]}";do
message=`mysql -ugroot -p<pass> -D <db> -h <host-ip> -P 3306 -s -N -e "SELECT <query> AND tg2.nombre = '${row}';"`
echo $message
done
Unless you explicitly need the result array I think you can use:
for row `mysql -ugroot -p<pass> -D <db> -h <host-ip> -P 3306 -s -N -e "SELECT <query> ;"` ; do
message=`mysql -ugroot -p<pass> -D <db> -h <host-ip> -P 3306 -s -N -e "SELECT <query> AND tg2.nombre = '${row}';"`
echo $message
done
BR
I am using MySQL on centos7. I have 50 databases Like database1, database2...., database50.
How can I set a cronjob for take a dump every day of all database same as database name [ Like database1.sql, database2.sql .... database50.sql ] using single command or script.
Please provide some adequate solution that will be appreciated.
Thanks.
Convert the current date to an integer number of days since some starting date.
Take that modulo 50. This gives you 0 .. 49.
Add 1 and concatenate. Now you have database1 .. database50. Put that in the shell variable db
mysqldump ... $db >$db.sql
I am using this script
#! /bin/bash
# MySQL database backup (databases in separate files) with daily, weekly and monthly rotation
# Sebastian Flippence (http://seb.flippence.net) originally based on code from: Ameir Abdeldayem (http://www.ameir.net)
# You are free to modify and distribute this code,
# so long as you keep the authors name and URL in it.
# Modified by IVO GELOV
# How many backups do you want to keep?
MAX_DAYS=5
# Date format that is appended to filename
DATE=`date +'%Y-%m-%d'`
DATSTR=`date '+%Y%m%d' -d "-$MAX_DAYS days"`
# MySQL server's name
SERVER=""
# Directory to backup to
BACKDIR="/var/db_arhiv/mysql"
#----------------------MySQL Settings--------------------#
# MySQL server's hostname or IP address
HOST="localhost"
# MySQL username
USER="user"
# MySQL password
PASS="password"
# List all of the MySQL databases that you want to backup,
# each separated by a space. Or set the option below to backup all database
DBS="db1 db2"
# Set to 'y' if you want to backup all your databases. This will override
# the database selection above.
DUMPALL="y"
# Custom path to system commands (enable these if you want use a different
# location for PHP and MySQL or if you are having problems running this script)
MYSQL="/usr/local/mysql/bin/mysql"
MYSQLDUMP="/usr/local/mysql/bin/mysqldump"
function checkMysqlUp() {
$MYSQL -N -h $HOST --user=$USER --password=$PASS -e status > /dev/null
}
trap checkMysqlUp 0
function error() {
local PARENT_LINENO="$1"
local MESSAGE="$2"
local CODE="${3:-1}"
if [[ -n "$MESSAGE" ]] ; then
echo "Error on or near line ${PARENT_LINENO}: ${MESSAGE}; exiting with status ${CODE}"
else
echo "Error on or near line ${PARENT_LINENO}; exiting with status ${CODE}"
fi
exit "${CODE}"
}
trap 'error ${LINENO}' ERR
# Check backup directory exists
# if not, create it
if [ ! -e "$BACKDIR/$DATE" ]; then
mkdir -p "$BACKDIR/$DATE"
echo "Created backup directory (${BACKDIR}/${DATE})"
fi
if [ $DUMPALL = "y" ]; then
echo "Creating list of databases on: ${HOST}..."
$MYSQL -N -h $HOST --user=$USER --password=$PASS -e "show databases;" > ${BACKDIR}/dbs_on_${SERVER}.txt
# redefine list of databases to be backed up
DBS=`sed -e ':a;N;$!ba;s/\n/ /g' -e 's/Database //g' ${BACKDIR}/dbs_on_${SERVER}.txt`
fi
echo "Backing up MySQL databases..."
#cd ${LATEST}
for database in $DBS; do
if [ ${database} = "information_schema" ] || [ ${database} = "performance_schema" ] || [ ${database} = "pinba" ]
then
continue
fi
echo "${database}..."
$MYSQLDUMP --host=$HOST --user=$USER --password=$PASS --default-character-set=utf8 --routines --triggers --lock-tables --disable-keys --force --single-transaction --allow-keywords --dump-date $database > ${BACKDIR}/${DATE}/${SERVER}$database.sql
done
if [ $DUMPALL = "y" ]; then
rm -f ${BACKDIR}/dbs_on_${SERVER}.txt
fi
# dump privileges
$MYSQL -N -h $HOST --user=$USER --password=$PASS --skip-column-names -A -e "SELECT CONCAT('SHOW GRANTS FOR ''',user,'''#''',host,''';') FROM mysql.user" | $MYSQL -N -h $HOST --user=$USER --password=$PASS --skip-column-names -A > ${BACKDIR}/${DATE}/${SERVER}_grants.sql
# delete older files
for x in `find ${BACKDIR}/20* -type d`
do
xd=`basename "${x//-/}"`
if [[ $xd < $DATSTR ]]
then
rm -rf "$x"
fi
done
echo "MySQL backup is complete"
I want to dockerize mysql database. I have .sh script for preparing environment, creating database, user and other things. Mysql dump restore command not working in my .sh script, but working good if i open container shell and exec command in it. I want working command in my .sh script.
Whats wrong in my script ?
Dockerfile:
FROM alpine:latest
WORKDIR /app
COPY startup.sh /startup.sh
COPY backup.sql /app/
RUN apk add --update mysql mysql-client && rm -f /var/cache/apk/*
COPY my.cnf /etc/mysql/my.cnf
EXPOSE 3306
startup.sh :
#!/bin/sh
if [ -d /app/mysql ]; then
echo "[i] MySQL directory already present, skipping creation"
else
echo "[i] MySQL data directory not found, creating initial DBs"
mysql_install_db --user=root > /dev/null
if [ "$MYSQL_ROOT_PASSWORD" = "" ]; then
MYSQL_ROOT_PASSWORD=111111
echo "[i] MySQL root Password: $MYSQL_ROOT_PASSWORD"
fi
MYSQL_DATABASE=myDb
if [ ! -d "/run/mysqld" ]; then
mkdir -p /run/mysqld
fi
tfile=`mktemp`
if [ ! -f "$tfile" ]; then
return 1
fi
cat << EOF > $tfile
EOF
if [ "$MYSQL_DATABASE" != "" ]; then
echo "[i] Creating database: $MYSQL_DATABASE"
echo "FLUSH PRIVILEGES;" >> $tfile
echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;" >> $tfile
echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;"
echo "CREATE USER 'myuser'#'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile
echo "CREATE USER 'myuser'#'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';"
echo "CREATE USER 'myuser'#'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile
echo "CREATE USER 'myuser'#'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';"
echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'#'localhost' WITH GRANT OPTION;" >> $tfile
echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'#'localhost' WITH GRANT OPTION;"
echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'#'%' WITH GRANT OPTION;" >> $tfile
echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'#'%' WITH GRANT OPTION;"
echo "FLUSH PRIVILEGES;" >> $tfile
fi
/usr/bin/mysqld --user=root --bootstrap --verbose=0 < $tfile
rm -f $tfile
fi
exec /usr/bin/mysqld --user=root --console
mysql -u root myDb < backup.sql # THIS LINE NOT WORKING! WHY ?
You're not actually executing your script when the container starts. I'm not certain what you're trying to accomplish with this, and I'm not supposed to ask for clarification, so I'll just answer as best I can and hope this helps.
Try something like:
Dockerfile
FROM alpine:latest
WORKDIR /app
COPY startup.sh /startup.sh
COPY backup.sql /app/
RUN apk add --update mysql mysql-client && rm -f /var/cache/apk/*
COPY my.cnf /etc/mysql/my.cnf
EXPOSE 3306
ENTRYPOINT ["/startup.sh"]
CMD /bin/bash -c "/usr/bin/mysqld --user=root --console && mysql -u root myDb < backup.sql"
Then, you'd modify your startup script like this:
startup.sh
#!/bin/sh
if [ -d /app/mysql ]; then
echo "[i] MySQL directory already present, skipping creation"
else
echo "[i] MySQL data directory not found, creating initial DBs"
mysql_install_db --user=root > /dev/null
if [ "$MYSQL_ROOT_PASSWORD" = "" ]; then
MYSQL_ROOT_PASSWORD=111111
echo "[i] MySQL root Password: $MYSQL_ROOT_PASSWORD"
fi
MYSQL_DATABASE=myDb
if [ ! -d "/run/mysqld" ]; then
mkdir -p /run/mysqld
fi
tfile=`mktemp`
if [ ! -f "$tfile" ]; then
return 1
fi
cat << EOF > $tfile
EOF
if [ "$MYSQL_DATABASE" != "" ]; then
echo "[i] Creating database: $MYSQL_DATABASE"
echo "FLUSH PRIVILEGES;" >> $tfile
echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;" >> $tfile
echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;"
echo "CREATE USER 'myuser'#'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile
echo "CREATE USER 'myuser'#'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';"
echo "CREATE USER 'myuser'#'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile
echo "CREATE USER 'myuser'#'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';"
echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'#'localhost' WITH GRANT OPTION;" >> $tfile
echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'#'localhost' WITH GRANT OPTION;"
echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'#'%' WITH GRANT OPTION;" >> $tfile
echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'#'%' WITH GRANT OPTION;"
echo "FLUSH PRIVILEGES;" >> $tfile
fi
/usr/bin/mysqld --user=root --bootstrap --verbose=0 < $tfile
rm -f $tfile
fi
exec "$#"
Take a look at the way the Dockerfile and docker-entrypoint.sh are set up in the official mysql Docker image repo.
Try the helicopterizer for Backup and Restore for Docker Container.
https://github.com/frekele/helicopterizer
.
I need to make a script that will update Zabbix on Ubuntu. It needs to update everything like the log files and so on. But I don't know how to make a script that will execute this. I've been trying to look up information on this subject but can't find anything.
Following is an old script that is used to install Zabbix-server automatically, but only for the first time, I think you must only edit thing which is required in the application
#!/bin/sh
#
# zabbix 2.0.2 install with postgresql and jabber support
#
#
# software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND
#
# written by:
# Stefan Krueger
# tested on Debian Squeeze/Wheezy & Ubuntu 10.04 LTS/12.04 LTS (use sudo su -)
#
(
dbpw=$(dd if=/dev/urandom bs=1 count=6 2>/dev/null | openssl base64)
zbxver=zabbix-2.0.2
ipadd=$(env LC_ALL=C /sbin/ifconfig eth0 | sed -n '/addr:/s/ [^r]*..//gp')
date
# update system
echo '###############'
echo 'update system'
echo '###############'
apt-get update
# install requirements
echo '###############'
echo 'install requirements'
echo '###############'
sleep 1
apt-get install -y postgresql build-essential fping apache2 php5 php5-pgsql php5-gd libsnmp-dev libcurl4-openssl-dev libapache2-mod-php5 libiksemel-dev libpq-dev libssh2-1-dev libopenipmi-dev
#DB setup
echo '###############'
echo 'database setup '
echo '###############'
sleep 1
adduser zabbix --no-create-home --system --group --disabled-password --shell /bin/false --quiet && echo 'User zabbix created' || echo 'User zabbix could not be created'
su postgres -c "echo \"create user zabbix with password '${dbpw}' createdb;\" | psql" && echo "database user zabbix created" || echo "database user zabbix could not be created"
su postgres -c "echo \"create database zabbix with owner=zabbix;\" | psql" && echo "database zabbix created" || echo "database zabbix could not be created"
sleep 2
# zabbix installation
echo '############################################################################'
echo ''
echo $zbxver installation
echo ''
echo '############################################################################'
cd /tmp/
mkdir -p /tmp/install
cd /tmp/install
wget http://prdownloads.sourceforge.net/zabbix/${zbxver}.tar.gz
tar -zxvf ${zbxver}.tar.gz
chmod -R 777 /tmp/install/*
su zabbix -s /bin/bash -c "psql -d zabbix -f /tmp/install/${zbxver}/postgresql/schema.sql" && echo "create schema success" || echo "create schema failed"
su zabbix -s /bin/bash -c "psql -d zabbix -f /tmp/install/${zbxver}/postgresql/images.sql" && echo "create schema success" || echo "create schema failed"
su zabbix -s /bin/bash -c "psql -d zabbix -f /tmp/install/${zbxver}/postgresql/data.sql" && echo "create schema success" || echo "create schema failed"
cd /tmp/install/${zbxver}
chmod +x /tmp/install/${zbxver}/configure
chmod +x ./configure
/tmp/install/${zbxver}/configure --enable-server --with-postgresql --with-net-snmp --with-libcurl --with-openipmi --with-jabber --with-ssh2 --enable-agent --prefix=/usr --mandir=\${prefix}/share/man --infodir=\${prefix}/share/info --sysconfdir=/etc/zabbix || exit
make install
#install frontend
echo '############################################################################'
echo ''
echo '$zbxver installation'
echo ' FRONTEND installation'
echo ''
echo '############################################################################'
sleep 1
sed -i.backup -e "s/post_max_size = 8M/post_max_size = 32M/g" -e "s/max_execution_time = 30/max_execution_time = 600/g" -e "s/max_input_time = 60/max_input_time = 600/g" -e '/date.timezon/a\date.timezone = "Europe/Berlin"' /etc/php5/apache2/php.ini
cd /tmp/install/${zbxver}/frontends/php
mkdir -p /var/www/zabbix
cp -a . /var/www/zabbix
chown www-data:www-data -R /var/www/zabbix
cat <<EOF > /etc/apache2/sites-available/zabbix
<VirtualHost /zabbix>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/zabbix
<Directory />
Options FollowSymLinks Indexes MultiViews
AllowOverride None
</Directory>
</VirtualHost>
EOF
echo '############################################################################'
echo ''
echo '$zbxver installation'
echo ' services installation agent & server'
echo ''
echo '############################################################################'
sleep 1
mkdir -p /var/log/zabbix && chown zabbix /var/log/zabbix && chmod 760 /var/log/zabbix
mkdir -p /var/run/zabbix && chown zabbix /var/run/zabbix && chmod 760 /var/run/zabbix
#cp /tmp/install/${zbxver}/misc/conf/zabbix_server.conf /etc/zabbix
#cp /tmp/install/${zbxver}/misc/conf/zabbix_agentd.conf /etc/zabbix
sed -i.backup -e "s|DBUser=root|DBUser=zabbix|g" -e "s|# DBPassword=|DBPassword=${dbpw}|g" -e "s|/tmp/zabbix_server.log|/var/log/zabbix/zabbix_server.log|g" -e "s|# PidFile=/tmp/zabbix_server.pid|PidFile=/var/run/zabbix/zabbix_server.pid|g" -e "s|# HousekeepingFrequency=1|HousekeepingFrequency=24|" /etc/zabbix/zabbix_server.conf
sed -i.backup -e "s|/tmp/zabbix_agentd.log|/var/log/zabbix/zabbix_agentd.log|g" -e "s|# PidFile=/tmp/zabbix_agentd.pid|PidFile=/var/run/zabbix/zabbix_agentd.pid|g" /etc/zabbix/zabbix_agentd.conf
chown -R zabbix:zabbix /etc/zabbix
cp /tmp/install/${zbxver}/misc/init.d/debian/zabbix* /etc/init.d/
sed -i.backup -e "s|/usr/local/sbin/|/usr/sbin/|" -e "s|PID=/tmp/|PID=/var/run/zabbix/|" /etc/init.d/zabbix-server
sed -i.backup -e "s|/usr/local/sbin/|/usr/sbin/|" -e "s|PID=/tmp/|PID=/var/run/zabbix/|" /etc/init.d/zabbix-agent
chmod 775 /etc/init.d/zabbix-server
chmod 775 /etc/init.d/zabbix-agent
update-rc.d zabbix-server defaults 90
update-rc.d zabbix-agent defaults 99
/etc/init.d/zabbix-agent start
/etc/init.d/zabbix-server start
/etc/init.d/apache2 restart
echo ""
echo ""
echo ""
if [ -e /var/run/zabbix/zabbix_server.pid ]; then echo " zabbix-server started succesfully"
else echo " !! zabbix-server dont start"
fi
if [ -e /var/run/zabbix/zabbix_agentd.pid ]; then echo " zabbix-agentd started succsfully"
else echo " !! zabbix-agentd dont start"
fi
echo " look at the zabbix_install.log"
echo ""
echo " please configure you postgresql.conf"
echo " the database password for zabbix is: ${dbpw}"
echo ""
echo " now you can configure the zabbix-frontend http://${ipadd}/zabbix"
) | tee zabbix_install.log
I created a little script which must change engine for tables:
#!/bin/bash
echo "use test_1;" > query.sql
get_list () {
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}' echo "alter table {} engine=innodb;" >> query.sql
}
get_list ;
mysql -u teamcity -ppassword < query.sql
But - how can I avoid use query.sql file? I make few attempts with "Here document" - but can't solve it...
For example - trying this:
#!/bin/bash
get_list () {
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}' echo "alter table {} engine=innodb;"
}
a="$(get_list)"
b="use test_1;"
c="$b $a"
mysql -u teamcity -ppassword <<< $c
But it is not working...
Put everything in a function and pipe that:
#!/bin/bash
get_list () {
echo "use test_1;"
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}' echo "alter table {} engine=innodb;"
}
get_list | mysql -u teamcity -ppassword