Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate

Advertising

BASH: mysql_backup.sh
Wednesday, August 31st, 2016 at 2:14:36pm UTC 

  1. Trying to master "functions" in BASH.  Having some mixed success, some working others not.
  2.  
  3. Currently stuck with the routine:
  4.  
  5. read_db_login
  6.  
  7. not working:
  8.  
  9. Calling this with the cmd:
  10.  
  11. bash /script_path/mysql_backup.sh "restore" ".sql" "/../backups/sql_contents" "/Config_files/DB_login.txt"
  12.  
  13. Getting nothing from the routine. Not sure if the file is not reading or not processing.
  14.  
  15. Can use all help on this!
  16.  
  17. Nyle aka: TBotNik
  18.  
  19. #! /bin/bash
  20. ################################################################################
  21. # Author:       Nyle Davis            Created 12-04-20
  22. # Purpose:      Backup all MySQL DBs and tables
  23. # File:  mysql_backup.sh
  24. # Run this with command:
  25. # bash ../Scripts/Backups/mysql_backup.sh (options)
  26. #                     Options:
  27. #                            1=mode:        daily, system, dump, restore
  28. #           2=fext:        .sql, .zip .tar.gz
  29. #                            3=path:        alternate backup path, such as flash or network
  30. #           4=Read File:   Read the file containing DB login info
  31. # Script is CRON capable
  32. ################################################################################
  33. # Modified:     16-08-27                    By: NED
  34. #                         Added the dump & restore options
  35. ################################################################################
  36.  
  37. chk_dirs(){
  38.    # Check directories, create missing ones
  39.    hstname=$(hostname -s);
  40.    dest="/backups/$hstname";
  41.    myuid=${user};
  42.    if [[ ${myuid} = "root" ]] || [[ -z ${myuid} ]]; then
  43.       myuid=${SUDO_USER};
  44.    fi
  45.    drlist="/backups /Blogs /Config_files /data /dpkg-repack /Repos_DPKG";
  46.    drlist="${drlist} /Scripts /var/www $dest $dest/daily $dest/system";
  47.    drlist="${drlist} $dest/dump $dest/individual";
  48.    for word in $drlist; do
  49.    #   echo "W=> $word";
  50.       if [ ! -d "$word" ]; then
  51.          echo "Creating Missing Dir=> $word";
  52.          mkdir -p $word;
  53.          chown -R $user:users $word;
  54.          chmod -R 775 $word;
  55.       fi
  56.    done;
  57. }       # end function chk_dirs
  58.  
  59. prm_qual(){
  60.    # Qualify parameters
  61.    #     1=action type: daily, system, individual, dump, restore
  62.    #     2=file ext:    .sql, .zip .tar.gz
  63.    #     3=Path:        Backup or Restore path
  64.    #     4=Read File:   Read the file containing DB login info
  65.    if [ $# -lt 4 ]; then
  66.       if [[ $# -lt 1 ]]; then
  67.          echo "You entered no parameters!";
  68.       else
  69.          echo "You are missing one or more parameters!";
  70.       fi
  71.       echo "The correct parameters are:";
  72.       echo "     1=action type:     daily, system, individual, dump, restore";
  73.       echo "     2=file ext:        .sql, .zip .tar.gz";
  74.       echo "     3=Path:            Backup or Restore path";
  75.       echo "     4=Read File:       Read the file containing DB login info";
  76.       echo "Try again!";
  77.       exit;
  78.    fi
  79.    echo "$1, $2, $3, $4";
  80. }       # end function prm_qual
  81.  
  82. read_db_login(){
  83.    # Find the DB login data from the passed file.
  84.    echo "Found DB_Login Read Routine!";
  85.    echo "Reading file => $1";
  86.    while IFS='' read -r line || [[ -n "$line" ]]; do
  87.       echo "LN=> $line";
  88.                 if [[ ${line:0:6} = "dbuser=" ]]; then
  89.                         dbuser=${line:7};
  90.                 fi
  91.                 if [[ ${line:0:6} = 'dbpass=' ]]; then
  92.                         dbpass=${line:7};
  93.                 fi
  94.                 if [[ ${line:0:6} = 'dbpath=' ]]; then
  95.                         dbpath=${line:7};
  96.                 fi
  97.                 if [[ ${line:0:6} = 'dbopts=' ]]; then
  98.                         dbopts=${line:7};
  99.                 fi
  100.    done < $1;
  101.    echo $dbuser, $dbpass, $dbpath, $dbopts;
  102. }       # end function read_db_login
  103.  
  104. db_restore(){
  105.         #function db_restore
  106.    echo "Found the Restore Routine!";
  107.    echo "1=> $1";
  108.    echo "2=> $2";
  109.    echo "3=> $3";
  110.    echo "Reading Login Info File=> $3";
  111.    declare -a log_ray=$(read_db_login $3);
  112.    echo "Login Info File done";
  113.    echo "LR=> $log_ray";
  114.      
  115.    # ${dbpath}/mysql -u ${dbuser} -p${dbpass} ${dbopts} < ${infile}
  116. }       # end function db_restore
  117.  
  118. db_backup( ){
  119.    # Backup DBs
  120.    echo "1=> $1";
  121.    echo "2=> $2";
  122.    echo "3=> $3";
  123.    cd ${bkpath};
  124.    rm -f "${flname}";
  125.    GZIP="$(which gzip)"
  126.    ${dbpath}/mysqldump -u ${dbuser} -p${dbpass} ${dbopts} -A -C -f | $GZIP -9 > ${flname};
  127.  
  128. }       # end function db_backup
  129.  
  130.  
  131. # May have to additionally manually backup the phpmyadmin option DB as sometime does not work correctly
  132.  
  133. db_bkupind(){
  134.         #function db_bkupind(){
  135.         # get all database listing
  136.         DBS="$(mysql -u $MUSER -p$MPASS -h $MHOST -P $MPORT -Bse 'show databases')";
  137.         # start to dump database one by one
  138.         for db in $DBS
  139.         do
  140.                      DUMP="yes";
  141.                      if [ "$IGNOREDB" != "" ]; then
  142.                              for i in $IGNOREDB # Store all value of $IGNOREDB ON i
  143.                              do
  144.                                      if [ "$db" == "$i" ]; then # If result of $DBS(db) is equal to $IGNOREDB(i) then
  145.                                              DUMP="NO";         # SET value of DUMP to "no"
  146.                                              #echo "$i database is being ignored!";
  147.                                      fi
  148.                              done
  149.                      fi
  150.  
  151.                      if [ "$DUMP" == "yes" ]; then # If value of DUMP is "yes" then backup database
  152.                              FILE="$BACKUPDIR/$NOW-$db.gz";
  153.                              echo "BACKING UP $db";
  154.                              $MYSQLDUMP --add-drop-database --opt --lock-all-tables -u $MUSER -p$MPASS -h $MHOST -P $MPORT $db | gzip > $FILE
  155.                      fi
  156.         done
  157. }       # end function db_bkupind
  158.  
  159. action_mode(){
  160.    if [[ $1 = "daily" ]]; then
  161.            flname="("`eval date +%Y-%m-%d`")_mysql_daily.${fil_ext}";
  162.       dest="${bakpath}/daily";
  163.            db_backup ${"daily", "${dest}/${flname}", "${DBL_file}"};
  164.    elif [[ $1 = "system" ]]; then
  165.            flname="("`eval date +%Y-%m-%d`")_mysql_system.${fil_ext}";
  166.       dest="${bakpath}/system";
  167.            db_backup ${"system", "${dest}/${flname}", "${DBL_file}"};
  168.    elif [[ $1 = "individual" ]]; then
  169.            db_bkupind "${bakpath}/individual";
  170.    elif [[ $1 = "dump" ]]; then
  171.            flname="("`eval date +%Y-%m-%d`")_mysql_dump.${fil_ext}";
  172.       dest="${bakpath}/dump";
  173.            db_backup ${"dump", "${dest}/${flname}", "${DBL_file}"};
  174.    elif [[ $1 = "restore" ]]; then
  175.            echo "Restore Started!";
  176.            db_restore $2 $3 $4;
  177.            echo "Restore Complete!";
  178.    fi
  179. }       # end function action_mode
  180.  
  181. echo "Checking directories!";
  182. chk_dirs;
  183. echo "Qualifying parameters!";
  184. declare -a ret_ray=$(prm_qual $1 $2 $3 $4);
  185. echo "RR=> $ret_ray";
  186. echo "Checking Mode!";
  187. action_mode $1 $2 $3 $4;
  188. echo "LR=> $log_ray";
  189. 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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

comments powered by Disqus
worth-right