Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /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(){
- #function chk_dirs(){
- # Check directories, create missing ones
- echo "Checking directories! Creating 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 Dir=> $word";
- mkdir -p $word;
- chown -R $user:users $word;
- chmod -R 775 $word;
- fi
- done;
- }
- prm_qual(){
- #function 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
- echo "Qualifying parameters!";
- echo "Cnt=> $#";
- echo "1=> $1";
- echo "2=> $2";
- echo "3=> $3";
- echo "4=> $4";
- 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
- run_type=$1;
- fil_ext=$2;
- bak_path=$3;
- DBL_file=$4;
- }
- read_db_login(){
- #function read_db_login(){
- # Find the DB login data from the passed file.
- dbuser='ndavis';
- dbpass='nomened1497';
- dbpath='/usr/bin';
- dbopts='--force --add-drop-database --add-drop-table';
- }
- db_backup( ){
- #function 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};
- }
- # 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
- }
- db_restore(){
- #function db_bkupind(){
- # get all database listing
- }
- action_mode(){
- #function action_mode(){
- if [ ${run_type} = "daily" ]; then
- flname="("`eval date +%Y-%m-%d`")_mysql_daily.${fil_ext}";
- dest="${bakpath}/daily";
- db_backup ${"daily", "${dest}/${flname}", "${DBL_file}"};
- elif [ ${run_type} = "system" ]; then
- flname="("`eval date +%Y-%m-%d`")_mysql_system.${fil_ext}";
- dest="${bakpath}/system";
- db_backup ${"system", "${dest}/${flname}", "${DBL_file}"};
- elif [ ${run_type} = "individual" ]; then
- db_bkupind "${bakpath}/individual";
- elif [ ${run_type} = "dump" ]; then
- flname="("`eval date +%Y-%m-%d`")_mysql_dump.${fil_ext}";
- dest="${bakpath}/dump";
- db_backup ${"dump", "${dest}/${flname}", "${DBL_file}"};
- elif [ ${run_type} = "restore" ]; then
- db_restore "${fil_ext} ${bak_path}";
- fi
- }
- chk_dirs;
- prm_qual;
- action_mode;
- exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement