Shadow-Here


Server : Apache
System : Linux methusalix2 3.16.0-11-amd64 #1 SMP Debian 3.16.84-1 (2020-06-09) x86_64
User : hios ( 1437)
PHP Version : 5.6.40-0+deb8u12
Disable Function : proc_close,proc_open,dl,shell_exec,passthru
Directory :  /etc/cron.daily/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :
Current File : //etc/cron.daily/apx-mysqldump2dir
#!/bin/bash
# vim: syntax=sh ts=3



# -------------------------------
# configs
# -------------------------------
if [ ! -f /etc/apx-mysqldump2dir/apx-mysqldump2dir.conf ]; then
	echo "FATAL apx-mysqldump2dir.conf missing.Abort"
	logger "FATAL apx-mysqldump2dir.conf missing.Abort"
	exit 1
fi
source /etc/apx-mysqldump2dir/apx-mysqldump2dir.conf

if [ /etc/apx-mysqldump2dir/apx-mysqldump2dir.local.conf ]; then
	source /etc/apx-mysqldump2dir/apx-mysqldump2dir.local.conf
fi

# some initial values
gzip_opt=""
gzip_suf=""


# -------------------------------
# check sanity
# -------------------------------

# drop privileges
if [ "x`whoami`" != "x$dump_user" ]; then
	echo "FATAL. Script must be run as $dump_user. ABORT"
	logger "FATAL. Script must be run as $dump_user. ABORT"
	echo "FATAL. Script must be run as $dump_user. ABORT" | mail -s "FATAL. Script must be run as $dump_user. ABORT" root
	exit 1
fi


# login
if ( ! echo "show databases;"|mysql $login > /dev/null ); then
	echo "FATAL. Cannot login to Database.ABORT"
	logger "FATAL. Cannot login to Database.ABORT"
	echo "FATAL. Cannot login to Database.ABORT" | mail -s "$0 FATAL. Cannot login to Database.ABORT" root
	exit 1
fi

# backup directory
if [ ! -d $directory ]; then
    echo "FATAL. Backup directory does not exists.ABORT"
    logger "FATAL. Backup directory does not exists.ABORT"
    echo "FATAL. Backup directory does not exists.ABORT" | mail -s "$0 FATAL. Backup directory does not exists.ABORT" root
    exit 1
fi

# hardcode chmod 700 for backups? @TODO
if ( ! chmod 700 $directory ); then
	echo "CANNOT CHMOD 700 backup dir. ABORT"
	logger "$0 CANNOT CHMOD 700 backup dir. ABORT"
	echo "$0 CANNOT CHMOD 700 backup dir. ABORT" | mail -s "$0 CANNOT CHMOD 700 backup dir. ABORT" root
	exit 1
fi

# check permissions of config files hardcode @TODO
if ( ! chmod 700 /etc/apx-mysqldump2dir/apx-mysqldump2dir* ); then
	echo "CANNOT CHMOD 700 backup files. ABORT"
	logger "$0 CANNOT CHMOD 700 backup files. ABORT"
	echo "$0 CANNOT CHMOD 700 backup files. ABORT" | mail -s "$0 CANNOT CHMOD 700 backup files. ABORT" root
	exit 1
fi




# gzip otf options
if [ "x1" = "x$gzip_otf" ]; then
	if [ ! -f /bin/gzip ]; then
		echo "GZIP OPTION NOT AVAILABLE. ABORT"
		logger "GZIP OPTION NOT AVAILABLE. ABORT"
		echo "GZIP OPTION NOT AVAILABLE. ABORT" | mail -s "$0 GZIP OPTION NOT AVAILABLE. ABORT" root
		exit 1
	fi
	gzip_opt='| /bin/gzip'
	gzip_suf='.gz'
fi

# check if max packets is equal to  my.cfg
cur_max_pack=`echo "show variables LIKE 'max_allowed_packet';" | mysql $login  | tail -n1|sed 's/\t/:/g'|cut -f2 -d':'`
if [ $cur_max_pack -ne $max_pack ]; then
	echo "FATAL. Your max_allowed_packet variable in dump is not equal to my.cnf. ABORT!"
	logger "FATAL. Your max_allowed_packet variable in dump is not equal to my.cnf. ABORT!"
	echo "FATAL. Your max_allowed_packet variable in dump is not equal to my.cnf. ABORT!" | mail -s "FATAL. Your max_allowed_packet variable in dump is not equal to my.cnf. ABORT!" root
	exit 1
fi

# --------------------------------------------
# MAIN
# --------------------------------------------
    databases=`echo "show databases;"|mysql $login|grep -v Database|grep -v information_schema`
    for database in $databases; do

		# hotneedles sanity check of database names
		if [ ! "x$database" = "x"`echo "$database" | sed -e "s/\ //g"` ]; then
			echo "DATABASE WITH BLANKS NOT ALLOWED" # @TODO besser machen
			logger  "$database :: DATABASE WITH BLANKS NOT ALLOWED" 
			echo "$database :: DATABASE WITH BLANKS NOT ALLOWED" # @TODO besser machen
			continue
		fi


	  # check for excludes
	  skipme=0
	  for skipthis in ${skip_databases[*]}; do
	  	if [ "x$database" = "x$skipthis" ]; then logger "$0 skipped database $skipthis as defined in configs"; skipme=1; break; fi
	  done
	  if [ "$skipme" = "1" ]; then continue; fi


      if ( ! mkdir -p $directory/$database ); then
	  		   echo "CANNOT CREATE BACKUP-DIR. ABORT"
	  		   logger "$0 CANNOT CREATE BACKUP-DIR. ABORT"
	  		   echo "$0 CANNOT CREATE BACKUP-DIR. ABORT." | mail -s "$0 CANNOT CREATE BACKUP-DIR. ABORT" root
			   exit 1
	   fi
               tables=`echo "show tables;" |  mysql $login -B $database | grep -v "Tables_in"`
               for table in $tables; do

					# hotneedles sanity check of database names
					if [ ! "x$table" = "x"`echo "$table" | sed -e "s/\ //g"` ]; then
						echo "table WITH BLANKS NOT ALLOWED" # @TODO besser machen
						logger "table WITH BLANKS NOT ALLOWED" # @TODO besser machen
						echo "$table :: table WITH BLANKS NOT ALLOWED" | mail -s "table WITH BLANKS NOT ALLOWED" root
						continue
					fi
			   	
				  # check for excludes
			      skipme=0
				  for skipthis in ${skip_tables[*]}; do
					if [ "x$table" = "x$skipthis" ] || [ "x$database.$table" = "x$skipthis" ] ; then logger "$0 skipped table $skipthis (db: $database) as defined in configs"; skipme=1; break; fi
				  done
				  if [ "$skipme" = "1" ]; then continue; fi


                   if [ -f $directory/$database/$table.sql ]; then rm -f $directory/$database/$table.sql; fi
                   if [ -f $directory/$database/$table.sql.gz ]; then rm -f $directory/$database/$table.sql.gz; fi

				   # fehler dump umleiten 2>/dev/null oder 2>>/tmp/logfile @TODO
                   if ( ! eval "mysqldump --max_allowed_packet=$max_pack $login $database $table $gzip_opt > $directory/$database/$table.sql$gzip_suf " ); then
                      echo "dump fehler $database.$table"
                      echo "dump fehler $database.$table" | mail -s "$0 dump fehler $database.$table" root
                      logger "dump fehler $database.$table"
                   fi
                   if [ -f $directory/$database/$table.sql ]; then gzip $directory/$database/$table.sql; fi
               done
    done

Samx