Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- All,
- Trying to learn BASH functions, don't make sense to me yet, being conceptual and concepts not clear to me yet.
- I'm down to this. When I assign the "read_db_login" routine, in line 86 (commenting out line # 87) of the following code, I get blanks returned. When I comment out #86 and uncomment 87 then I see all the vars/arrays correctly.
- Not sure why I'm not getting the array to set right, so all help appreciated.
- Cheers
- OMR
- #! /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.
- while IFS='' read -r line || [[ -n "$line" ]]; do
- if [[ ${line:0:7} = "dbuser=" ]]; then
- dbuser=${line:7};
- fi
- if [[ ${line:0:7} = "dbpass=" ]]; then
- dbpass=${line:7};
- fi
- if [[ ${line:0:7} = "dbpath=" ]]; then
- dbpath=${line:7};
- fi
- if [[ ${line:0:7} = "dbopts=" ]]; then
- dbopts=${line:7};
- fi
- done < $1;
- echo $dbuser, $dbpass, $dbpath, $dbopts;
- } # end function read_db_login
- db_restore(){
- #function db_restore
- echo "Reading file => $3";
- # declare -a log_ray=$(read_db_login $3);
- read_db_login $3;
- echo "LR=> $log_ray";
- echo "Login Info File done";
- # ${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 "Checking Mode!";
- action_mode $1 $2 $3 $4;
- exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement