Create mySQL database through ssh in a bash script - mysql

I want to create a database on a remote server through ssh with a bash script. Something like this
#!/bin/sh
ssh user#example.com 'mysql -h example.com -uroot -pMYPASSWD -e "CREATE DATABASE $1;"'
So that I in terminal can run it like
$ myBashScript nameOfNewDatabase

The following script can remotely execute arbitrary commands
#!/bin/bash
# call mysql on a remote server to execute the given command
# show usage and exit
usage() {
echo "$0 server sql-command" 1>&2
exit 1
}
# check number of arguments
if [ $# -lt 2 ]
then
usage
fi
# get server name and sql command
server="$1"
sql="$2"
# copy command to a temporary file
tmpsql="/tmp/sql$$"
echo "$sql" > $tmpsql
# copy command file to server
scp $tmpsql $server:$tmpsql
# run command remotely with removing command file afterwards
ssh $server "mysql -uroot -pPASSWORD < $tmpsql;rm $tmpsql"
# remove local copy of command file
rm $tmpsql

Related

Can't MariaDB inside docker container

I need to run MariaDB inside existing Docker container.
Building and installation works just fine, but when Docker executes
RUN mysql < init.sql
to load DB schema I get
Can't connect to MySQL server (111 Connection refused)
However when I run the container and execute
docker exec -it silly_allen /bin/bash -c "mysql < init.sql"
it works just fine.
What might be the problem?
Thanks!
EDIT: Here's part of Dockerfile related to DB.
FROM centos:7
WORKDIR /root
...
RUN echo "[mariadb]" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "name = MariaDB" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "baseurl = http://yum.mariadb.org/10.1/centos7-amd64" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "gpgcheck=1" >> /etc/yum.repos.d/MariaDB.repo
RUN rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
RUN yum install -y MariaDB-server MariaDB-client
RUN yum clean all
RUN echo "[mysqld]" > /etc/my.cnf
RUN echo "bind-address=0.0.0.0" >> /etc/my.cnf
RUN /etc/init.d/mysql restart
ADD init.sql /root
RUN mysql < /root/init.sql
...
According to Docker's best practices, you should be having 1 container per process that you want to run.
Also, there's an official mariadb image which allows you to mount a directory as volume, that could contain SQL dumps. These dumps are auto-imported when the container gets created, so this might prove to be handy.
I'd suggest instead of having one very large dockerfile, you break it up in separate services with docker-compose
If you do however want to keep this the way it is, I'd suggest you move the ADD init.sql ... part to the top, and concatenate the server starting up part and the dump import, because each RUN command is a separate layer with Docker. So you'd need something like what's described in the answer of this StackOverflow question:
RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \
sleep 5 && \
mysql -u root -e "CREATE DATABASE mydb" && \
mysql -u root mydb < /root/init.sql
So that the server initializes and the dump gets imported in one layer
From what I can see, you are trying to run mysql < init.sql before starting the database. The error shows that this command requires the database to be running.
To solve this problem, add a startup script into you container containing:
mysqld
mysql < init.sql
And change your Dockerfile CMD to call this script.
This way is right:
# cat Dockerfile
...
ADD init.sql /tmp
ADD initdb.sh /tmp
RUN /tmp/initdb.bash
CMD ["/usr/bin/mysqld_safe --datadir=/var/lib/mysql"]
And the script:
# cat dump/initdb.bash
#!/bin/bash
set -e
set -x
mysqld_safe --datadir='/var/lib/mysql' --user=root &
until mysqladmin ping >/dev/null 2>&1; do
sleep 0.2
done
mysql -e 'create database init;' && \
mysql init < /tmp/init.sql && \
echo "Successfully imported" && exit 0

cannot open connection of database in docker

sql docker:
FROM ubuntu:latest
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get -y install mysql-client mysql-server curl
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
ADD database.sql /var/db/database.sql
ENV user admin
ENV password admin
ENV url file:/var/db/database.sql
ENV right WRITE
ADD ./start-database.sh /usr/local/bin/start-database.sh
RUN chmod +x /usr/local/bin/start-database.sh
EXPOSE 3306
CMD ["/usr/local/bin/start-database.sh"]
--------\------
startdatabase.sh
#!/bin/bash
# This script starts the database server.
echo "Creating user $user for databases loaded from $url"
# Import database if provided via 'docker run --env url="http:/ex.org/db.sql"'
echo "Adding data into MySQL"
/usr/sbin/mysqld &
sleep 5
curl $url -o import.sql
# Fixing some phpmysqladmin export problems
sed -ri.bak 's/-- Database: (.*?)/CREATE DATABASE \1;\nUSE \1;/g' import.sql
# Fixing some mysqldump export problems (when run without --databases switch)
# This is not tested so far
# if grep -q "CREATE DATABASE" import.sql; then :; else sed -ri.bak 's/-- MySQL dump/CREATE DATABASE `database_1`;\nUSE `database_1`;\n-- MySQL dump/g' import.sql; fi
mysql --default-character-set=utf8 < import.sql
rm import.sql
mysqladmin shutdown
echo "finished"
# Now the provided user credentials are added
/usr/sbin/mysqld &
sleep 5
echo "Creating user"
echo "CREATE USER '$user' IDENTIFIED BY '$password'" | mysql --default-character-set=utf8
echo "REVOKE ALL PRIVILEGES ON *.* FROM '$user'#'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
echo "GRANT SELECT ON *.* TO '$user'#'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
#if [ "$right" = "WRITE" ]; then
#echo "adding write access"
echo "GRANT ALL PRIVILEGES ON *.* TO '$user'#'%' WITH GRANT OPTION; FLUSH PRIVILEGES" | mysql --default-character-set=utf8
echo "finished"
#fi
# And we restart the server to go operational
mysqladmin shutdown
echo "Starting MySQL Server"
/usr/sbin/mysqld
When I run this docker file of sql it runs.
But when I link it to application using the command
$ docker run --link mysqldb -d abc // here abc is image name of application
& mysqldb is name of sql db image.
it terminates the server and shows cannot open the connection.
Even though I had provided privileges to it and also provided the ip addr of sql image to application image.
I have seen that my sql server is running properly and it connect to mysql client also.
Please proivide a better solution**

How to execute MySQL command from the host to container running MySQL server?

I have followed the instruction in https://registry.hub.docker.com/_/mysql/ to pull an image and running a container in which it runs a MySQL server.
The container is running in the background and I would like to run some commands.
Which is the best way to connect to the container and execute this command from command line?
Thanks.
You can connect to your mysql container and run your commands using:
docker exec -it mysql bash -l
(Where mysql is the name you gave the container)
Keep in mind that anything you do will not persist to the next time your run a container from the same image.
docker exec -i some_mysql_container mysql -uroot -ppassword <<< "select database();"
To connect to the MySQL database using MySQL command line client.
I connect to the bash into the running MySQL container:
$ docker exec -t -i container_mysql_name /bin/bash
-i is the shortcut for --interactive option. This options is used for keep STDIN open even if not attached
-t is the shortcut for --tty option, used to allocate a pseudo-TTY
I run MySQL client from bash MySQL container:
$ mysql -uroot -proot
-u is shortcut for --user=name option, used to define user for login if not current user.
-p is shortcut for -password[=name] option, used to define password to use when connecting to server. If password is not given it's asked from the tty.
Disco!
In my case the <<< solution did not work.
Instead I used -e.
Example:
docker exec ${CONTAINER_NAME} mysql -u ${USER_NAME} -p${PASSWORD} -e "drop schema test; create schema test;"
For #Abdullah Jibaly solution, after tested in MySQL 5.7, it would only entered into bash terminal prompt, whereby you still need to enter mysql command second time.
In order to directly enter into MySQL command line client after run MySQL container with one line of command, just run the following:
docker exec -it container_mysql_name mysql -u username -p
Its possible with docker run, start a new container just to execute your mysql statement.
This approach helped me to workaround the access denied problem when you try to run a statement with docker exec using localhost to connect to mysql
$ docker run -it --rm mysql mysql -h172.17.0.2 -uroot -pmy-secret-pw -e "show databases;"
I use the following to create a command that will sort out at least a couple of cases with databases outside or inside the container (with -h and -P) and supporting -e:
cat > ~/bin/mysql <<'EOF'
#/bin/bash
MARGS=()
MPORT="3306"
while test $# != 0; do
if [[ $1 == -h ]]; then MHOST=$2; shift;
elif [[ $1 == -h* ]]; then MHOST=${1#"-h"};
elif [[ $1 == -e ]]; then MEXEC=$2; shift;
elif [[ $1 == -e* ]]; then MEXEC=${1#"-e"};
elif [[ $1 == --execute=* ]]; then MEXEC=${1#"--execute="};
elif [[ $1 == -P ]]; then MPORT=$2; shift;
elif [[ $1 == -P* ]]; then MPORT=${1#"-P"};
else MARGS="$MARGS $1"
fi
shift;
done
if [ -z "${MHOST+x}" ]; then
MHOST=localhost
fi
if [ $(docker inspect --format '{{ .State.Status }}' mysql) == "running" ]; then
if [ ! -z "${MHOST+x}" ]; then
if [ "$MHOST" == "localhost" -o "$MHOST" == "127.0.0.1" ]; then
CPORT=$(docker port mysql 3306/tcp)
if [ ${CPORT#"0.0.0.0:"} == $MPORT ]; then
#echo "aiming for container port ($MPORT -> $CPORT)";
MHOST=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql);
else
MHOST=$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p' | head -1);
fi
fi
fi
fi
if [ -z "$MEXEC" ]; then
docker run --link mysql:mysql -i --rm mysql mysql "-h" $MHOST "-P" $MPORT $MARGS
else
docker run --link mysql:mysql -i --rm mysql mysql "-h" $MHOST "-P" $MPORT $MARGS <<< $MEXEC
fi
EOF
chmod +x ~/bin/mysql
i didn't find any of these solutions to be effective for my use case: needing to store the returned data from the SQL to a bash variable.
i ended up with the following syntax when making the call from inside a bash script running on the host computer (outside the docker mysql server), basically use 'echo' to forward the SQL statement to stdin on the docker exec command.
modify the following to specify the mysql container name and proper mysql user and password for your use case:
#!/bin/bash
mysqlCMD="docker exec -i _mysql-container-name_ mysql -uroot -proot "
sqlCMD="select count(*) from DBnames where name = 'sampleDB'"
count=`echo $sqlCMD | $mysqlCMD | grep -v count`
# count variable now contains the result of the SQL statement
for whatever reason, when i used the -e option, and then provided that string within the back-quotes, the interpreter modified the quotation marks resulting in SQL syntax failure.
richard

Linux shell script for database backup

I tried many scripts for database backup but I couldn't make it. I want to backup my database every hour.
I added files to "/etc/cron.hourly/" folder, changed its chmod to 755, but it didn't run.
At least I write my pseudo code.
I would be happy if you can write a script for this operation and tell me what should I do more ?
After adding this script file to /etc/cron.hourly/ folder.
Get current date and create a variable, date=date(d_m_y_H_M_S)
Create a variable for the file name, filename="$date".gz
Get the dump of my database like this mysqldump --user=my_user --password=my_pass --default-character-set=utf8 my_database | gzip > "/var/www/vhosts/system/example.com/httpdocs/backups/$("filename")
Delete all files in the folder /var/www/vhosts/system/example.com/httpdocs/backups/ that are older than 8 days
To the file "/var/www/vhosts/system/example.com/httpdocs/backup_log.txt", this text will be written: Backup is created at $("date")
Change the file owners (chown) from root to "my_user". Because I want to open the backup and log files from the "my_user" FTP account.
I don't want an email after each cron. >/dev/null 2>&1 will be added.
After hours and hours work, I created a solution like the below. I copy paste for other people that can benefit.
First create a script file and give this file executable permission.
# cd /etc/cron.daily/
# touch /etc/cron.daily/dbbackup-daily.sh
# chmod 755 /etc/cron.daily/dbbackup-daily.sh
# vi /etc/cron.daily/dbbackup-daily.sh
Then copy following lines into file with Shift+Ins
#!/bin/sh
now="$(date +'%d_%m_%Y_%H_%M_%S')"
filename="db_backup_$now".gz
backupfolder="/var/www/vhosts/example.com/httpdocs/backups"
fullpathbackupfile="$backupfolder/$filename"
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt
echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
mysqldump --user=mydbuser --password=mypass --default-character-set=utf8 mydatabase | gzip > "$fullpathbackupfile"
echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
chown myuser "$fullpathbackupfile"
chown myuser "$logfile"
echo "file permission changed" >> "$logfile"
find "$backupfolder" -name db_backup_* -mtime +8 -exec rm {} \;
echo "old files deleted" >> "$logfile"
echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "*****************" >> "$logfile"
exit 0
Edit:
If you use InnoDB and backup takes too much time, you can add "single-transaction" argument to prevent locking. So mysqldump line will be like this:
mysqldump --user=mydbuser --password=mypass --default-character-set=utf8
--single-transaction mydatabase | gzip > "$fullpathbackupfile"
Create a script similar to this:
#!/bin/sh -e
location=~/`date +%Y%m%d_%H%M%S`.db
mysqldump -u root --password=<your password> database_name > $location
gzip $location
Then you can edit the crontab of the user that the script is going to run as:
$> crontab -e
And append the entry
01 * * * * ~/script_path.sh
This will make it run on the first minute of every hour every day.
Then you just have to add in your rolls and other functionality and you are good to go.
I got the same issue.
But I manage to write a script.
Hope this would help.
#!/bin/bash
# Database credentials
user="username"
password="password"
host="localhost"
db_name="dbname"
# Other options
backup_path="/DB/DB_Backup"
date=$(date +"%d-%b-%Y")
# Set default file permissions
umask 177
# Dump database into SQL file
mysqldump --user=$user --password=$password --host=$host $db_name >$backup_path/$db_name-$date.sql
# Delete files older than 30 days
find $backup_path/* -mtime +30 -exec rm {} \;
#DB backup log
echo -e "$(date +'%d-%b-%y %r '):ALERT:Database has been Backuped" >>/var/log/DB_Backup.log
#!/bin/sh
#Procedures = For DB Backup
#Scheduled at : Every Day 22:00
v_path=/etc/database_jobs/db_backup
logfile_path=/etc/database_jobs
v_file_name=DB_Production
v_cnt=0
MAILTO="abc#as.in"
touch "$logfile_path/kaka_db_log.log"
#DB Backup
mysqldump -uusername -ppassword -h111.111.111.111 ddbname > $v_path/$v_file_name`date +%Y-%m-%d`.sql
if [ "$?" -eq 0 ]
then
v_cnt=`expr $v_cnt + 1`
mail -s "DB Backup has been done successfully" $MAILTO < $logfile_path/db_log.log
else
mail -s "Alert : kaka DB Backup has been failed" $MAILTO < $logfile_path/db_log.log
exit
fi
Here is my mysql backup script for ubuntu in case it helps someone.
#Mysql back up script
start_time="$(date -u +%s)"
now(){
date +%d-%B-%Y_%H-%M-%S
}
ip(){
/sbin/ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'
}
filename="`now`".zip
backupfolder=/path/to/any/folder
fullpathbackupfile=$backupfolder/$filename
db_user=xxx
db_password=xxx
db_name=xxx
printf "\n\n"
printf "******************************\n"
printf "Started Automatic Mysql Backup\n"
printf "******************************\n"
printf "TIME: `now`\n"
printf "IP_ADDRESS: `ip` \n"
printf "DB_SERVER_NAME: DB-SERVER-1\n"
printf "%sBACKUP_FILE_PATH $fullpathbackupfile\n"
printf "Starting Mysql Dump \n"
mysqldump -u $db_user -p$db_password $db_name| pv | zip > $fullpathbackupfile
end_time="$(date -u +%s)"
elapsed=$(($end_time-$start_time))
printf "%sMysql Dump Completed In $elapsed seconds\n"
printf "******************************\n"
PS: Rememember to install pv and zip in your ubuntu
sudo apt install pv
sudo apt install zip
Here is how I set crontab by using crontab -e in ubuntu to run every 6 hours
0 */6 * * * sh /path/to/shfile/backup-mysql.sh >> /path/to/logs/backup-mysql.log 2>&1
Cool thing is it will create a zip file which is easier to unzip from anywhere
Now, copy the following content in a script file (like: /backup/mysql-backup.sh) and save on your Linux system.
#!/bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%d%b%Y"`
DB_BACKUP_PATH='/backup/dbbackup'
MYSQL_HOST='localhost'
MYSQL_PORT='3306'
MYSQL_USER='root'
MYSQL_PASSWORD='mysecret'
DATABASE_NAME='mydb'
BACKUP_RETAIN_DAYS=30
mkdir -p ${DB_BACKUP_PATH}/${TODAY}
echo "Backup started for database - ${DATABASE_NAME}"
mysqldump -h ${MYSQL_HOST} \
-P ${MYSQL_PORT} \
-u ${MYSQL_USER} \
-p${MYSQL_PASSWORD} \
${DATABASE_NAME} | gzip > ${DB_BACKUP_PATH}/${TODAY}/${DATABASE_NAME}-${TODAY}.sql.gz
if [ $? -eq 0 ]; then
echo "Database backup successfully completed"
else
echo "Error found during backup"
exit 1
fi
##### Remove backups older than {BACKUP_RETAIN_DAYS} days #####
DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
if [ ! -z ${DB_BACKUP_PATH} ]; then
cd ${DB_BACKUP_PATH}
if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
rm -rf ${DBDELDATE}
fi
fi
After creating or downloading script make sure to set execute permission to run properly.
$ chmod +x /backup/mysql-backup.sh
Edit crontab on your system with crontab -e command. Add following settings to enable backup at 3 in the morning.
0 3 * * * root /backup/mysql-backup.sh
Add the following code to your shell script file. Replace dbname, dbuser and dbpass with your database name, username and password respectively.
#!/bin/sh
echo "starting db backup"
day="$(date +"%m-%d-%y")"
db_backup="mydb_${day}.sql"
sudo mysqldump -udbuser -pdbpass --no-tablespaces dbname >/home/${db_backup}
echo " backup complete"
If you want to compress the above backup data, just
Replace with the following code.
db_backup="mydb_${day}.gz"
sudo mysqldump -udbuser -pdbpass --no-tablespaces dbname | gzip -c >/home/${db_backup}
If you want to delete files older than 14 days in a folders,
use following code.
#!/bin/bash
fpath1=/home/ubuntu/mysql/*
fpath2=/home/ubuntu/postgsql/*
file_path=($fpath1 $fpath2)
for i in ${file_path[#]};
do
find $i -type d -mtime +13 -exec rm -Rf {} +
done
#!/bin/bash
# Add your backup dir location, password, mysql location and mysqldump location
DATE=$(date +%d-%m-%Y)
BACKUP_DIR="/var/www/back"
MYSQL_USER="root"
MYSQL_PASSWORD=""
MYSQL='/usr/bin/mysql'
MYSQLDUMP='/usr/bin/mysqldump'
DB='demo'
#to empty the backup directory and delete all previous backups
rm -r $BACKUP_DIR/*
mysqldump -u root -p'' demo | gzip -9 > $BACKUP_DIR/demo$date_format.sql.$DATE.gz
#changing permissions of directory
chmod -R 777 $BACKUP_DIR
You might consider this Open Source tool, matiri, https://github.com/AAFC-MBB/matiri which is a concurrent mysql backup script with metadata in Sqlite3. Features:
Multi-Server: Multiple MySQL servers are supported whether they are co-located on the same or separate physical servers.
Parallel: Each database on the server to be backed up is done separately, in parallel (concurrency settable: default: 3)
Compressed: Each database backup compressed
Checksummed: SHA256 of each compressed backup file stored and the archive of all files
Archived: All database backups tar'ed together into single file
Recorded: Backup information stored in Sqlite3 database
Full disclosure: original matiri author.
As a DBA, You must schedule the backup of MySQL Database in case of any issues so that you can recover your databases from the current backup.
Here, we are using mysqldump to take the backup of mysql databases and the same you can put into the script.
[orahow#oradbdb DB_Backup]$ cat .backup_script.sh
#!/bin/bash
# Database credentials
user="root"
password="1Loginxx"
db_name="orahowdb"
v_cnt=0
logfile_path=/DB_Backup
touch "$logfile_path/orahowdb_backup.log"
# Other options
backup_path="/DB_Backup"
date=$(date +"%d-%b-%Y-%H-%M-%p")
# Set default file permissions
Continue Reading ....
MySQL Backup
I have prepared a Shell Script to create a Backup of MYSQL database.
You can use it so that we have backup of our database(s).
#!/bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%d%b%Y_%I:%M:%S%p"`
################################################################
################## Update below values ########################
DB_BACKUP_PATH='/backup/dbbackup'
MYSQL_HOST='localhost'
MYSQL_PORT='3306'
MYSQL_USER='auriga'
MYSQL_PASSWORD='auriga#123'
DATABASE_NAME=( Project_O2 o2)
BACKUP_RETAIN_DAYS=30 ## Number of days to keep local backup copy; Enable script code in end of th script
#################################################################
{ mkdir -p ${DB_BACKUP_PATH}/${TODAY}
echo "
${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
} || {
echo "Can not make Directry"
echo "Possibly Path is wrong"
}
{ if ! mysql -u ${MYSQL_USER} -p${MYSQL_PASSWORD} -e 'exit'; then
echo 'Failed! You may have Incorrect PASSWORD/USER ' >> ${DB_BACKUP_PATH}/Backup-Report.txt
exit 1
fi
for DB in "${DATABASE_NAME[#]}"; do
if ! mysql -u ${MYSQL_USER} -p${MYSQL_PASSWORD} -e "use "${DB}; then
echo "Failed! Database ${DB} Not Found on ${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
else
# echo "Backup started for database - ${DB}"
# mysqldump -h localhost -P 3306 -u auriga -pauriga#123 Project_O2 # use gzip..
mysqldump -h ${MYSQL_HOST} -P ${MYSQL_PORT} -u ${MYSQL_USER} -p${MYSQL_PASSWORD} \
--databases ${DB} | gzip > ${DB_BACKUP_PATH}/${TODAY}/${DB}-${TODAY}.sql.gz
if [ $? -eq 0 ]; then
touch ${DB_BACKUP_PATH}/Backup-Report.txt
echo "successfully backed-up of ${DB} on ${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
# echo "Database backup successfully completed"
else
touch ${DB_BACKUP_PATH}/Backup-Report.txt
echo "Failed to backup of ${DB} on ${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
# echo "Error found during backup"
exit 1
fi
fi
done
} || {
echo "Failed during backup"
echo "Failed to backup on ${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
# ./myshellsc.sh 2> ${DB_BACKUP_PATH}/Backup-Report.txt
}
##### Remove backups older than {BACKUP_RETAIN_DAYS} days #####
# DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
# if [ ! -z ${DB_BACKUP_PATH} ]; then
# cd ${DB_BACKUP_PATH}
# if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
# rm -rf ${DBDELDATE}
# fi
# fi
### End of script ####
In the script we just need to give our Username, Password, Name of Database(or Databases if more than one) also Port number if Different.
To Run the script use Command as:
sudo ./script.sc
I also Suggest that if You want to see the Result in a file Like:
Failure Occurs or Successful in backing-up,
then Use the Command as Below:
sudo ./myshellsc.sh 2>> Backup-Report.log
Thank You.

conditional mysql operation in shell script

I have the following shell script which writes volumes in a find command to a file, and loads that file into a mysql database:
# find all the paths and print them to a file
sudo find $FULFILLMENT/ > $FILE
sudo find $ARCH1/ >> $FILE
sudo find $ARCH2/ >> $FILE
sudo find $MASTERING/ >> $FILE
# load the file into the mysql database, `files`, table `path`
/usr/local/bin/mysql -u root files -e "TRUNCATE path"
/usr/local/bin/mysql -u root files -e "LOAD DATA INFILE '/tmp/files.txt' INTO TABLE path"
TRUNCATE is to be used to delete all the old entries before adding the new. However, if any of the find commands don't work (for example, if the volume isn't accessible), I want it to skip on the two mysql commands. How would I modify the above script to do this?
This will execute your two mysql commands only if all the find commands succeed:
# find all the paths and print them to a file
sudo find $FULFILLMENT/ > $FILE &&
sudo find $ARCH1/ >> $FILE &&
sudo find $ARCH2/ >> $FILE &&
sudo find $MASTERING/ >> $FILE &&
{
# load the file into the mysql database, `files`, table `path`
/usr/local/bin/mysql -u root files -e "TRUNCATE path"
/usr/local/bin/mysql -u root files -e "LOAD DATA INFILE '/tmp/files.txt' INTO TABLE path"
}
The && operator causes the command on the RHS to run only if the command on the LHS succeeds. The { ... } groups the two mysql commands into one compound command, so either both run (if the last find succeeds) or neither does (if the last find does not succeed).
You can use this if there is more to your script that should run whether or not the finds succeed and the mysql commands run.
Something like:
sudo find $FULFILLMENT > $FILE || exit 1
You can use #!/bin/bash -e or add a line with set -e above the find commands. That way, Bash will automatically abort as soon as a command fails. It's good practice to always do this by default, since you rarely want to continue when the previous command fails:
set -e
# find all the paths and print them to a file
sudo find $FULFILLMENT/ > $FILE
sudo find $ARCH1/ >> $FILE
sudo find $ARCH2/ >> $FILE
sudo find $MASTERING/ >> $FILE
# load the file into the mysql database, `files`, table `path`
/usr/local/bin/mysql -u root files -e "TRUNCATE path"
/usr/local/bin/mysql -u root files -e "LOAD DATA INFILE '/tmp/files.txt' INTO TABLE path"