Advertising
- BASH: mysql_backup.sh
- Wednesday, August 31st, 2016 at 2:14:36pm UTC
- Trying to master "functions" in BASH. Having some mixed success, some working others not.
- Currently stuck with the routine:
- read_db_login
- not working:
- Calling this with the cmd:
- bash /script_path/mysql_backup.sh "restore" ".sql" "/../backups/sql_contents" "/Config_files/DB_login.txt"
- Getting nothing from the routine. Not sure if the file is not reading or not processing.
- Can use all help on this!
- Nyle aka: TBotNik
- #! /bin/bash
- ################################################################################
- # Author: Nyle Davis Created 12-04-20
- # Purpose: Backup all MySQL DBs and tables
- # File: mysql_backup.sh
- # Run this with command:
- # bash ../Scripts/Backups/mysql_backup.sh (options)
- # Options:
- # 1=mode: daily, system, dump, restore
- # 2=fext: .sql, .zip .tar.gz
- # 3=path: alternate backup path, such as flash or network
- # 4=Read File: Read the file containing DB login info
- # Script is CRON capable
- ################################################################################
- # Modified: 16-08-27 By: NED
- # Added the dump & restore options
- ################################################################################
- chk_dirs(){
- # Check directories, create missing ones
- hstname=$(hostname -s);
- dest="/backups/$hstname";
- myuid=${user};
- if [[ ${myuid} = "root" ]] || [[ -z ${myuid} ]]; then
- myuid=${SUDO_USER};
- fi
- drlist="/backups /Blogs /Config_files /data /dpkg-repack /Repos_DPKG";
- drlist="${drlist} /Scripts /var/www $dest $dest/daily $dest/system";
- drlist="${drlist} $dest/dump $dest/individual";
- for word in $drlist; do
- # echo "W=> $word";
- if [ ! -d "$word" ]; then
- echo "Creating Missing Dir=> $word";
- mkdir -p $word;
- chown -R $user:users $word;
- chmod -R 775 $word;
- fi
- done;
- } # end function chk_dirs
- prm_qual(){
- # Qualify parameters
- # 1=action type: daily, system, individual, dump, restore
- # 2=file ext: .sql, .zip .tar.gz
- # 3=Path: Backup or Restore path
- # 4=Read File: Read the file containing DB login info
- if [ $# -lt 4 ]; then
- if [[ $# -lt 1 ]]; then
- echo "You entered no parameters!";
- else
- echo "You are missing one or more parameters!";
- fi
- echo "The correct parameters are:";
- echo " 1=action type: daily, system, individual, dump, restore";
- echo " 2=file ext: .sql, .zip .tar.gz";
- echo " 3=Path: Backup or Restore path";
- echo " 4=Read File: Read the file containing DB login info";
- echo "Try again!";
- exit;
- fi
- echo "$1, $2, $3, $4";
- } # end function prm_qual
- read_db_login(){
- # Find the DB login data from the passed file.
- echo "Found DB_Login Read Routine!";
- echo "Reading file => $1";
- while IFS='' read -r line || [[ -n "$line" ]]; do
- echo "LN=> $line";
- if [[ ${line:0:6} = "dbuser=" ]]; then
- dbuser=${line:7};
- fi
- if [[ ${line:0:6} = 'dbpass=' ]]; then
- dbpass=${line:7};
- fi
- if [[ ${line:0:6} = 'dbpath=' ]]; then
- dbpath=${line:7};
- fi
- if [[ ${line:0:6} = 'dbopts=' ]]; then
- dbopts=${line:7};
- fi
- done < $1;
- echo $dbuser, $dbpass, $dbpath, $dbopts;
- } # end function read_db_login
- db_restore(){
- #function db_restore
- echo "Found the Restore Routine!";
- echo "1=> $1";
- echo "2=> $2";
- echo "3=> $3";
- echo "Reading Login Info File=> $3";
- declare -a log_ray=$(read_db_login $3);
- echo "Login Info File done";
- echo "LR=> $log_ray";
- # ${dbpath}/mysql -u ${dbuser} -p${dbpass} ${dbopts} < ${infile}
- } # end function db_restore
- db_backup( ){
- # Backup DBs
- echo "1=> $1";
- echo "2=> $2";
- echo "3=> $3";
- cd ${bkpath};
- rm -f "${flname}";
- GZIP="$(which gzip)"
- ${dbpath}/mysqldump -u ${dbuser} -p${dbpass} ${dbopts} -A -C -f | $GZIP -9 > ${flname};
- } # end function db_backup
- # May have to additionally manually backup the phpmyadmin option DB as sometime does not work correctly
- db_bkupind(){
- #function db_bkupind(){
- # get all database listing
- DBS="$(mysql -u $MUSER -p$MPASS -h $MHOST -P $MPORT -Bse 'show databases')";
- # start to dump database one by one
- for db in $DBS
- do
- DUMP="yes";
- if [ "$IGNOREDB" != "" ]; then
- for i in $IGNOREDB # Store all value of $IGNOREDB ON i
- do
- if [ "$db" == "$i" ]; then # If result of $DBS(db) is equal to $IGNOREDB(i) then
- DUMP="NO"; # SET value of DUMP to "no"
- #echo "$i database is being ignored!";
- fi
- done
- fi
- if [ "$DUMP" == "yes" ]; then # If value of DUMP is "yes" then backup database
- FILE="$BACKUPDIR/$NOW-$db.gz";
- echo "BACKING UP $db";
- $MYSQLDUMP --add-drop-database --opt --lock-all-tables -u $MUSER -p$MPASS -h $MHOST -P $MPORT $db | gzip > $FILE
- fi
- done
- } # end function db_bkupind
- action_mode(){
- if [[ $1 = "daily" ]]; then
- flname="("`eval date +%Y-%m-%d`")_mysql_daily.${fil_ext}";
- dest="${bakpath}/daily";
- db_backup ${"daily", "${dest}/${flname}", "${DBL_file}"};
- elif [[ $1 = "system" ]]; then
- flname="("`eval date +%Y-%m-%d`")_mysql_system.${fil_ext}";
- dest="${bakpath}/system";
- db_backup ${"system", "${dest}/${flname}", "${DBL_file}"};
- elif [[ $1 = "individual" ]]; then
- db_bkupind "${bakpath}/individual";
- elif [[ $1 = "dump" ]]; then
- flname="("`eval date +%Y-%m-%d`")_mysql_dump.${fil_ext}";
- dest="${bakpath}/dump";
- db_backup ${"dump", "${dest}/${flname}", "${DBL_file}"};
- elif [[ $1 = "restore" ]]; then
- echo "Restore Started!";
- db_restore $2 $3 $4;
- echo "Restore Complete!";
- fi
- } # end function action_mode
- echo "Checking directories!";
- chk_dirs;
- echo "Qualifying parameters!";
- declare -a ret_ray=$(prm_qual $1 $2 $3 $4);
- echo "RR=> $ret_ray";
- echo "Checking Mode!";
- action_mode $1 $2 $3 $4;
- echo "LR=> $log_ray";
- exit;
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will not expire by default. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.