![]() |
||||
|
||||
|
« Disabling root SSH logins | A basic Bash Script » |
My first BASH Script.
by: admin - Dec 18, 2005
I have used *nix Bourne Again Shell (BASH) scripts occasionally in the past. Although I know what they are and how to use them, I have never actually written one myself before. Today, I had a plan to backup all the MySQL databases on this server and writing a BASH script to help me do that easily seemed like a very good idea. I started searching the WWW for BASH shell scripting tutorials bright and early this morning, and within a couple of hours, I had written a script that did the job! In other words, BASH only seemed difficult and complicated (to me) because I didn't take the trouble to figure it out. I will paste my script below. It's not perfect and I know there is a lot of room for improvement but considering that I started learning BASH shell scripting this morning, I am quite pleased with it. A BASH script to backup MySQL databases.I only use a total of 4 MySQL databases on this server and I have root access to the MySQL server, which makes it very easy for me to use this script. Generic Code Example: #!/bin/bash echo -n "Please enter the database name [* for all]: " read db_name if [ ! -n "$db_name" ] then echo "Invalid Input. Exiting..." exit 1 fi file_name='./backup_mysql_' if [ "$db_name" = '*' ] then db_name="--databases network_db1 network_db2 network_db3 network_db4" file_name="${file_name}all" else file_name="${file_name}${db_name}" fi file_name="${file_name}.sql.bz2" if [ -e $file_name ] then rm -f $file_name; fi # mysqldump echo -e "SET FOREIGN_KEY_CHECKS=0;\n" | bzip2 > $file_name mysqldump -cC --opt --skip-lock-tables --single-transaction -u root -p $db_name | bzip2 >> $file_name echo "SET FOREIGN_KEY_CHECKS=1;" | bzip2 >> $file_name exit 0 Even now, as I write this and review the code, I see a few lines I could change and I probably will, first thing tomorrow morning. I will also write about the things I had to figure out the hard way, but that's for tomorrow as well.
|
GIDNetwork Sites
Archives
Recent GIDBlog Posts
Recent GIDForums Posts
Contact Us
|
« Disabling root SSH logins | A basic Bash Script » |