automysqlbackup/0000755000000000000000000000000013520266407013005 5ustar rootrootautomysqlbackup/automysqlbackup0000700000175000017500000025601111666445003017024 0ustar centoscentos#!/usr/bin/env bash # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # shopt -s extglob # BEGIN _flags let "filename_flag_encrypted=0x01" let "filename_flag_gz=0x02" let "filename_flag_bz2=0x04" let "filename_flag_diff=0x08" # END _flags # BEGIN _errors_notifications let "E=0x00" # no errors let "N=0x00" # no notifications let "E_dbdump_failed=0x01" let "E_backup_local_failed=0x02" let "E_mkdir_basedir_failed=0x04" let "E_mkdir_subdirs_failed=0x08" let "E_perm_basedir=0x10" let "E_enc_cleartext_delfailed=0x20" let "E_enc_failed=0x40" let "E_db_empty=0x80" let "E_create_pipe_failed=0x100" let "E_missing_deps=0x200" let "E_no_basedir=0x400" let "E_config_backupdir_not_writable=0x800" let "E_dump_status_failed=0x1000" let "E_dump_fullschema_failed=0x2000" let "N_config_file_missing=0x01" let "N_arg_conffile_parsed=0x02" let "N_arg_conffile_unreadable=0x04" let "N_too_many_args=0x08" let "N_latest_cleanup_failed=0x10" let "N_backup_local_nofiles=0x20" # END _errors_notifications # BEGIN _functions # @info: Default configuration options. # @deps: (none) load_default_config() { CONFIG_configfile="/etc/automysqlbackup/automysqlbackup.conf" CONFIG_backup_dir='/var/backup/db' CONFIG_multicore='yes' CONFIG_multicore_threads=2 CONFIG_do_monthly="01" CONFIG_do_weekly="5" CONFIG_rotation_daily=6 CONFIG_rotation_weekly=35 CONFIG_rotation_monthly=150 CONFIG_mysql_dump_port=3306 CONFIG_mysql_dump_usessl='yes' CONFIG_mysql_dump_username='root' CONFIG_mysql_dump_password='' CONFIG_mysql_dump_host='localhost' CONFIG_mysql_dump_host_friendly='' CONFIG_mysql_dump_socket='' CONFIG_mysql_dump_create_database='no' CONFIG_mysql_dump_use_separate_dirs='yes' CONFIG_mysql_dump_compression='gzip' CONFIG_mysql_dump_commcomp='no' CONFIG_mysql_dump_latest='no' CONFIG_mysql_dump_latest_clean_filenames='no' CONFIG_mysql_dump_max_allowed_packet='' CONFIG_mysql_dump_single_transaction='no' CONFIG_mysql_dump_master_data= CONFIG_mysql_dump_full_schema='yes' CONFIG_mysql_dump_dbstatus='yes' CONFIG_mysql_dump_differential='no' CONFIG_backup_local_files=() CONFIG_db_names=() CONFIG_db_month_names=() CONFIG_db_exclude=( 'information_schema' ) CONFIG_table_exclude=() CONFIG_mailcontent='stdout' CONFIG_mail_maxattsize=4000 CONFIG_mail_splitandtar='yes' CONFIG_mail_use_uuencoded_attachments='no' CONFIG_mail_address='root' CONFIG_encrypt='no' CONFIG_encrypt_password='password0123' } # @return: true, if variable is set; else false isSet() { if [[ ! ${!1} && ${!1-_} ]]; then return 1; else return 0; fi } # @return: true, if variable is empty; else false isEmpty() { if [[ ${!1} ]]; then return 1; else return 0; fi } # @info: Called when one of the signals EXIT, SIGHUP, SIGINT, SIGQUIT or SIGTERM is emitted. # It removes the IO redirection, mails any log file information and cleans up any temporary files. # @args: (none) # @return: (none) mail_cleanup () { removeIO # if the variables $log_file and $log_errfile aren't set or are empty and both associated files don't exist, skip output methods. # this might happen if 'exit' occurs before they are set. if [[ ! -e "$log_file" && ! -e "$log_errfile" ]];then echo "Skipping normal output methods, since the program exited before any log files could be created." else case "${CONFIG_mailcontent}" in 'files') # Include error log if larger than zero. if [[ -s "$log_errfile" ]]; then backupfiles=( "${backupfiles[@]}" "$log_errfile" ) errornote="WARNING: Error Reported - " fi temp="$(mktemp "$CONFIG_backup_dir"/tmp/mail_content.XXXXXX)" # Get backup size attsize=`du -c "${backupfiles[@]}" | awk 'END {print $1}'` if (( ${CONFIG_mail_maxattsize} >= ${attsize} )); then if [[ "x$CONFIG_mail_use_uuencoded_attachments" = "xyes" ]]; then cat "$log_file" > "$temp" for j in "${backupfiles[@]}"; do uuencode "$j" "$j" >> "$temp" done mail -s "${errornote} MySQL Backup Log and SQL Files for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address} < "$temp" else mutt -s "${errornote} MySQL Backup Log and SQL Files for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" -a "${backupfiles[@]}" -- ${CONFIG_mail_address} < "$log_file" fi elif (( ${CONFIG_mail_maxattsize} <= ${attsize} )) && [[ "x$CONFIG_mail_splitandtar" = "xyes" ]]; then if sPWD="$PWD"; cd "$CONFIG_backup_dir"/tmp && pax -wv "${backupfiles[@]}" | bzip2_compression | split -b $((CONFIG_mail_maxattsize*1000)) - mail_attachment_${datetimestamp}_ && cd "$sPWD"; then files=("$CONFIG_backup_dir"/tmp/mail_attachment_${datetimestamp}_*) echo -e "\n\nThe attachments have been split into multiple files.\nUse 'cat mail_attachment_2011-08-13_13h15m_* > mail_attachment_2011-08-13_13h15m.tar.bz2' to combine them and \ 'bunzip2 "$temp" uuencode "$j" "$j" >> "$temp" else uuencode "$j" "$j" > "$temp" fi mail -s "${errornote} MySQL Backup Log and SQL Files for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address} < "$temp" else mutt -s "${errornote} MySQL Backup Log and SQL Files for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}; Part $((j+1))/${#files[@]}" -a "${files[j]}" -- ${CONFIG_mail_address} < "$log_file" fi done else cat "$log_file" | mail -s "WARNING! - MySQL Backup exceeds set maximum attachment size on ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address} fi else cat "$log_file" | mail -s "WARNING! - MySQL Backup exceeds set maximum attachment size on ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address} fi rm "$temp" ;; 'log') cat "$log_file" | mail -s "MySQL Backup Log for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address} [[ -s "$log_errfile" ]] && cat "$log_errfile" | mail -s "ERRORS REPORTED: MySQL Backup error Log for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address} ;; 'quiet') [[ -s "$log_errfile" ]] && cat "$log_errfile" | mail -s "ERRORS REPORTED: MySQL Backup error Log for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address} ;; *) if [[ -s "$log_errfile" ]]; then cat "$log_file" echo echo "###### WARNING ######" echo "Errors reported during AutoMySQLBackup execution.. Backup failed" echo "Error log below.." cat "$log_errfile" else cat "$log_file" fi ;; esac ################################################################################### # Clean up and finish [[ -e "$log_file" ]] && rm -f "$log_file" [[ -e "$log_errfile" ]] && rm -f "$log_errfile" fi } # @params: #month #year # @deps: (none) days_of_month() { m="$1"; y="$2"; a=$(( 30+(m+m/8)%2 )) (( m==2 )) && a=$((a-2)) (( m==2 && y%4==0 && ( y<100 || y%100>0 || y%400==0) )) && a=$((a+1)) printf '%d' $a } # @info: Checks if a folder is writable by creating a temporary file in it and removing it afterwards. # @args: folder to test # @return: returns false if creation of temporary file failed or it can't be removed afterwards; else true # @deps: (none) chk_folder_writable () { local temp; temp="$(mktemp "$1"/tmp.XXXXXX)" if (( $? == 0 )); then rm "${temp}" || return 1 return 0 else return 1 fi } # @info: bzip2 compression bzip2_compression() { var=("$@") re='^[0-9]*$' if [[ "x$CONFIG_multicore" = 'xyes' ]]; then if [[ "x$CONFIG_multicore_threads" != 'xauto' ]] && [[ "x$CONFIG_multicore_threads" =~ $re ]]; then var=( "-p${CONFIG_multicore_threads}" "${var[@]}" ) fi pbzip2 "${var[@]}" else bzip2 "${var[@]}" fi } # @info: gzip compression gzip_compression() { var=("$@") re='^[0-9]*$' if [[ "x$CONFIG_multicore" = 'xyes' ]]; then if [[ "x$CONFIG_multicore_threads" != 'xauto' ]] && [[ "x$CONFIG_multicore_threads" =~ $re ]]; then var=( "-p${CONFIG_multicore_threads}" "${var[@]}" ) fi pigz "${var[@]}" else gzip "${var[@]}" fi } # @info: Remove date and time information from filename by renaming it. # @args: filename # @return: (none) # @deps: (none) remove_datetimeinfo () { mv "$1" "$(echo "$1" | sed -re 's/_[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}h[0-9]{2}m_(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday|January|February|March|April|May|June|July|August|September|October|November|December|[0-9]{1,2})//g')" } export -f remove_datetimeinfo # @info: Set time and date variables. # @args: (none) # @deps: days_of_month set_datetime_vars() { datetimestamp=`date +%Y-%m-%d_%Hh%Mm` # Datestamp e.g 2002-09-21_18h12m date_stamp=`date +%Y-%m-%d` # Datestamp e.g 2002-09-21 date_day_of_week=`date +%A` # Day of the week e.g. Monday date_dayno_of_week=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday date_day_of_month=`date +%e | sed -e 's/^ //'` # Date of the Month e.g. 27 date_month=`date +%B` # Month e.g January date_weekno=`date +%V | sed -e 's/^0//'` # Week Number e.g 37 year=`date +%Y` month=`date +%m | sed -e 's/^0//'` date_lastday_of_last_month=$(days_of_month $(( $month==1 ? 12 : $month-1 )) $(( $month==1 ? ($year-1):$year )) ) date_lastday_of_this_month=$(days_of_month $month $year) } # @info: This function is called after data has already been saved. It performs encryption and # hardlink-copying of files to a latest folder. # @return: flags # @deps: load_default_config files_postprocessing () { local flags let "flags=0x00" let "flags_files_postprocessing_success_encrypt=0x01" # -> CONFIG_encrypt [[ "${CONFIG_encrypt}" = "yes" && "${CONFIG_encrypt_password}" ]] && { if (( $CONFIG_dryrun )); then printf 'dry-running: openssl enc -aes-256-cbc -e -in %s -out %s.enc -pass pass:%s\n' ${1} ${1} "${CONFIG_encrypt_password}" else openssl enc -aes-256-cbc -e -in ${1} -out ${1}.enc -pass pass:"${CONFIG_encrypt_password}" if (( $? == 0 )); then if rm ${1} 2>&1; then echo "Successfully encrypted archive as ${1}.enc" let "flags |= $flags_files_postprocessing_success_encrypt" else echo "Successfully encrypted archive as ${1}.enc, but could not remove cleartext file ${1}." let "E |= $E_enc_cleartext_delfailed" fi else let "E |= $E_enc_failed" fi fi } # <- CONFIG_encrypt # -> CONFIG_mysql_dump_latest [[ "${CONFIG_mysql_dump_latest}" = "yes" ]] && { if (( $flags & $flags_files_postprocessing_success_encrypt )); then if (( $CONFIG_dryrun )); then printf 'dry-running: cp -al %s.enc %s/latest/\n' "${1}" "${CONFIG_backup_dir}" else cp -al "${1}${suffix}.enc" "${CONFIG_backup_dir}"/latest/ fi else if (( $CONFIG_dryrun )); then printf 'dry-running: cp -al %s %s/latest/\n' "${1}" "${CONFIG_backup_dir}" else cp -al "${1}" "${CONFIG_backup_dir}"/latest/ fi fi } # <- CONFIG_mysql_dump_latest return $flags } # @info: When called, sets error and notify strings matching their flags. It then goes through all # collected error and notify messages and displays them. # @args: (none) # @return: true if no errors were set, otherwise false # @deps: log_base2, load_default_config error_handler () { errors=( [0x01]='dbdump() failed.' [0x02]='Backup of local files failed. This is not this scripts primary objective. Continuing anyway.' [0x04]="Could not create the backup_dir ${CONFIG_backup_dir}. Please check permissions of the higher directory." [0x08]='At least one of the subdirectories (daily, weekly, monthly, latest) failed to create.' [0x10]="The backup_dir ${CONFIG_backup_dir} is not writable AND/OR executable." [0x20]='Could not remove the cleartext file after encryption. This error did not cause an abort. Remove it manually and check permissions.' [0x40]='Encryption failed. Continuing without encryption.' [0x80]='The mysql server is empty, i.e. no databases found. Check if something is wrong. Exiting.' [0x100]='Failed to create the named pipe (fifo) for reading in all databases. Exiting.' [0x200]='Dependency programs are missing. Perhaps they are not in $PATH. Exiting.' [0x400]='No basedir found, i.e. ' [0x800]="${CONFIG_backup_dir} is not writable. Exiting." [0x1000]='Running of mysqlstatus failed.' [0x2000]='Running of mysqldump full schema failed.' ) notify=( [0x01]="${CONFIG_configfile} was not found - no global config file." [0x02]="Parsed config file ${opt_config_file}." [0x04]="Unreadable config file \"${opt_config_file}\"" [0x08]='Supplied more than one argument, ignoring ALL arguments - using default and global config file only.' [0x10]='Could not remove the files in the latest directory. Please check this.' [0x20]='No local backup files were set.' [0x40]='' [0x80]='' [0x100]='' [0x200]='' [0x400]='' [0x800]='' [0x1000]='' [0x2000]='' ) local n local e n=$((${#notify[@]}-1)) while (( N > 0 )); do e=$((2**n)) if (( N&e )); then echo "Note:" ${notify[e]} let "N-=e" fi ((n--)) done unset n; n=$((${#errors[@]}-1)) if (( E > 0 )); then while (( E > 0 )); do e=$((2**n)) if (( E&e )); then echo "Error:" ${errors[e]} let "E-=e" fi ((n--)) done exit 1 else exit 0 fi } # @info: Packs files in array ${#CONFIG_backup_local_files[@]} into tar file with optional compression. # @args: archive file without compression suffix, i.e. ending on .tar # @return: true in case of dry-run, otherwise the return value of tar -cvf # @deps: load_default_config backup_local_files () { if ((! ${#CONFIG_backup_local_files[@]})) ; then if (( $CONFIG_dryrun )); then case "${CONFIG_mysql_dump_compression}" in 'gzip') echo "tar -czvf ${1}${suffix} ${CONFIG_backup_local_files[@]}"; ;; 'bzip2') echo "tar -cjvf ${1}${suffix} ${CONFIG_backup_local_files[@]}"; ;; *) echo "tar -cvf ${1}${suffix} ${CONFIG_backup_local_files[@]}"; ;; esac echo "dry-running: tar -cv ${1} ${CONFIG_backup_local_files[@]}" return 0; else case "${CONFIG_mysql_dump_compression}" in 'gzip') tar -czvf "${1}${suffix}" "${CONFIG_backup_local_files[@]}"; return $? ;; 'bzip2') tar -cjvf "${1}${suffix}" "${CONFIG_backup_local_files[@]}"; return $? ;; *) tar -cvf "${1}${suffix}" "${CONFIG_backup_local_files[@]}"; return $? ;; esac fi else let "N |= $N_backup_local_nofiles" echo "No local backup files specified." fi } # @info: Parses the configuration options and sets the variables appropriately. # @args: (none) # @deps: load_default_config parse_configuration () { # OPT string for use with mysqldump ( see man mysqldump ) opt=( '--quote-names' '--opt' ) # OPT string for use with mysql (see man mysql ) mysql_opt=() # OPT string for use with mysqldump fullschema opt_fullschema=( '--all-databases' '--routines' '--no-data' ) # OPT string for use with mysqlstatus opt_dbstatus=( '--status' ) [[ "${CONFIG_mysql_dump_usessl}" = "yes" ]] && { opt=( "${opt[@]}" '--ssl' ) mysql_opt=( "${mysql_opt[@]}" '--ssl' ) opt_fullschema=( "${opt_fullschema[@]}" '--ssl' ) opt_dbstatus=( "${opt_dbstatus[@]}" '--ssl' ) } [[ "${CONFIG_mysql_dump_master_data}" ]] && (( ${CONFIG_mysql_dump_master_data} == 1 || ${CONFIG_mysql_dump_master_data} == 2 )) && { opt=( "${opt[@]}" "--master-data=${CONFIG_mysql_dump_master_data}" );} [[ "${CONFIG_mysql_dump_single_transaction}" = "yes" ]] && { opt=( "${opt[@]}" '--single-transaction' ) opt_fullschema=( "${opt_fullschema[@]}" '--single-transaction' ) } [[ "${CONFIG_mysql_dump_commcomp}" = "yes" ]] && { opt=( "${opt[@]}" '--compress' ) opt_fullschema=( "${opt_fullschema[@]}" '--compress' ) opt_dbstatus=( "${opt_dbstatus[@]}" '--compress' ) } [[ "${CONFIG_mysql_dump_max_allowed_packet}" ]] && { opt=( "${opt[@]}" "--max_allowed_packet=${CONFIG_mysql_dump_max_allowed_packet}" ) opt_fullschema=( "${opt_fullschema[@]}" "--max_allowed_packet=${CONFIG_mysql_dump_max_allowed_packet}" ) } [[ "${CONFIG_mysql_dump_socket}" ]] && { opt=( "${opt[@]}" "--socket=${CONFIG_mysql_dump_socket}" ) mysql_opt=( "${mysql_opt[@]}" "--socket=${CONFIG_mysql_dump_socket}" ) opt_fullschema=( "${opt_fullschema[@]}" "--socket=${CONFIG_mysql_dump_socket}" ) opt_dbstatus=( "${opt_dbstatus[@]}" "--socket=${CONFIG_mysql_dump_socket}" ) } [[ "${CONFIG_mysql_dump_port}" ]] && { opt=( "${opt[@]}" "--port=${CONFIG_mysql_dump_port}" ) mysql_opt=( "${mysql_opt[@]}" "--port=${CONFIG_mysql_dump_port}" ) opt_fullschema=( "${opt_fullschema[@]}" "--port=${CONFIG_mysql_dump_port}" ) opt_dbstatus=( "${opt_dbstatus[@]}" "--port=${CONFIG_mysql_dump_port}" ) } # Check if CREATE DATABASE should be included in Dump if [[ "${CONFIG_mysql_dump_use_separate_dirs}" = "yes" ]]; then if [[ "${CONFIG_mysql_dump_create_database}" = "no" ]]; then opt=( "${opt[@]}" '--no-create-db' ) else opt=( "${opt[@]}" '--databases' ) fi else opt=( "${opt[@]}" '--databases' ) fi # if differential backup is active and the specified rotation is smaller than 21 days, set it to 21 days to ensure, that # master backups aren't deleted. if [[ "x$CONFIG_mysql_dump_differential" = "xyes" ]] && (( ${CONFIG_rotation_daily} < 21 )); then CONFIG_rotation_daily=21 fi # -> determine suffix case "${CONFIG_mysql_dump_compression}" in 'gzip') suffix='.gz';; 'bzip2') suffix='.bz2';; *) suffix='';; esac # <- determine suffix # -> check exclude tables for wildcards local tmp;tmp=() local z;z=0 for i in "${CONFIG_table_exclude[@]}"; do r='^[^*.]+\.[^.]+$'; [[ "$i" =~ $r ]] || { printf 'The entry %s in CONFIG_table_exclude has a wrong format. Ignoring the entry.' "$i"; continue; } db=${i%.*} table=${i#"$db".} r='\*'; [[ "$i" =~ $r ]] || { tmp[z++]="$i"; continue; } while read -r; do tmp[z++]="${db}.${REPLY}"; done < <(mysql --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${mysql_opt[@]}" --batch --skip-column-names -e "select table_name from information_schema.tables where table_schema='${db}' and table_name like '${table//\*/%}';") done for l in "${tmp[@]}"; do echo "exclude $l";done CONFIG_table_exclude=("${tmp[@]}") # <- if ((${#CONFIG_table_exclude[@]})); then for i in "${CONFIG_table_exclude[@]}"; do opt=( "${opt[@]}" "--ignore-table=$i" ) done fi } # @info: Backup database status # @args: archive file without compression suffix, i.e. ending on .txt # @return: true in case of dry-run, otherwise the return value of mysqlshow # @deps: load_default_config, parse_configuration dbstatus() { if (( $CONFIG_dryrun )); then case "${CONFIG_mysql_dump_compression}" in 'gzip') echo "dry-running: mysqlshow --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_dbstatus[@]} | gzip_compression > ${1}${suffix}"; ;; 'bzip2') echo "dry-running: mysqlshow --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_dbstatus[@]} | bzip2_compression > ${1}${suffix}"; ;; *) echo "dry-running: mysqlshow --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_dbstatus[@]} > ${1}${suffix}"; ;; esac return 0; else case "${CONFIG_mysql_dump_compression}" in 'gzip') mysqlshow --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_dbstatus[@]}" | gzip_compression > "${1}${suffix}"; return $? ;; 'bzip2') mysqlshow --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_dbstatus[@]}" | bzip2_compression > "${1}${suffix}"; return $? ;; *) mysqlshow --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_dbstatus[@]}" > "${1}${suffix}"; return $? ;; esac fi } # @info: Backup of the database schema. # @args: filename to save data to # @return: true in case of dry-run, otherwise the return value of mysqldump # @deps: load_default_config, parse_configuration fullschema () { if (( $CONFIG_dryrun )); then case "${CONFIG_mysql_dump_compression}" in 'gzip') echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_fullschema[@]} | gzip_compression > ${1}${suffix}"; ;; 'bzip2') echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_fullschema[@]} | bzip2_compression > ${1}${suffix}"; ;; *) echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_fullschema[@]} > ${1}${suffix}"; ;; esac return 0; else case "${CONFIG_mysql_dump_compression}" in 'gzip') mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_fullschema[@]}" | gzip_compression > "${1}${suffix}"; return $? ;; 'bzip2') mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_fullschema[@]}" | bzip2_compression > "${1}${suffix}"; return $? ;; *) mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_fullschema[@]}" > "${1}${suffix}"; return $? ;; esac fi } # @info: Process a single db. # @args: subfolder, prefix, midfix, extension, rotation, rotation_divisor, rotation_string, 0/1 (db/dbs), db[, db ...] process_dbs() { local subfolder="$1" local prefix="$2" local midfix="$3" local extension="$4" local rotation="$5" local rotation_divisor="$6" local rotation_string="$7" local multipledbs="$8" shift 8 local name local subsubfolder # only activate differential backup for daily backups [[ "x$subfolder" != "xdaily" ]] && activate_differential_backup=0 || activate_differential_backup=1 if (( $multipledbs )); then # multiple dbs subsubfolder="" name="all-databases" else # single db subsubfolder="/$1" name="$@" fi [[ -d "${CONFIG_backup_dir}/${subfolder}${subsubfolder}" ]] || { if (( $CONFIG_dryrun )); then printf 'dry-running: mkdir -p %s/${subfolder}%s\n' "${CONFIG_backup_dir}" "${subsubfolder}" else mkdir -p "${CONFIG_backup_dir}/${subfolder}${subsubfolder}" fi } manifest_file="${CONFIG_backup_dir}/${subfolder}${subsubfolder}/Manifest" fname="${CONFIG_backup_dir}/${subfolder}${subsubfolder}/${prefix}${name}_${datetimestamp}${midfix}${extension}" (( $CONFIG_debug )) && echo "DEBUG: process_dbs >> Setting manifest file to: ${manifest_file}" if (( $multipledbs )); then # multiple databases db="all-databases" else # single db db="$1" fi if [[ "x$CONFIG_mysql_dump_differential" = "xyes" ]] && [[ "x${CONFIG_encrypt}" != "xyes" ]] && (( $activate_differential_backup )); then unset manifest_entry manifest_entry_to_check echo "## Reading in Manifest file" parse_manifest "$manifest_file" echo echo "Number of manifest entries: $(num_manifest_entries)" echo # -> generate diff file let "filename_flags=0x00" # ## -> get latest differential manifest entry for specified db # if get_latest_manifest_entry_for_db "$db" 1; then # pid="${manifest_entry[2]}" # # filename format: prefix_db_YYYY-MM-DD_HHhMMm_[A-Za-z0-9]{8}(.sql|.diff)(.gz|.bz2)(.enc) # FileStub=${manifest_entry[0]%.@(sql|diff)*} # FileExt=${manifest_entry[0]#"$FileStub"} # re=".*\.enc.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_encrypted" # re=".*\.gz.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_gz" # re=".*\.bz2.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_bz2" # re=".*\.diff.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_diff" # manifest_latest_diff_entry=("${manifest_entry[@]}") # else # no entries in manifest # pid=0 # fi # ## <- get latest differential manifest entry for specified db ## -> get latest master manifest entry for specified db # Create a differential backup if a master entry in the manifest exists, it isn't the day we do weekly master backups or the master file we fetched is already from today. if get_latest_manifest_entry_for_db "$db" 0 && ( (( ${date_dayno_of_week} != ${CONFIG_do_weekly} )) || [[ "${manifest_entry[0]}" = *_$(date +%Y-%m-%d)_* ]] ); then pid="${manifest_entry[2]}" # filename format: prefix_db_YYYY-MM-DD_HHhMMm_[A-Za-z0-9]{8}(.sql|.diff)(.gz|.bz2)(.enc) FileStub="${manifest_entry[0]%.@(sql|diff)*}" FileExt="${manifest_entry[0]#"$FileStub"}" re=".*\.enc.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_encrypted" re=".*\.gz.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_gz" re=".*\.bz2.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_bz2" re=".*\.diff.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_diff" manifest_latest_master_entry=("${manifest_entry[@]}") else # no entries in manifest pid=0 fi ## <- get latest master manifest entry for specified db fi if [[ "x$CONFIG_mysql_dump_differential" = "xyes" ]] && [[ "x${CONFIG_encrypt}" != "xyes" ]] && (( $activate_differential_backup )) && ((! ($filename_flags & $filename_flag_encrypted) )); then # the master file is encrypted ... well this just shouldn't happen ^^ not going to decrypt or stuff like that ...at least not today :) if [[ "x$pid" = "x0" ]]; then # -> create master backup cfname="$(mktemp "${fname%.sql}_"XXXXXXXX".sql${suffix}")" uid="${cfname%.@(diff|sql)*}" uid="${uid:-8:8}" case "${CONFIG_mysql_dump_compression}" in 'gzip') mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" | gzip_compression > "$cfname"; ;; 'bzip2') mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" | bzip2_compression > "$cfname"; ;; *) mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" > "$cfname"; ;; esac add_manifest_entry "$manifest_file" "$cfname" "$pid" "$db" && parse_manifest "$manifest_file" && cp -al "$cfname" "${CONFIG_backup_dir}"/latest/ && echo "Generated master backup $cfname" && return 0 || return 1 # <- create master backup else cfname="$(mktemp "${fname%.sql}_"XXXXXXXX".diff${suffix}")" uid="${cfname%.@(diff|sql)*}" uid=${uid:-8:8} echo "Creating differential backup to ${manifest_entry[0]}:" case "${CONFIG_mysql_dump_compression}" in 'gzip') if (( $filename_flags & $filename_flag_gz )); then diff <(gzip_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | gzip_compression > "$cfname"; elif (( $filename_flags & $filename_flag_bz2 )); then diff <(bzip2_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | gzip_compression > "$cfname"; else diff "${manifest_latest_master_entry[0]}" <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | gzip_compression > "$cfname"; fi ;; 'bzip2') if (( $filename_flags & $filename_flag_gz )); then diff <(gzip_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | bzip2_compression > "$cfname"; elif (( $filename_flags & $filename_flag_bz2 )); then diff <(bzip2_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | bzip2_compression > "$cfname"; else diff "${manifest_latest_master_entry[0]}" <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | bzip2_compression > "$cfname"; fi ;; *) if (( $filename_flags & $filename_flag_gz )); then diff <(gzip_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") > "$cfname"; elif (( $filename_flags & $filename_flag_bz2 )); then diff <(bzip2_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") > "$cfname"; else diff "${manifest_latest_master_entry[0]}" <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") > "$cfname"; fi ;; esac add_manifest_entry "$manifest_file" "$cfname" "$pid" "$db" && parse_manifest "$manifest_file" && cp -al "$cfname" "${manifest_latest_master_entry[0]}" "${CONFIG_backup_dir}"/latest/ && echo "generated $cfname" && return 0 || return 1 fi # <- generate diff filename else cfname="${fname}${suffix}" if (( $CONFIG_dryrun )); then case "${CONFIG_mysql_dump_compression}" in 'gzip') echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt[@]} $@ | gzip_compression > ${cfname}" ;; 'bzip2') echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt[@]} $@ | bzip2_compression > ${cfname}" ;; *) echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt[@]} $@ > ${cfname}" ;; esac return 0; else case "${CONFIG_mysql_dump_compression}" in 'gzip') mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" | gzip_compression > "${cfname}" ret=$? ;; 'bzip2') mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" | bzip2_compression > "${cfname}" ret=$? ;; *) mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" > "${cfname}" ret=$? ;; esac fi fi if (( $ret == 0 )); then echo "Rotating $(( ${rotation}/${rotation_divisor} )) ${rotation_string} backups for ${name}" if (( $CONFIG_dryrun )); then find "${CONFIG_backup_dir}/${subfolder}${subsubfolder}" -mtime +"${rotation}" -type f -exec echo "dry-running: rm" {} \; else find "${CONFIG_backup_dir}/${subfolder}${subsubfolder}" -mtime +"${rotation}" -type f -exec rm {} \; fi files_postprocessing "$cfname" tmp_flags=$?; var=; (( $tmp_flags & $flags_files_postprocessing_success_encrypt )) && var=.enc backupfiles=( "${backupfiles[@]}" "${cfname}${var}" ) else let "E |= $E_dbdump_failed" echo "dbdump with parameters \"${CONFIG_db_names[@]}\" \"${cfname}\" failed!" fi } # @info: Save stdout and stderr # @deps: (none) activateIO() { ################################################################################### # IO redirection for logging. # $1 = $log_file, $2 = $log_errfile #(( $CONFIG_debug )) || { touch "$log_file" exec 6>&1 # Link file descriptor #6 with stdout. Saves stdout. exec > "$log_file" # stdout replaced with file $log_file. touch "$log_errfile" exec 7>&2 # Link file descriptor #7 with stderr. Saves stderr. exec 2> "$log_errfile" # stderr replaced with file $log_errfile. #} } # @info: Restore stdout and stderr redirections. # @deps: (none) removeIO() { exec 1>&6 6>&- # Restore stdout and close file descriptor #6. exec 2>&7 7>&- # Restore stdout and close file descriptor #7. } # @info: Checks directories and subdirectories for existence and activates logging to either # $CONFIG_backup_dir or /tmp depending on what exists. # @args: (none) # @deps: load_default_config, activateIO, chk_folder_writable, error_handler directory_checks_enable_logging () { ################################################################################### # Check directories and do cleanup work checkdirs=( "${CONFIG_backup_dir}"/{daily,weekly,monthly,latest,tmp} ) [[ "${CONFIG_backup_local_files[@]}" ]] && { checkdirs=( "${checkdirs[@]}" "${CONFIG_backup_dir}/backup_local_files" ); } [[ "${CONFIG_mysql_dump_full_schema}" = 'yes' ]] && { checkdirs=( "${checkdirs[@]}" "${CONFIG_backup_dir}/fullschema" ); } [[ "${CONFIG_mysql_dump_dbstatus}" = 'yes' ]] && { checkdirs=( "${checkdirs[@]}" "${CONFIG_backup_dir}/status" ); } tmp_permcheck=0 printf '# Checking for permissions to write to folders:\n' # "dirname ${CONFIG_backup_dir}" exists? # Y -> ${CONFIG_backup_dir} exists? # Y -> Dry-run? # Y -> log to /tmp, proceed to test subdirs # N -> check writable ${CONFIG_backup_dir}? # Y -> proceed to test subdirs # N -> error: can't write to ${CONFIG_backup_dir}. Exit. # N -> Dry-run? # N -> proceed without testing subdirs # Y -> create directory ${CONFIG_backup_dir}? # Y -> check writable ${CONFIG_backup_dir}? # Y -> proceed to test subdirs # N -> error: can't write to ${CONFIG_backup_dir}. Exit. # N -> error: ${CONFIG_backup_dir} is not writable. Exit. # N -> Dry-run? # Y -> log to /tmp, proceed without testing subdirs # N -> error: no basedir. Exit. # -> check base folder printf 'base folder %s ... ' "$(dirname "${CONFIG_backup_dir}")" if [[ -d "$(dirname "${CONFIG_backup_dir}")" ]]; then printf 'exists ... ok.\n' printf 'backup folder %s ... ' "${CONFIG_backup_dir}" if [[ -d "${CONFIG_backup_dir}" ]]; then printf 'exists ... writable? ' if (( $CONFIG_dryrun )); then printf 'dry-running. Skipping. Logging to /tmp\n' log_file="/tmp/${CONFIG_mysql_dump_host}-`date +%N`.log" log_errfile="/tmp/ERRORS_${CONFIG_mysql_dump_host}-`date +%N`.log" activateIO "$log_file" "$log_errfile" tmp_permcheck=1 else if chk_folder_writable "${CONFIG_backup_dir}"; then printf 'yes. Proceeding.\n' log_file="${CONFIG_backup_dir}/${CONFIG_mysql_dump_host}-`date +%N`.log" log_errfile="${CONFIG_backup_dir}/ERRORS_${CONFIG_mysql_dump_host}-`date +%N`.log" activateIO "$log_file" "$log_errfile" tmp_permcheck=1 else printf 'no. Exiting.\n' let "E |= $E_config_backupdir_not_writable" error_handler fi fi else printf 'creating ... ' if (( $CONFIG_dryrun )); then printf 'dry-running. Skipping.\n' else if mkdir -p "${CONFIG_backup_dir}" >/dev/null 2>&1; then printf 'success.\n' log_file="${CONFIG_backup_dir}/${CONFIG_mysql_dump_host}-`date +%N`.log" log_errfile="${CONFIG_backup_dir}/ERRORS_${CONFIG_mysql_dump_host}-`date +%N`.log" activateIO "$log_file" "$log_errfile" tmp_permcheck=1 else printf 'failed. Exiting.\n' let "E |= $E_mkdir_basedir_failed" error_handler fi fi fi else if (( $CONFIG_dryrun )); then printf 'dry-running. Skipping. Logging to /tmp\n' log_file="/tmp/${CONFIG_mysql_dump_host}-`date +%N`.log" log_errfile="/tmp/ERRORS_${CONFIG_mysql_dump_host}-`date +%N`.log" activateIO "$log_file" "$log_errfile" else printf 'does not exist. Exiting.\n' let "E |= $E_no_basedir" error_handler fi fi # <- check base folder # -> check subdirs if (( $tmp_permcheck == 1 )); then (( $CONFIG_dryrun )) || [[ -r "${CONFIG_backup_dir}" && -x "${CONFIG_backup_dir}" ]] || { let "E |= $E_perm_basedir"; error_handler; } for i in "${checkdirs[@]}"; do printf 'checking directory "%s" ... ' "$i" if [[ -d "$i" ]]; then printf 'exists.\n' else printf 'creating ... ' if (( $CONFIG_dryrun )); then printf 'dry-running. Skipping.\n' else if mkdir -p "$i" >/dev/null 2>&1; then printf 'success.\n' else printf 'failed. Exiting.\n' let "E |= $E_mkdir_subdirs_failed" error_handler fi fi fi done fi # <- check subdirs } # @info: If CONFIG_mysql_dump_latest is set to 'yes', the directory ${CONFIG_backup_dir}"/latest will # be cleaned. # @args: (none) # @deps: load_default_config cleanup_latest () { # -> latest cleanup if [[ "${CONFIG_mysql_dump_latest}" = "yes" ]]; then printf 'Cleaning up latest directory ... ' if (( $CONFIG_dryrun )); then printf 'dry-running. Skipping.\n' else if rm -f "${CONFIG_backup_dir}"/latest/* >/dev/null 2>&1; then printf 'success.\n' else printf 'failed. Continuing anyway, activating Note-Flag.\n' let "N |= $N_latest_cleanup_failed" fi fi fi # <- latest cleanup } # @info: Checks for dependencies in form of external programs, that need to be available when running # this program. # @args: (none) # @deps: load_default_config check_dependencies () { echo echo "# Testing for installed programs" dependencies=( 'mysql' 'mysqldump' ) if [[ "x$CONFIG_multicore" = 'xyes' ]]; then if [[ "x$CONFIG_mysql_dump_compression" = 'xbzip2' ]]; then if type pbzip2 &>/dev/null; then echo "pbzip2 ... found." else CONFIG_multicore='no' # turn off multicore support, since the program isn't there echo "WARNING: Turning off multicore support, since pbzip2 isn't there." fi elif [[ "x$CONFIG_mysql_dump_compression" = 'xgzip' ]]; then if type pigz &>/dev/null; then echo "pigz ... found." else CONFIG_multicore='no' # turn off multicore support, since the program isn't there echo "WARNING: Turning off multicore support, since pigz isn't there." fi fi else [[ "x$CONFIG_mysql_dump_compression" = 'xbzip2' ]] && dependencies=("${dependencies[@]}" 'bzip2' ) [[ "x$CONFIG_mysql_dump_compression" = 'xgzip' ]] && dependencies=("${dependencies[@]}" 'gzip' ) fi if [[ "x$CONFIG_mailcontent" = 'xlog' || "x$CONFIG_mailcontent" = 'xquiet' ]]; then dependencies=( "${dependencies[@]}" 'mail' ) elif [[ "x$CONFIG_mailcontent" = 'xfiles' ]]; then dependencies=( "${dependencies[@]}" 'mail' ) if [[ "x$CONFIG_mail_use_uuencoded_attachments" != 'xyes' ]]; then dependencies=( "${dependencies[@]}" 'mutt' ) fi fi for i in "${dependencies[@]}"; do printf '%s ... ' "$i" if type "$i" &>/dev/null; then printf 'found.\n' else printf 'not found. Aborting.\n'; let "E |= $E_missing_deps" error_handler fi done echo } # @info: Get database list and remove excluded ones. # @args: (none) # @deps: load_default_config, error_handler # # alldbnames = array of all databases # empty? -> error # remove excludes from array alldbnames # CONFIG_db_names empty? -> set to alldbnames # CONFIG_db_month_names empty? -> set to alldbnames # parse_databases() { # bash 4.x version #mapfile -t alldbnames < <(mysql --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" --batch --skip-column-names -e "show databases") alldbnames=() printf "# Parsing databases ... " # bash 3.0 local i;i=0; while read -r; do alldbnames[i++]="$REPLY"; done < <(mysql --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${mysql_opt[@]}" --batch --skip-column-names -e "show databases") unset i # mkfifo foo || exit; trap 'rm -f foo' EXIT ((! "${#alldbnames[@]}" )) && { let "E |= $E_db_empty"; error_handler; } # -> remove excluded dbs from list for exclude in "${CONFIG_db_exclude[@]}"; do for i in "${!alldbnames[@]}"; do if [[ "x${alldbnames[$i]}" = "x${exclude}" ]]; then unset 'alldbnames[i]'; fi; done done # <- remove excluded dbs from list # check for empty array lists and copy all dbs ((! ${#CONFIG_db_names[@]})) && CONFIG_db_names=( "${alldbnames[@]}" ) ((! ${#CONFIG_db_month_names[@]})) && CONFIG_db_month_names=( "${alldbnames[@]}" ) printf "done.\n" } # @return: true if locked, false otherwise # @param: manifest_file status_manifest() { if [[ -e "$1".lock ]]; then return 0 else return 1 fi } # @return: true if successfully created lock file, else false # @param: manifest_file lock_manifest() { if status_manifest "$1"; then return 0 else if touch "$1".lock &>/dev/null; then return 0 else return 1 fi fi } # @return: true if successfully removed lock file, else false # @param: manifest_file unlock_manifest() { if status_manifest "$1"; then if rm "$1".lock &>/dev/null; then return 0 else return 1 fi else return 0 fi } # @return: true if unlock_manifest or lock_manifest, depending on status_manifest, return true, else false # @param: manifest_file toggle_manifest() { if status_manifest "$1"; then unlock_manifest "$1" && return 0 || return 1 else lock_manifest "$1" && return 0 || return 1 fi } # expects manifest_entry_to_check to be an array with four entries # @param: manifest_file # return: 0, if all is okay # 1, file doesn't exist - removed entry from manifest # 2, file doesn't exist - tried to remove entry from manifest, but failed check_manifest_entry() { local entry_md5sum [[ ! -e "${manifest_entry_to_check[0]}" ]] && { rm_manifest_entry_by_filename "${manifest_entry_to_check[0]}" 1 && return 1 || return 2; } entry_md5sum="$(md5sum "${manifest_entry_to_check[0]}" | awk '{print $1}')" if [[ "${entry_md5sum}" != "${manifest_entry_to_check[1]}" ]]; then printf 'g/%s/s//%s/g\nw\nq' "${manifest_entry_to_check[1]}" "${entry_md5sum}" | ed -s "$1" else return 0 fi } # parse manifest file and collect entries in manifest_array # @param: manifest_file # # sort manifest file after first field (filename) -> read this line by line # check if line matches regexp || add to array manifest_entries_corrupted && continue # split lines at tab character \t and put into array line_arr # check manifest entry # -> file does not exist -> remove entry from manifest; continue no matter if this succeeds or not # loop through previous entries in the manifest # filename already in there? remove all entries with the same filename but the one that is already in the array # md5sum has already occured? # if size = 0 # then don't compare # else # request user action by adding entry to array manifest_entries_user_action_required with information, that identical files exist && continue 2 # fi # add entry to manifest_array # parse_manifest() { local i n re line line_arr check unset manifest_array; manifest_array=() local tmp_md5sum # array ( filename_1, md5sum_1, id_1[, rel_id_1] ), ... ) # reserving 4 members for each entry, thus each filename entry in the array has array key 4(n-1)+1 (( $CONFIG_debug )) && echo ">>>>>>> Parsing manifest file: $1" n=1 [[ -s "$1" ]] && while read line do # ANY CHANGES INSIDE HERE ON THE MANIFEST_FILE HAVE NO IMPACT ON THE LINES WE LOOP OVER; THE sort COMMAND READS THE FILE ENTIRELY AT THE BEGINNING AND PASSES THE OUTPUT TO THE LOOP # check if line has expected format, i.e. check against regular expression re=$'^[^\t]*\tmd5sum\t[^\t]*\tdiff_id\t[A-Za-z0-9]{8}\trel_id\t(0|[A-Za-z0-9]{8})\tdb\t[^\t]*$' [[ $line =~ $re ]] || { echo "Corrupted line: $line"; manifest_entries_corrupted=( "${manifest_entries_corrupted[@]}" "$1" "$line" ); continue; } IFS=$'\t' read -ra line_arr <<< "$line" # prepare array of the current line manifest_entry_to_check=() for ((i=0;i<${#line_arr[@]};i=$i+2)); do manifest_entry_to_check[i/2]="${line_arr[i]}" done # check manifest entry, which uses the array manifest_entry_to_check check_manifest_entry "$1" check=$? case $check in 1) (( $CONFIG_debug )) && echo "File for manifest entry $line does not exist. Entry removed." continue # file doesn't exist - removed entry from manifest break;; 2) (( $CONFIG_debug )) && echo "File for manifest entry $line does not exist. Failed to remove the entry." continue # file doesn't exist - tried to remove entry from manifest, but failed break;; esac # loop through the manifest_array, as it has been filled by now and check if an entry already exists with the same values for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "x${manifest_array[i]}" = "x${line_arr[0]}" ]]; then # found entry with the same filename (( $CONFIG_debug )) && echo "Found multiple entries with the same filename. Removing all but the first-found entry from manifest." # remove all entries with this filename and add a new one based on the values of the item already in the array rm_manifest_entry_by_filename "$1" "${manifest_array[i]}" 1 && add_manifest_entry "$1" "${manifest_array[i]}" "${manifest_array[i+3]}" continue 2 # the original entry, to which we compared, is already in the manifest_array; no matter if this is resolved or not, we don't # need to add this entry to the manifest_array elif [[ "x${manifest_array[i+1]}" = "x${line_arr[2]}" ]]; then # found entry with different filename but same md5sum - file copied and renamed?! if [[ ! -s "${line_arr[0]}" ]]; then # empty file - don't start to compare md5sums ... (( $CONFIG_debug )) && echo "Found empty file ${line_arr[0]}." else (( $CONFIG_debug )) && echo "Found multiple entries with the same md5sum but different filename." (( $CONFIG_debug )) && echo -e ">> fname_manifest:\t${manifest_array[i]}\t${manifest_array[i+1]}\n>> fname_line:\t\t${line_arr[0]}\t${line_arr[2]}" if [[ "x${line_arr[6]}" != "x0" ]]; then if [[ "x${manifest_array[i+3]}" = "x${line_arr[6]}" ]]; then # parent id is the same; TODO inform user of this predicament and suggest solution manifest_entries_user_action_required=( "${manifest_entries_user_action_required[@]}" "$1" "${manifest_array[i]}" "The file has an identical copy with the same parent id. If you don't know why it exists, it is safe to remove it." ) continue 2 else manifest_entries_user_action_required=( "${manifest_entries_user_action_required[@]}" "$1" "${manifest_array[i]}" "The file has an identical copy with different parent id. This should not happen. Remove the file, which is not the correct follow-up to the previous differential or master backup." ) continue 2 fi fi fi fi done # add entry to manifest array for ((i=0;i<${#line_arr[@]};i=$i+2)); do manifest_array[(n-1)*${fields}+i/2]="${line_arr[i]}" #echo "manifest array key $((($n-1)*4+$i/2)) with value ${line_arr[i]}" done ((n++)) done < <(sort -t $'\t' -k"1" "$1") (( $CONFIG_debug )) && echo "<<<<<<< # manifest entries: $((${#manifest_array[@]}/$fields))" (( $CONFIG_debug )) && echo "<<<<<<< FINISHED" return 0 } # get_manifest_entry_by_* PATTERN [regexp] # if second parameter 'regexp' (string!) is passed, PATTERN will be matched as regular expression get_manifest_entry_by_filename() { local i if [[ "x$2" = "xregexp" ]]; then for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "${manifest_array[i]}" =~ $1 ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done else for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "x${manifest_array[i]}" = "x$1" ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done fi return 1 } get_manifest_entry_by_md5sum() { local i if [[ "x$2" = "xregexp" ]]; then for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "${manifest_array[i+1]}" =~ $1 ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done else for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "x${manifest_array[i+1]}" = "x$1" ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done fi return 1 } get_manifest_entry_by_id() { local i if [[ "x$2" = "xregexp" ]]; then for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "${manifest_array[i+2]}" =~ $1 ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done else for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "x${manifest_array[i+2]}" = "x$1" ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done fi return 1 } get_manifest_entry_by_rel_id() { local i if [[ "x$2" = "xregexp" ]]; then for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "${manifest_array[i+3]}" =~ $1 ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done else for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "x${manifest_array[i+3]}" = "x$1" ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done fi return 1 } get_manifest_entry_by_db() { local i if [[ "x$2" = "xregexp" ]]; then for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "${manifest_array[i+4]}" =~ $1 ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done else for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "x${manifest_array[i+4]}" = "x$1" ]]; then manifest_entry=( "${manifest_array[i]}" "${manifest_array[i+1]}" "${manifest_array[i+2]}" "${manifest_array[i+3]}" "${manifest_array[i+4]}" ) return 0 break; fi done fi return 1 } # @params: db, master/diff (0,1) # @return: 2: no entries in manifest for the specified database 'db' # 1: could not get manifest element by filename # 0: all fine, match is in array 'manifest_entry' get_latest_manifest_entry_for_db() { local db_array newarray i for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if (( $2 )); then # latest differential or master backup, i.e. just take the latest one! if [[ "x${manifest_array[i+4]}" = "x$1" ]]; then db_array=( "${db_array[@]}" "${manifest_array[i]}" ) fi else # latest master backup, pid=0 if [[ "x${manifest_array[i+4]}" = "x$1" && "x${manifest_array[i+3]}" = "x0" ]]; then db_array=( "${db_array[@]}" "${manifest_array[i]}") fi fi done if (( "${#db_array[@]}" == 0 )); then return 2; else #newarray=(); while IFS= read -r -d '' line; do newarray+=("$line"); done < <(printf '%s\0' "${db_array[@]}" | sort -z) get_manifest_entry_by_filename "${db_array[@]:(-1)}" # last entry of db_array, has, due to the way sort works, to be the latest one return $? fi } # @params: manifest_file filename/md5sum/id/rel_id [1(=don't parse manifest after finished)] # if second parameters # # lock manifest -> use awk, print all lines that don't have second parameter at the appropriate field -> unlock manifest # param3=0 -> parse manifest # rm_manifest_entry_by_filename() { lock_manifest "$1" && awk -F"\t" -v v="$2" '$1 != v' "$1" > "$1".tmp && mv "$1".tmp "$1" && unlock_manifest "$1" || return 1 (( "$3" )) || parse_manifest "$1" return 0 } rm_manifest_entry_by_md5sum() { lock_manifest "$1" && awk -F"\t" -v v="$2" '$3 != v' "$1" > "$1".tmp && mv "$1".tmp "$1" && unlock_manifest "$1" || return 1 (( "$3" )) || parse_manifest "$1" return 0 } rm_manifest_entry_by_id() { lock_manifest "$1" && awk -F"\t" -v v="$2" '$5 != v' "$1" > "$1".tmp && mv "$1".tmp "$1" && unlock_manifest "$1" || return 1 (( "$3" )) || parse_manifest "$1" return 0 } rm_manifest_entry_by_rel_id() { lock_manifest "$1" && awk -F"\t" -v v="$2" '$7 != v' "$1" > "$1".tmp && mv "$1".tmp "$1" && unlock_manifest "$1" || return 1 (( "$3" )) || parse_manifest "$1" return 0 } rm_manifest_entry_by_db() { lock_manifest "$1" && awk -F"\t" -v v="$2" '$9 != v' "$1" > "$1".tmp && mv "$1".tmp "$1" && unlock_manifest "$1" || return 1 (( "$3" )) || parse_manifest "$1" return 0 } # parameters: manifest_file, filename, parent_id, db add_manifest_entry() { local md5sum local id local filename local parent_id local db filename="$2" parent_id="$3" db="$4" lock_manifest "$1" || return 1 id="${filename%.@(diff|sql)*}" id="${id:(-8):8}" #id="$(echo $filename | sed -re 's/.*_[0-9]{2}h[0-9]{2}m_([^\.]*)\..*/\1/')" md5sum="$(md5sum "$filename" | awk '{print $1}')" if [[ "x$parent_id" = 'x' ]]; then echo -e "${filename}\tmd5sum\t${md5sum}\tdiff_id\t${id}\trel_id\t0\tdb\t${db}" >> "$1" else echo -e "${filename}\tmd5sum\t${md5sum}\tdiff_id\t${id}\trel_id\t${parent_id}\tdb\t${db}" >> "$1" fi unlock_manifest "$1" || return 1 } # @info: Echos number of manifest entries. num_manifest_entries() { echo "$((${#manifest_array[@]}/$fields))" } # @info: Test if a value is in the array testarray # @param: value # @var in_array_index: array index of the first match # @return 0 if a match was found, otherwise 1 in_array() { local j for ((j=0;j<"${#testarray[@]}";j++)); do if [[ "x${testarray[j]}" = "x$1" ]]; then in_array_index=$j return 0 fi done return 1 } # @param: clear(0/1), meta_information, list_value1, list_value2, ... extended_select() { local a c k m i r r_number meta_information choice selection do_clear meta_information="$2" do_clear="$1" shift 2 declare -a list=("$@") selection=() # BEGIN _select_filenames #tput sc while true; do if (( $do_clear )); then clear else : #tput rc fi declare -a testarray=("${selection[@]}") echo "Selection for <$meta_information>" echo "Notation: 1,2-4,-5,-6-9 or * or -* ('-' will remove selections)." # print options for ((i=0;i<"${#list[@]}";i++)); do if in_array $i; then echo -e "$i) [+]\t${list[i]}" else echo -e "$i) [ ]\t${list[i]}" fi done echo -e "$i)\tDONE" done_id=$i min=0 max=${#list[@]} # we have to account for the last possible number of DONE # evaluate response while true; do printf '#? ' read choice r='^((-?[0-9]+(-[0-9]+)?,)*-?[0-9]+(-[0-9]+)?|-?\*)$' [[ $choice =~ $r ]] || continue if [[ "x$choice" = 'x*' ]]; then unset m for ((m=0;m<"${#list[@]}";m++)); do selection=("${selection[@]}" "$m") done continue 2 elif [[ "x$choice" = 'x-*' ]]; then selection=() continue 2 else unset string num1 num2 op op_rm r_number='^[0-9]$' # BEGIN process_choice for ((a=0;a<${#choice};a++)) do c="${choice:a:1}" declare -a testarray=("${selection[@]}") if (( ${#string} == 0 )) && [[ "x$c" = "x-" ]] && ! (($op)); then op_rm=1 continue elif [[ $c =~ $r ]]; then string=${string}"$c" if (( $a == (${#choice}-1) )); then # last character # we have a A-B case if (($op)); then num2="$string" unset k for ((k=$num1;k<=$num2;k++)); do (( $k >= $min )) && (( $k <= $max )) || continue if ! in_array $k; then selection=("${selection[@]}" $k) else if (( $op_rm )); then new_array=() for ((m="$((${#selection[@]}-1))";m>=0;m--)); do if [[ "x${selection[m]}" != "x$k" ]]; then new_array=("${new_array[@]}" "${selection[m]}") fi done declare -a selection=("${new_array[@]}") fi fi done unset op op_rm num1 num2 string continue else (( $string >= $min )) && (( $string <= $max )) || continue if ! in_array "$string"; then selection=("${selection[@]}" "$string") else if (($op_rm)); then unset m new_array=() for ((m=0;m<"${#selection[@]}";m++)); do if [[ "x${selection[m]}" != "x$string" ]]; then new_array=("${new_array[@]}" "${selection[m]}") fi done declare -a selection=("${new_array[@]}") fi fi fi else continue fi elif [[ "x$c" = "x-" ]]; then num1="$string" unset string op=1 if (( $a == (${#choice}-1) )); then break else continue fi elif [[ "x$c" = "x," ]]; then # we have a A-B case if (($op)); then num2="$string" unset k for ((k=$num1;k<=$num2;k++)); do (( $k >= $min )) && (( $k <= $max )) || continue if ! in_array $k; then selection=("${selection[@]}" $k) else if (( $op_rm )); then unset m new_array=() for ((m=0;m<"${#selection[@]}";m++)); do if [[ "x${selection[m]}" != "x$k" ]]; then new_array=("${new_array[@]}" "${selection[m]}") fi done declare -a selection=("${new_array[@]}") fi fi done unset op op_rm num1 num2 string continue else # it's just a single number (( $string >= $min )) && (( $string <= $max )) || { unset op op_rm num1 num2 string; continue; } if ! in_array "$string"; then selection=("${selection[@]}" "$string") else if (($op_rm)); then unset m new_array=() for ((m=0;m<"${#selection[@]}";m++)); do if [[ "x${selection[m]}" != "x$string" ]]; then new_array=("${new_array[@]}" "${selection[m]}") fi done declare -a selection=("${new_array[@]}") fi fi unset op op_rm num1 num2 string continue fi else continue 2; # this should not happen fi done # END process_choice declare -a testarray=("${selection[@]}") if in_array "$done_id"; then break 2 else continue 2 fi fi done done extended_select_return=() extended_select_return_id=() for i in "${selection[@]}"; do [[ "x$i" != "x$done_id" ]] && { extended_select_return=("${extended_select_return[@]}" "${list[i]}"); extended_select_return_id=("${extended_select_return_id[@]}" "$i"); } done } # END _functions # BEGIN _methods # @info: Backup method method_backup () { manifest_entries_corrupted=() manifest_entries_user_action_required=() # END __FUNCTIONS ############################################################################################################## # BEGIN __STARTUP load_default_config trap mail_cleanup EXIT SIGHUP SIGINT SIGQUIT SIGTERM if [[ -r "${CONFIG_configfile}" ]]; then source "${CONFIG_configfile}"; echo "Parsed config file \"${CONFIG_configfile}\""; else let "N |= $N_config_file_missing"; fi; echo if (( $opt_flag_config_file )); then if [[ -r "${opt_config_file}" ]]; then source "${opt_config_file}"; let "N |= $N_arg_conffile_parsed"; else let "N |= $N_arg_conffile_unreadable"; fi; else let "N |= $N_too_many_args"; fi (( $CONFIG_dryrun )) && { echo "NOTE: We are dry-running. That means, that the script just shows you what it would do, if it were operating normally." echo "THE PRINTED COMMANDS CAN'T BE COPIED AND EXECUTED IF THERE ARE SPECIAL CHARACTERS, SPACES, ETC. IN THERE THAT WOULD NEED TO BE PROPERLY QUOTED IN ORDER TO WORK. THESE WERE CORRECTLY QUOTED FOR THE OUTPUT COMMAND, BUT CAN'T BE SEEN NOW." echo } export LC_ALL=C PROGNAME=`basename $0` PATH=${PATH}:/usr/local/bin:/usr/bin:/bin:/usr/local/mysql/bin version=3.0 fields=5 # manifest fields directory_checks_enable_logging cleanup_latest set_datetime_vars check_dependencies # check for required programs parse_configuration # parse configuration and set variables appropriately # END __STARTUP #-------------------------------------------------------------------------------------------------------------------------------------- # BEGIN __PREPARE backupfiles=() parse_databases # debug output of variables (( $CONFIG_debug )) && { echo; echo "# DEBUG: printing all current variables"; declare -p | egrep -o '.* (CONFIG_[a-z_]*|opt|mysql_opt|opt_dbstatus|opt_fullschema)=.*'; echo; } (( $CONFIG_debug )) && { echo "DEBUG: before pre-backup"; ( IFS=,; echo "DEBUG: CONFIG_db_names '${CONFIG_db_names[*]}'" ); ( IFS=,; echo "DEBUG: CONFIG_db_month_names '${CONFIG_db_month_names[*]}'" );} # END __PREPARE #-------------------------------------------------------------------------------------------------------------------------------------- # BEGIN __MAIN ### filename formats ## ## example date values: # 14'th of August (08) 2011 # week number: 32 # Sunday (date_dayno_of_week: 7) ## ## separate db's: # monthly_DBNAME_2011-08-14_18h12m_August.sql(.enc).{gz,bzip2} # weekly_DBNAME_2011-08-14_18h12m_32.sql(.enc).{gz,bzip2} # daily_DBNAME_2011-08-14_18h12m_7.sql(.enc).{gz,bzip2} ## all-databases: # monthly_all-databases_DBNAME_2011-08-14_18h12m_August.sql(.enc).{gz,bzip2} # weekly_all-databases_DBNAME_2011-08-14_18h12m_32.sql(.enc).{gz,bzip2} # daily_all-databases_DBNAME_2011-08-14_18h12m_7.sql(.enc).{gz,bzip2} echo "======================================================================" echo "AutoMySQLBackup version ${version}" echo "http://sourceforge.net/projects/automysqlbackup/" echo echo "Backup of Database Server - ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host}" ( IFS=,; echo "Databases - ${CONFIG_db_names[*]}" ) ( IFS=,; echo "Databases (monthly) - ${CONFIG_db_month_names[*]}" ) echo "======================================================================" # -> preback commands if [[ "${CONFIG_prebackup}" ]]; then echo "======================================================================" echo "Prebackup command output." echo source ${CONFIG_prebackup} echo echo "======================================================================" echo fi # <- preback commands # -> backup local files if [[ "${CONFIG_backup_local_files[@]}" ]] && [[ ${CONFIG_do_weekly} != 0 && ${date_dayno_of_week} = ${CONFIG_do_weekly} ]] && (shopt -s nullglob dotglob; f=("${CONFIG_backup_dir}/backup_local_files/bcf_weekly_${date_stamp}_"[0-9][0-9]"h"[0-9][0-9]"m_${date_weekno}.tar${suffix}"); ((! ${#f[@]}))); then echo "======================================================================" echo "Backup local files. Doing this weekly on CONFIG_do_weekly." echo backup_local_files "${CONFIG_backup_dir}/backup_local_files/bcf_weekly_${datetimestamp}_${date_weekno}.tar" tmp_flags=$?; var=; if (( $? == 0 )); then echo "success!" backupfiles=( "${backupfiles[@]}" "${CONFIG_backup_dir}/backup_local_files/bcf_weekly_${datetimestamp}_${date_weekno}.tar" ) else let "E |= $E_backup_local_failed" echo "failed!" fi echo echo "======================================================================" echo fi # <- backup local files # -> dump full schema if [[ "${CONFIG_mysql_dump_full_schema}" = 'yes' ]]; then echo "======================================================================" echo "Dump full schema." echo # monthly if (( ${CONFIG_do_monthly} != 0 && (${date_day_of_month} == ${CONFIG_do_monthly} || $date_day_of_month == $date_lastday_of_this_month && $date_lastday_of_this_month < ${CONFIG_do_monthly}) )) && (shopt -s nullglob dotglob; f=("${CONFIG_backup_dir}/fullschema/fullschema_monthly_${date_stamp}_"[0-9][0-9]"h"[0-9][0-9]"m_${date_month}.sql${suffix}"); ((! ${#f[@]}))); then fullschema "${CONFIG_backup_dir}/fullschema/fullschema_monthly_${datetimestamp}_${date_month}.sql" if (( $? == 0 )); then echo "Rotating $(( ${CONFIG_rotation_monthly}/31 )) month backups for ${mdb}" if (( $CONFIG_dryrun )); then find "${CONFIG_backup_dir}/fullschema" -mtime +"${CONFIG_rotation_monthly}" -type f -name 'fullschema_monthly*' -exec echo "dry-running: rm" {} \; else find "${CONFIG_backup_dir}/fullschema" -mtime +"${CONFIG_rotation_monthly}" -type f -name 'fullschema_monthly*' -exec rm {} \; fi files_postprocessing "${CONFIG_backup_dir}/fullschema/fullschema_monthly_${datetimestamp}_${date_month}.sql${suffix}" tmp_flags=$?; var=; (( $tmp_flags & $flags_files_postprocessing_success_encrypt )) && var=.enc backupfiles=( "${backupfiles[@]}" "${CONFIG_backup_dir}/fullschema/fullschema_monthly_${datetimestamp}_${date_month}.sql${suffix}${var}" ) else let "E |= $E_dump_fullschema_failed" fi fi # weekly if [[ ${CONFIG_do_weekly} != 0 && ${date_dayno_of_week} = ${CONFIG_do_weekly} ]] && (shopt -s nullglob dotglob; f=("${CONFIG_backup_dir}/fullschema/fullschema_weekly_${date_stamp}_"[0-9][0-9]"h"[0-9][0-9]"m_${date_weekno}.sql${suffix}"); ((! ${#f[@]}))); then fullschema "${CONFIG_backup_dir}/fullschema/fullschema_weekly_${datetimestamp}_${date_weekno}.sql" if (( $? == 0 )); then echo "Rotating $(( ${CONFIG_rotation_monthly}/31 )) month backups for ${mdb}" if (( $CONFIG_dryrun )); then find "${CONFIG_backup_dir}/fullschema" -mtime +"${CONFIG_rotation_weekly}" -type f -name 'fullschema_weekly*' -exec echo "dry-running: rm" {} \; else find "${CONFIG_backup_dir}/fullschema" -mtime +"${CONFIG_rotation_weekly}" -type f -name 'fullschema_weekly*' -exec rm {} \; fi files_postprocessing "${CONFIG_backup_dir}/fullschema/fullschema_weekly_${datetimestamp}_${date_weekno}.sql${suffix}" tmp_flags=$?; var=; (( $tmp_flags & $flags_files_postprocessing_success_encrypt )) && var=.enc backupfiles=( "${backupfiles[@]}" "${CONFIG_backup_dir}/fullschema/fullschema_weekly_${datetimestamp}_${date_weekno}.sql${suffix}${var}" ) else let "E |= $E_dump_fullschema_failed" fi fi # daily fullschema "${CONFIG_backup_dir}/fullschema/fullschema_daily_${datetimestamp}_${date_day_of_week}.sql" if (( $? == 0 )); then echo "Rotating $(( ${CONFIG_rotation_monthly}/31 )) month backups for ${mdb}" if (( $CONFIG_dryrun )); then find "${CONFIG_backup_dir}/fullschema" -mtime +"${CONFIG_rotation_daily}" -type f -name 'fullschema_daily*' -exec echo "dry-running: rm" {} \; else find "${CONFIG_backup_dir}/fullschema" -mtime +"${CONFIG_rotation_daily}" -type f -name 'fullschema_daily*' -exec rm {} \; fi files_postprocessing "${CONFIG_backup_dir}/fullschema/fullschema_daily_${datetimestamp}_${date_day_of_week}.sql${suffix}" tmp_flags=$?; var=; (( $tmp_flags & $flags_files_postprocessing_success_encrypt )) && var=.enc backupfiles=( "${backupfiles[@]}" "${CONFIG_backup_dir}/fullschema/fullschema_daily_${datetimestamp}_${date_day_of_week}.sql${suffix}${var}" ) else let "E |= $E_dump_fullschema_failed" fi echo echo "======================================================================" echo fi # <- dump full schema # -> dump status if [[ "${CONFIG_mysql_dump_dbstatus}" = 'yes' ]]; then echo "======================================================================" echo "Dump status." echo # monthly if (( ${CONFIG_do_monthly} != 0 && (${date_day_of_month} == ${CONFIG_do_monthly} || $date_day_of_month == $date_lastday_of_this_month && $date_lastday_of_this_month < ${CONFIG_do_monthly}) )) && (shopt -s nullglob dotglob; f=("${CONFIG_backup_dir}/status/status_monthly_${date_stamp}_"[0-9][0-9]"h"[0-9][0-9]"m_${date_month}.txt${suffix}"); ((! ${#f[@]}))); then dbstatus "${CONFIG_backup_dir}/status/status_monthly_${datetimestamp}_${date_month}.txt" if (( $? == 0 )); then echo "Rotating $(( ${CONFIG_rotation_monthly}/31 )) month backups for ${mdb}" if (( $CONFIG_dryrun )); then find "${CONFIG_backup_dir}/status" -mtime +"${CONFIG_rotation_monthly}" -type f -name 'status_monthly*' -exec echo "dry-running: rm" {} \; else find "${CONFIG_backup_dir}/status" -mtime +"${CONFIG_rotation_monthly}" -type f -name 'status_monthly*' -exec rm {} \; fi files_postprocessing "${CONFIG_backup_dir}/status/status_monthly_${datetimestamp}_${date_month}.txt${suffix}" tmp_flags=$?; var=; (( $tmp_flags & $flags_files_postprocessing_success_encrypt )) && var=.enc backupfiles=( "${backupfiles[@]}" "${CONFIG_backup_dir}/status/status_monthly_${datetimestamp}_${date_month}.txt${suffix}${var}" ) else let "E |= $E_dump_status_failed" fi fi # weekly if [[ ${CONFIG_do_weekly} != 0 && ${date_dayno_of_week} = ${CONFIG_do_weekly} ]] && (shopt -s nullglob dotglob; f=("${CONFIG_backup_dir}/status/status_weekly_${date_stamp}_"[0-9][0-9]"h"[0-9][0-9]"m_${date_weekno}.txt${suffix}"); ((! ${#f[@]}))); then dbstatus "${CONFIG_backup_dir}/status/status_weekly_${datetimestamp}_${date_weekno}.txt" if (( $? == 0 )); then echo "Rotating $(( ${CONFIG_rotation_monthly}/31 )) month backups for ${mdb}" if (( $CONFIG_dryrun )); then find "${CONFIG_backup_dir}/status" -mtime +"${CONFIG_rotation_weekly}" -type f -name 'status_weekly*' -exec echo "dry-running: rm" {} \; else find "${CONFIG_backup_dir}/status" -mtime +"${CONFIG_rotation_weekly}" -type f -name 'status_weekly*' -exec rm {} \; fi files_postprocessing "${CONFIG_backup_dir}/status/status_weekly_${datetimestamp}_${date_weekno}.txt${suffix}" tmp_flags=$?; var=; (( $tmp_flags & $flags_files_postprocessing_success_encrypt )) && var=.enc backupfiles=( "${backupfiles[@]}" "${CONFIG_backup_dir}/status/status_weekly_${datetimestamp}_${date_weekno}.txt${suffix}${var}" ) else let "E |= $E_dump_status_failed" fi fi # daily dbstatus "${CONFIG_backup_dir}/status/status_daily_${datetimestamp}_${date_day_of_week}.txt" if (( $? == 0 )); then echo "Rotating $(( ${CONFIG_rotation_monthly}/31 )) month backups for ${mdb}" if (( $CONFIG_dryrun )); then find "${CONFIG_backup_dir}/status" -mtime +"${CONFIG_rotation_daily}" -type f -name 'status_daily*' -exec echo "dry-running: rm" {} \; else find "${CONFIG_backup_dir}/status" -mtime +"${CONFIG_rotation_daily}" -type f -name 'status_daily*' -exec rm {} \; fi files_postprocessing "${CONFIG_backup_dir}/status/status_daily_${datetimestamp}_${date_day_of_week}.txt${suffix}" tmp_flags=$?; var=; (( $tmp_flags & $flags_files_postprocessing_success_encrypt )) && var=.enc backupfiles=( "${backupfiles[@]}" "${CONFIG_backup_dir}/status/status_daily_${datetimestamp}_${date_day_of_week}.txt${suffix}${var}" ) else let "E |= $E_dump_status_failed" fi echo echo "======================================================================" echo fi # <- dump status # -> BACKUP DATABASES echo "Backup Start Time `date`" echo "======================================================================" ## <- monthly backup, unique per month if (( ${CONFIG_do_monthly} != 0 && (${date_day_of_month} == ${CONFIG_do_monthly} || $date_day_of_month == $date_lastday_of_this_month && $date_lastday_of_this_month < ${CONFIG_do_monthly}) )); then echo "Monthly Backup ..." echo subfolder="monthly" prefix="monthly_" midfix="_${date_month}" extension=".sql" rotation="${CONFIG_rotation_monthly}" rotation_divisor="31" rotation_string="month" if [[ "${CONFIG_mysql_dump_use_separate_dirs}" = "yes" ]]; then for db in "${CONFIG_db_month_names[@]}"; do echo "Monthly Backup of Database ( ${db} )" (shopt -s nullglob dotglob; f=("${CONFIG_backup_dir}/${subfolder}/${db}/${prefix}${db}_${date_stamp}_"[0-9][0-9]"h"[0-9][0-9]"m${midfix}${extension}${suffix}"); ((${#f[@]}))) && continue process_dbs "$subfolder" "$prefix" "$midfix" "$extension" "$rotation" "$rotation_divisor" "$rotation_string" 0 "$db" echo ---------------------------------------------------------------------- done else echo "Monthly backup of databases ( ${CONFIG_db_month_names[@]} )." (shopt -s nullglob dotglob; f=("${CONFIG_backup_dir}/${subfolder}/${prefix}all-databases_${date_stamp}_"[0-9][0-9]"h"[0-9][0-9]"m${midfix}${extension}${suffix}"); ((${#f[@]}))) && process_dbs "$subfolder" "$prefix" "$midfix" "$extension" "$rotation" "$rotation_divisor" "$rotation_string" 1 "${CONFIG_db_month_names[@]}" echo "----------------------------------------------------------------------" fi fi ## <- monthly backup ## <- weekly backup, unique per week if (( ${CONFIG_do_weekly} != 0 && ${date_dayno_of_week} == ${CONFIG_do_weekly} )); then echo "Weekly Backup ..." echo subfolder="weekly" prefix="weekly_" midfix="_${date_weekno}" extension=".sql" rotation="${CONFIG_rotation_weekly}" rotation_divisor="7" rotation_string="week" if [[ "${CONFIG_mysql_dump_use_separate_dirs}" = "yes" ]]; then for db in "${CONFIG_db_names[@]}"; do echo "Weekly Backup of Database ( ${db} )" (shopt -s nullglob dotglob; f=("${CONFIG_backup_dir}/${subfolder}/${db}/${prefix}${db}_${date_stamp}_"[0-9][0-9]"h"[0-9][0-9]"m${midfix}${extension}${suffix}"); ((${#f[@]}))) && continue process_dbs "$subfolder" "$prefix" "$midfix" "$extension" "$rotation" "$rotation_divisor" "$rotation_string" 0 "$db" echo "----------------------------------------------------------------------" done else echo "Weekly backup of databases ( ${CONFIG_db_names[@]} )." (shopt -s nullglob dotglob; f=("${CONFIG_backup_dir}/${subfolder}/${prefix}all-databases_${date_stamp}_"[0-9][0-9]"h"[0-9][0-9]"m${midfix}${extension}${suffix}"); ((${#f[@]}))) && process_dbs "$subfolder" "$prefix" "$midfix" "$extension" "$rotation" "$rotation_divisor" "$rotation_string" 1 "${CONFIG_db_names[@]}" echo "----------------------------------------------------------------------" fi fi ## <- weekly backup ## -> daily backup, test (( 1 )) is always true, just creates a grouping for Kate, which can be closed ^^ if (( 1 )); then echo "Daily Backup ..." echo subfolder="daily" prefix="daily_" midfix="_${date_day_of_week}" extension=".sql" rotation="${CONFIG_rotation_daily}" rotation_divisor="1" rotation_string="day" if [[ "${CONFIG_mysql_dump_use_separate_dirs}" = "yes" ]]; then for db in "${CONFIG_db_names[@]}"; do echo "Daily Backup of Database ( ${db} )" process_dbs "$subfolder" "$prefix" "$midfix" "$extension" "$rotation" "$rotation_divisor" "$rotation_string" 0 "$db" echo "----------------------------------------------------------------------" done else echo "Daily backup of databases ( ${CONFIG_db_names[@]} )." process_dbs "$subfolder" "$prefix" "$midfix" "$extension" "$rotation" "$rotation_divisor" "$rotation_string" 1 "${CONFIG_db_names[@]}" echo "----------------------------------------------------------------------" fi fi ## <- daily backup echo echo "Backup End Time `date`" echo "======================================================================" # <- BACKUP DATABASES # -> clean latest filenames [[ "${CONFIG_mysql_dump_latest_clean_filenames}" = 'yes' ]] && find "${CONFIG_backup_dir}"/latest/ -type f -exec bash -c 'remove_datetimeinfo "$@"' -- {} \; # <- clean latest filenames # -> finished information echo "Total disk space used for backup storage..." echo "Size - Location" echo `du -hsH "${CONFIG_backup_dir}"` echo echo "======================================================================" # <- finished information # -> postbackup commands if [[ "${CONFIG_postbackup}" ]];then echo "======================================================================" echo "Postbackup command output." echo source ${CONFIG_postbackup} echo echo "======================================================================" fi # <- postbackup commands if [[ -s "$log_errfile" ]];then status=1; else status=0; fi exit ${status} } # @return variable method_list_manifest_entries_array method_list_manifest_entries () { local files files_master files_manifest file db manifest_files manifest_files_db selected_dbs i z l master_flags master to_rm actions manifest_entries_corrupted=() manifest_entries_user_action_required=() files=() files_master=() files_manifest=() master_flags=0 let "filename_flag_encrypted=0x01" let "filename_flag_gz=0x02" let "filename_flag_bz2=0x04" let "filename_flag_diff=0x08" ############################################################################################################## # BEGIN __STARTUP load_default_config if [[ -r "${CONFIG_configfile}" ]]; then source "${CONFIG_configfile}"; echo "Parsed config file \"${CONFIG_configfile}\""; else let "N |= $N_config_file_missing"; fi; echo if (( $opt_flag_config_file )); then if [[ -r "${opt_config_file}" ]]; then source "${opt_config_file}"; let "N |= $N_arg_conffile_parsed"; else let "N |= $N_arg_conffile_unreadable"; fi; else let "N |= $N_too_many_args"; fi export LC_ALL=C PROGNAME=`basename $0` PATH=${PATH}:/usr/local/bin:/usr/bin:/bin:/usr/local/mysql/bin version=3.0 fields=5 # manifest fields set_datetime_vars check_dependencies # check for required programs parse_configuration # parse configuration and set variables appropriately # BEGIN __MAIN unset manifest_files manifest_files_db i db while IFS= read -r -d '' file; do db="${file#/var/backup/db/@(daily|monthly|weekly|latest)/}"; db="${db%/Manifest}"; manifest_files_db[i]="$db" manifest_files[i++]="$file" done < <(find "${CONFIG_backup_dir}"/ -type f -name 'Manifest' -print0) extended_select 0 "Databases" "${manifest_files_db[@]}" declare -a selected_dbs=("${extended_select_return[@]}") for db in "${selected_dbs[@]}"; do selected_available_files=() for ((i=0;i<"${#manifest_files_db[@]}";i++)); do if [[ "x${manifest_files_db[i]}" = "x$db" ]]; then selected_available_files[j++]="${manifest_files[i]}" fi done if (( "${#selected_available_files[@]}" > 0 )); then extended_select 1 "$db" "${selected_available_files[@]}" declare -a selected_entries=("${extended_select_return[@]}") if (( "${#selected_entries[@]}" > 0 )); then for z in "${selected_entries[@]}"; do parse_manifest "$z" list=() list_id=() for ((i=0;$i<"${#manifest_array[@]}";i=$i+$fields)); do if [[ "${manifest_array[i+3]}" != 0 ]]; then # only add differential backups list=("${list[@]}" "${manifest_array[i]}") list_id=("${list_id[@]}" "${manifest_array[i+3]}") # save rel_id, so we can retrieve the master backup file fi done if (( "${#list[@]}" > 0 )); then extended_select 1 "$z" "${list[@]}" if (( "${#extended_select_return[@]}" > 0 )); then for ((i=0;$i<"${#extended_select_return[@]}";i++)); do if get_manifest_entry_by_id "${list_id[${extended_select_return_id[i]}]}"; then files=("${files[@]}" "${extended_select_return[i]}") files_master=("${files_master[@]}" "${manifest_entry[0]}") files_manifest=("${files_manifest[@]}" "$z") else echo "no found master for id ${list_id[${extended_select_return_id[i]}]}" fi done fi fi done fi fi done # END _select_filenames declare -a method_list_manifest_entries_array=("${files[@]}") declare -a method_list_manifest_entries_array_master=("${files_master[@]}") declare -a method_list_manifest_entries_array_manifest=("${files_manifest[@]}") clear echo "You have selected the following files:" for i in "${files[@]}"; do printf '>>> %s\n' "$i"; done echo actions=('diff to full' 'remove files (also from Manifest)') extended_select 0 "Actions" "${actions[@]}" for action in "${extended_select_return[@]}"; do case "$action" in 'diff to full') for ((l=0;$l<"${#files[@]}";l++)); do # put the unpacking of the master file in here, so that in the case of multiple diffs with the same # master file don't cause the script to unpack the same master file multiple times master="${files_master[l]}" diff="${files[l]}" FileStub="${master%.@(sql|master)*}" FileExt="${master#"$FileStub"}" re=".*\.enc.*"; [[ "$FileExt" =~ $re ]] && let "master_flags|=$filename_flag_encrypted" re=".*\.gz.*"; [[ "$FileExt" =~ $re ]] && let "master_flags|=$filename_flag_gz" re=".*\.bz2.*"; [[ "$FileExt" =~ $re ]] && let "master_flags|=$filename_flag_bz2" re=".*\.diff.*"; [[ "$FileExt" =~ $re ]] && let "master_flags|=$filename_flag_diff" if (( $master_flags & $filename_flag_gz )); then declare -a testarray=("${to_rm[@]}") if ! in_array "${master%.gz}"; then gzip_compression -dc "$master" > "${master%.gz}" to_rm=("${to_rm[@]}" "${master%.gz}") fi master="${master%.gz}" elif (( $master_flags & $filename_flag_bz2 )); then declare -a testarray=("${to_rm[@]}") if ! in_array "${master%.bz2}"; then bzip2_compression -dc "$master" > "${master%.bz2}" to_rm=("${to_rm[@]}" "${master%.bz2}") fi master="${master%.bz2}" else : fi method_diff_to_full "$master" "$diff" #printf '%s\n>>> master: %s\n>>> manifest: %s\n' "${files[l]}" "${files_master[l]}" "${files_manifest[l]}" done # cleanup all unpacked master files ... the unpacked diff files are cleaned up by method_diff_to_full for i in "${to_rm[@]}"; do rm "$i"; done ;; 'remove files (also from Manifest)') for ((l=0;$l<"${#files[@]}";l++)); do if rm_manifest_entry_by_filename "${files_manifest[l]}" "${files[l]}" 1; then rm "${files[l]}" fi done ;; *) echo "Unrecognized option. This Should not happen! Error!" ;; esac done # END __MAIN } # @info: Convert a differential backup file to a full one. # @param: master_backup_file diff_backup_file method_diff_to_full() { local diff full diff_flags master_flags to_rm master="$1" diff="$2" diff_flags=0 master_flags=0 to_rm=() FileStub="${diff%.@(sql|diff)*}" FileExt="${diff#"$FileStub"}" re=".*\.enc.*"; [[ "$FileExt" =~ $re ]] && let "diff_flags|=$filename_flag_encrypted" re=".*\.gz.*"; [[ "$FileExt" =~ $re ]] && let "diff_flags|=$filename_flag_gz" re=".*\.bz2.*"; [[ "$FileExt" =~ $re ]] && let "diff_flags|=$filename_flag_bz2" re=".*\.diff.*"; [[ "$FileExt" =~ $re ]] && let "diff_flags|=$filename_flag_diff" FileStub="${master%.@(sql|master)*}" FileExt="${master#"$FileStub"}" re=".*\.enc.*"; [[ "$FileExt" =~ $re ]] && let "master_flags|=$filename_flag_encrypted" re=".*\.gz.*"; [[ "$FileExt" =~ $re ]] && let "master_flags|=$filename_flag_gz" re=".*\.bz2.*"; [[ "$FileExt" =~ $re ]] && let "master_flags|=$filename_flag_bz2" re=".*\.diff.*"; [[ "$FileExt" =~ $re ]] && let "master_flags|=$filename_flag_diff" # TODO: Differential backup with encryption is not yet implemented! if (( $diff_flags & $filename_flag_encrypted )); then : #decrypt it fi if (( $master_flags & $filename_flag_encrypted )); then : #decrypt it fi if (( $diff_flags & $filename_flag_gz )); then gzip_compression -dc "$diff" > "${diff%.gz}" to_rm=("${to_rm[@]}" "${diff%.gz}") diff="${diff%.gz}" elif (( $diff_flags & $filename_flag_bz2 )); then bzip2_compression -dc "$diff" > "${diff%.bz2}" to_rm=("${to_rm[@]}" "${diff%.bz2}") diff="${diff%.bz2}" else : fi if (( $master_flags & $filename_flag_gz )); then gzip_compression -dc "$master" > "${master%.gz}" to_rm=("${to_rm[@]}" "${master%.gz}") master="${master%.gz}" elif (( $master_flags & $filename_flag_bz2 )); then bzip2_compression -dc "$master" > "${master%.bz2}" to_rm=("${to_rm[@]}" "${master%.bz2}") master="${master%.bz2}" else : fi patch "$master" "$diff" -o "${diff/diff/sql}" # cleanup for i in "${to_rm[@]}"; do rm "$i"; done } # END _methods # BEGIN __main NO_ARGS=0 E_OPTERROR=85 if (( $# == $NO_ARGS )); then # Script invoked with no command-line args? echo "Invoking backup method."; echo; method_backup fi while getopts ":c:blh" Option do case $Option in c ) echo "Using \"$OPTARG\" as optional config file."; echo; opt_config_file="$OPTARG"; opt_flag_config_file=1;; b ) echo "MySQL backup method invoked."; echo; opt_flag_method_backup=1;; l ) echo "List manifest entries."; echo; opt_flag_list_manifest_entries=1;; h ) echo "Usage `basename $0` options -cblh" echo -e "-c CONFIG_FILE\tSpecify optional config file." echo -e "-b\tUse backup method." echo -e "-l\tList manifest entries." echo -e "-h\tShow this help." exit 0;; #n | o ) echo "Scenario #2: option -$Option- [OPTIND=${OPTIND}]";; #q ) echo "Scenario #4: option -q-\ # with argument \"$OPTARG\" [OPTIND=${OPTIND}]";; # Note that option 'q' must have an associated argument, #+ otherwise it falls through to the default. #r | s ) echo "Scenario #5: option -$Option-";; * ) echo "Unimplemented option chosen.";; # Default. esac done (( $opt_flag_method_backup )) && method_backup (( $opt_flag_list_manifest_entries )) && method_list_manifest_entries shift $(($OPTIND - 1)) # Decrements the argument pointer so it points to next argument. # $1 now references the first non-option item supplied on the command-line #+ if one exists. # For backward compatibility. If no option items are present and only one non-option item is there, we expect it # to be the optional config file and invoke the backup method. opt_flags=( "${!opt_flag_@}" ) # array of all set variables starting with opt_flag_ if (( $# == 1 )) && (( ${#opt_flags[@]} == 0 )); then opt_config_file="$1"; opt_flag_config_file=1; method_backup elif (( $# == 0 )) && (( ${#opt_flags[@]} == 0 )); then method_backup fi # END __mainautomysqlbackup/automysqlbackup.conf0000600000175000017500000002712211662460524017747 0ustar centoscentos#version=3.0_rc2 # DONT'T REMOVE THE PREVIOUS VERSION LINE! # # Uncomment to change the default values (shown after =) # WARNING: # This is not true for UMASK, CONFIG_prebackup and CONFIG_postbackup!!! # # Default values are stored in the script itself. Declarations in # /etc/automysqlbackup/automysqlbackup.conf will overwrite them. The # declarations in here will supersede all other. # Edit $PATH if mysql and mysqldump are not located in /usr/local/bin:/usr/bin:/bin:/usr/local/mysql/bin #PATH=${PATH}:FULL_PATH_TO_YOUR_DIR_CONTAINING_MYSQL:FULL_PATH_TO_YOUR_DIR_CONTAINING_MYSQLDUMP # Basic Settings # Username to access the MySQL server e.g. dbuser #CONFIG_mysql_dump_username='root' # Password to access the MySQL server e.g. password #CONFIG_mysql_dump_password='' # Host name (or IP address) of MySQL server e.g localhost #CONFIG_mysql_dump_host='localhost' # "Friendly" host name of MySQL server to be used in email log # if unset or empty (default) will use CONFIG_mysql_dump_host instead #CONFIG_mysql_dump_host_friendly='' # Backup directory location e.g /backups #CONFIG_backup_dir='/var/backup/db' # This is practically a moot point, since there is a fallback to the compression # functions without multicore support in the case that the multicore versions aren't # present in the system. Of course, if you have the latter installed, but don't want # to use them, just choose no here. # pigz -> gzip # pbzip2 -> bzip2 #CONFIG_multicore='yes' # Number of threads (= occupied cores) you want to use. You should - for the sake # of the stability of your system - not choose more than (#number of cores - 1). # Especially if the script is run in background by cron and the rest of your system # has already heavy load, setting this too high, might crash your system. Assuming # all systems have at least some sort of HyperThreading, the default is 2 threads. # If you wish to let pigz and pbzip2 autodetect or use their standards, set it to # 'auto'. #CONFIG_multicore_threads=2 # Databases to backup # List of databases for Daily/Weekly Backup e.g. ( 'DB1' 'DB2' 'DB3' ... ) # set to (), i.e. empty, if you want to backup all databases #CONFIG_db_names=() # You can use #declare -a MDBNAMES=( "${DBNAMES[@]}" 'added entry1' 'added entry2' ... ) # INSTEAD to copy the contents of $DBNAMES and add further entries (optional). # List of databases for Monthly Backups. # set to (), i.e. empty, if you want to backup all databases #CONFIG_db_month_names=() # List of DBNAMES to EXLUCDE if DBNAMES is empty, i.e. (). #CONFIG_db_exclude=( 'information_schema' ) # List of tables to exclude, in the form db_name.table_name # You may use wildcards for the table names, i.e. 'mydb.a*' selects all tables starting with an 'a'. # However we only offer the wildcard '*', matching everything that could appear, which translates to the # '%' wildcard in mysql. #CONFIG_table_exclude=() # Advanced Settings # Rotation Settings # Which day do you want monthly backups? (01 to 31) # If the chosen day is greater than the last day of the month, it will be done # on the last day of the month. # Set to 0 to disable monthly backups. #CONFIG_do_monthly="01" # Which day do you want weekly backups? (1 to 7 where 1 is Monday) # Set to 0 to disable weekly backups. #CONFIG_do_weekly="5" # Set rotation of daily backups. VALUE*24hours # If you want to keep only today's backups, you could choose 1, i.e. everything older than 24hours will be removed. #CONFIG_rotation_daily=6 # Set rotation for weekly backups. VALUE*24hours #CONFIG_rotation_weekly=35 # Set rotation for monthly backups. VALUE*24hours #CONFIG_rotation_monthly=150 # Server Connection Settings # Set the port for the mysql connection #CONFIG_mysql_dump_port=3306 # Compress communications between backup server and MySQL server? #CONFIG_mysql_dump_commcomp='no' # Use ssl encryption with mysqldump? #CONFIG_mysql_dump_usessl='yes' # For connections to localhost. Sometimes the Unix socket file must be specified. #CONFIG_mysql_dump_socket='' # The maximum size of the buffer for client/server communication. e.g. 16MB (maximum is 1GB) #CONFIG_mysql_dump_max_allowed_packet='' # This option sends a START TRANSACTION SQL statement to the server before dumping data. It is useful only with # transactional tables such as InnoDB, because then it dumps the consistent state of the database at the time # when BEGIN was issued without blocking any applications. # # When using this option, you should keep in mind that only InnoDB tables are dumped in a consistent state. For # example, any MyISAM or MEMORY tables dumped while using this option may still change state. # # While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents and # binary log coordinates), no other connection should use the following statements: ALTER TABLE, CREATE TABLE, # DROP TABLE, RENAME TABLE, TRUNCATE TABLE. A consistent read is not isolated from those statements, so use of # them on a table to be dumped can cause the SELECT that is performed by mysqldump to retrieve the table # contents to obtain incorrect contents or fail. #CONFIG_mysql_dump_single_transaction='no' # http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html#option_mysqldump_master-data # --master-data[=value] # Use this option to dump a master replication server to produce a dump file that can be used to set up another # server as a slave of the master. It causes the dump output to include a CHANGE MASTER TO statement that indicates # the binary log coordinates (file name and position) of the dumped server. These are the master server coordinates # from which the slave should start replicating after you load the dump file into the slave. # # If the option value is 2, the CHANGE MASTER TO statement is written as an SQL comment, and thus is informative only; # it has no effect when the dump file is reloaded. If the option value is 1, the statement is not written as a comment # and takes effect when the dump file is reloaded. If no option value is specified, the default value is 1. # # This option requires the RELOAD privilege and the binary log must be enabled. # # The --master-data option automatically turns off --lock-tables. It also turns on --lock-all-tables, unless # --single-transaction also is specified, in which case, a global read lock is acquired only for a short time at the # beginning of the dump (see the description for --single-transaction). In all cases, any action on logs happens at # the exact moment of the dump. # ================================================================================================================== # possible values are 1 and 2, which correspond with the values from mysqldump # VARIABLE= , i.e. no value, turns it off (default) # #CONFIG_mysql_dump_master_data= # Included stored routines (procedures and functions) for the dumped databases in the output. Use of this option # requires the SELECT privilege for the mysql.proc table. The output generated by using --routines contains # CREATE PROCEDURE and CREATE FUNCTION statements to re-create the routines. However, these statements do not # include attributes such as the routine creation and modification timestamps. This means that when the routines # are reloaded, they will be created with the timestamps equal to the reload time. # # If you require routines to be re-created with their original timestamp attributes, do not use --routines. Instead, # dump and reload the contents of the mysql.proc table directly, using a MySQL account that has appropriate privileges # for the mysql database. # # This option was added in MySQL 5.0.13. Before that, stored routines are not dumped. Routine DEFINER values are not # dumped until MySQL 5.0.20. This means that before 5.0.20, when routines are reloaded, they will be created with the # definer set to the reloading user. If you require routines to be re-created with their original definer, dump and # load the contents of the mysql.proc table directly as described earlier. # #CONFIG_mysql_dump_full_schema='yes' # Backup status of table(s) in textfile. This is very helpful when restoring backups, since it gives an idea, what changed # in the meantime. #CONFIG_mysql_dump_dbstatus='yes' # Backup dump settings # Include CREATE DATABASE in backup? #CONFIG_mysql_dump_create_database='no' # Separate backup directory and file for each DB? (yes or no) #CONFIG_mysql_dump_use_separate_dirs='yes' # Choose Compression type. (gzip or bzip2) #CONFIG_mysql_dump_compression='gzip' # Store an additional copy of the latest backup to a standard # location so it can be downloaded by third party scripts. #CONFIG_mysql_dump_latest='no' # Remove all date and time information from the filenames in the latest folder. # Runs, if activated, once after the backups are completed. Practically it just finds all files in the latest folder # and removes the date and time information from the filenames (if present). #CONFIG_mysql_dump_latest_clean_filenames='no' # Create differential backups. Master backups are created weekly at #$CONFIG_do_weekly weekday. Between master backups, # diff is used to create differential backups relative to the latest master backup. In the Manifest file, you find the # following structure # $filename md5sum $md5sum diff_id $diff_id rel_id $rel_id # where each field is separated by the tabular character '\t'. The entries with $ at the beginning mean the actual values, # while the others are just for readability. The diff_id is the id of the differential or master backup which is also in # the filename after the last _ and before the suffixes begin, i.e. .diff, .sql and extensions. It is used to relate # differential backups to master backups. The master backups have 0 as $rel_id and are thereby identifiable. Differential # backups have the id of the corresponding master backup as $rel_id. # # To ensure that master backups are kept long enough, the value of $CONFIG_rotation_daily is set to a minimum of 21 days. # #CONFIG_mysql_dump_differential='no' # Notification setup # What would you like to be mailed to you? # - log : send only log file # - files : send log file and sql files as attachments (see docs) # - stdout : will simply output the log to the screen if run manually. # - quiet : Only send logs if an error occurs to the MAILADDR. #CONFIG_mailcontent='stdout' # Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs]) #CONFIG_mail_maxattsize=4000 # Allow packing of files with tar and splitting it in pieces of CONFIG_mail_maxattsize. #CONFIG_mail_splitandtar='yes' # Use uuencode instead of mutt. WARNING: Not all email clients work well with uuencoded attachments. #CONFIG_mail_use_uuencoded_attachments='no' # Email Address to send mail to? (user@domain.com) #CONFIG_mail_address='root' # Encryption # Do you wish to encrypt your backups using openssl? #CONFIG_encrypt='no' # Choose a password to encrypt the backups. #CONFIG_encrypt_password='password0123' # Other # Backup local files, i.e. maybe you would like to backup your my.cnf (mysql server configuration), etc. # These files will be tar'ed, depending on your compression option CONFIG_mysql_dump_compression compressed and # depending on the option CONFIG_encrypt encrypted. # # Note: This could also have been accomplished with CONFIG_prebackup or CONFIG_postbackup. #CONFIG_backup_local_files=() # Command to run before backups (uncomment to use) #CONFIG_prebackup="/etc/mysql-backup-pre" # Command run after backups (uncomment to use) #CONFIG_postbackup="/etc/mysql-backup-post" # Uncomment to activate! This will give folders rwx------ # and files rw------- permissions. #umask 0077 # dry-run, i.e. show what you are gonna do without actually doing it # inactive: =0 or commented out # active: uncommented AND =1 #CONFIG_dryrun=1 automysqlbackup/CHANGELOG0000600000175000017500000002200511663513621015057 0ustar centoscentos#===================================================================== # Change Log #===================================================================== # # version 3.0_rc4 - (2011-11-24) # - Removing mkfifo commands, thereby improving portability. # - Fixing not working table exclude feature for wildcards. # version 3.0_rc3 - (2011-11-24) # - Changed code to make it more portable, thereby resolving FreeBSD issue. # version 3.0_rc2 - (2011-11-21) # - Added multicore support for bzip and gzip2. # - Fixed error in README file. # version 3.0_rc1 - (2011-11-15) # - Added differential backup method. # - Added user-friendly method to recreate full backups out of differential ones. # - Changed paramters, with which the script can be called, to make the new # methods available. # - Fixed some bugs. # version 3.0_beta2 - (2011-08-21) # - Added possibility to backup local files. # - Added full schema backup. # - Added master-data option. # - Fixed some bugs. # version 3.0_beta1 - (2011-08-15) # - REMOVED: Implementation of Variables containing full path to binaries to # avoid possibly confusion with aliases or builtins. (by Johannes Kolter) # Inside a bash script no aliases are used! This didn't make ANY sense! # Thereby resolved bug item #3074425. # - Changed some variables to be arrays, i.e. lists, and removed the ugly # sed stuff. # Fixed bug item #3169562 # - Added check for weekly and monthly backups, so that they are unique per day. # bug item #3185389 # - Changed SHEBANG to #!/usr/bin/env bash for portability reasons. # bug item #3292873 # - Changed config file structure: read /etc/automysqlbackup/mysqlbackup.conf, # if supplied read configfile parameter (no more -c or sth. like that, just # the name of the file!), # whatever isn't set yet, set in here to default values. # - bug item #3110715: create a file in /etc/cron.d/daily and call the script # from in there, i.e. place the script in /usr/local/bin # - bug item #3082899: the PATH variables are different in ssh, you have to # supply the complete path # - Fixed bug item #3064547, suggestion accepted. # - Fixed bug item #3031023, suggestion accepted. # - Fixed bug item #3030604, resolved due to design correction. # - Fixed bug item #3025849, as long as basename is in $PATH on your system. # - Fixed bug item #3030478. # - bug item #3054633: .muttrc entry save=yes will result in saving sent files! # - Feature request item #1538588. # - Feature request item #1538138. # - Feature request item #1538142. # - Feature request item #1541843 was already included. # - Feature request item #2808012. # - Feature request item #2831465. # - Feature request item #3052484. Mysqldump already has an ssl option. # - Feature request item #3190079. I hope cleaning up everything older than 24 # hours as a lower limit is good enough. # - Feature request item #3284779 was already included. See CONFIG_mysql_dump_latest. # - Feature request item #3053623. # version 2.6.0 - (2011-07-19) # - Fixed bug where files would not email correctly (Fix by Jesse Vaughan) # - Added section to encrypt .gz and .bz2 files using openssl (added by Jesse Vaughan) # version 2.5.1-01 - (2010-07-06) # - Fixed pathname bug item #3025849 (by Johannes Kolter) # version 2.5.1 - (2010-07-04) # - Added support for default and optional config file (by Johannes Kolter) # - Rotating after backup was successful whith find(1) (by Johannes Kolter) # - Implementation of Variables containing full path to binaries to # avoid possibly confusion with aliases or builtins. (by Johannes Kolter) # - Fixed bug where weekly backups were not being rotated. # Added rotation of 5 monthly backups # Now all old backups are deleted, not only the most recent one # (inspired by oleg@bintime.com) # - Use Debian special-file to access database (by Johannes Kolter) # - Fixed bug ID: 1438565 # Moved IO redirection to a place before decicions are made and actions are taken. # (inspired by Derk Bernhardt) # - Fixed bug ID: #3000316 (reported by Sascha Feldhorst) # - Fixed bug ID: #1529458 (reported by Natalie ( njwood )) # - Fixed bug ID: #1548919 (reported by Piotr Kuczynski) # version 2.5 - (2006-01-15) # Added support for setting MAXIMUM_PACKET_SIZE and CONFIG_mysql_dump_socket parameters (suggested by Yvo van Doorn) # version 2.4 - (2006-01-23) # Fixed bug where weekly backups were not being rotated. (Fix by wolf02) # Added hour an min to backup filename for the case where backups are taken multiple # times in a day. NOTE This is not complete support for mutiple executions of the script # in a single day. # Added MAILCONTENT="quiet" option, see docs for details. (requested by snowsam) # Updated path statment for compatibility with OSX. # Added "CONFIG_mysql_dump_latest" to additionally store the last backup to a standard location. (request by Grant29) # version 2.3 - (2005-11-07) # Better error handling and notification of errors (a long time coming) # Compression on Backup server to MySQL server communications. # version 2.2 - (2004-12-05) # Changed from using depricated "-N" to "--skip-column-names". # Added ability to have compressed backup's emailed out. (code from Thomas Heiserowski) # Added maximum attachment size setting. # version 2.1 - (2004-11-04) # Fixed a bug in daily rotation when not using gzip compression. (Fix by Rob Rosenfeld) # version 2.0 - (2004-07-28) # Switched to using IO redirection instead of pipeing the output to the logfile. # Added choice of compression of backups being gzip of bzip2. # Switched to using functions to facilitate more functionality. # Added option of either gzip or bzip2 compression. # version 1.10 - (2004-07-17) # Another fix for spaces in the paths (fix by Thomas von Eyben) # Fixed bug when using PREBACKUP and POSTBACKUP commands containing many arguments. # version 1.9 - (2004-05-25) # Small bug fix to handle spaces in LOGFILE path which contains spaces (reported by Thomas von Eyben) # Updated docs to mention that Log email can be sent to multiple email addresses. # version 1.8 - (2004-05-01) # Added option to make backups restorable to alternate database names # meaning that a copy of the database can be created (Based on patch by Rene Hoffmann) # Seperated options into standard and advanced. # Removed " from single file dump DBMANES because it caused an error but # this means that if DB's have spaces in the name they will not dump when CONFIG_mysql_dump_use_separate_dirs=no. # Added -p option to mkdir commands to create multiple subdirs without error. # Added disk usage and location to the bottom of the backup report. # version 1.7 - (2004-04-22) # Fixed an issue where weelky backups would only work correctly if server # locale was set to English (issue reported by Tom Ingberg) # used "eval" for "rm" commands to try and resolve rotation issues. # Changed name of status log so multiple scripts can be run at the same time. # version 1.6 - (2004-03-14) # Added PREBACKUP and POSTBACKUP command functions. (patch by markpustjens) # Added support for backing up DB's with Spaces in the name. # (patch by markpustjens) # version 1.5 - (2004-02-24) # Added the ability to exclude DB's when the "all" option is used. # (Patch by kampftitan) # version 1.4 - (2004-02-02) # Project moved to Sourceforge.net # version 1.3 - (2003-09-25) # Added support for backing up "all" databases on the server without # having to list each one seperately in the configuration. # Added DB restore instructions. # version 1.2 - (2003-03-16) # Added server name to the backup log so logs from multiple servers # can be easily identified. # version 1.1 - (2003-03-13) # Small Bug fix in monthly report. (Thanks Stoyanski) # Added option to email log to any email address. (Inspired by Stoyanski) # Changed Standard file name to .sh extention. # Option are set using yes and no rather than 1 or 0. # version 1.0 - (2003-01-30) # Added the ability to have all databases backup to a single dump # file or seperate directory and file for each database. # Output is better for log keeping. # version 0.6 - (2003-01-22) # Bug fix for daily directory (Added in version 0.5) rotation. # version 0.5 - (2003-01-20) # Added "daily" directory for daily backups for neatness (suggestion by Jason) # Added CONFIG_mysql_dump_host option to allow backing up a remote server (Suggestion by Jason) # Added "--quote-names" option to mysqldump command. # Bug fix for handling the last and first of the year week rotation. # version 0.4 - (2002-11-06) # Added the abaility for the script to create its own directory structure. # version 0.3 - (2002-10-01) # Changed Naming of Weekly backups so they will show in order. # version 0.2 - (2002-09-27) # Corrected weekly rotation logic to handle weeks 0 - 10 # version 0.1 - (2002-09-21) # Initial Releaseautomysqlbackup/install.sh0000700000175000017500000005104411666445036015664 0ustar centoscentos#!/usr/bin/env bash # # return true, if variable is set; else false isSet() { if [[ ! ${!1} && ${!1-_} ]]; then return 1; else return 0; fi } activateIO() { touch "$1" exec 6>&1 exec > "$1" } removeIO() { exec 1>&6 6>&- } upgrade_config_file () { ( # execute in subshell, so that sourced variables are only available inside () source "$1" #declare -p local temp temp=$(mktemp /tmp/tmp.XXXXXX) (( $? != 0 )) && return 1 activateIO "$temp" echo "#version=3.0_rc2" echo "# Uncomment to change the default values (shown after =)" echo "# WARNING:" echo "# This is not true for UMASK, CONFIG_prebackup and CONFIG_postbackup!!!" echo "#" echo "# Default values are stored in the script itself. Declarations in" echo "# /etc/automysqlbackup/automysqlbackup.conf will overwrite them. The" echo "# declarations in here will supersede all other." echo "" echo "# Edit \$PATH if mysql and mysqldump are not located in /usr/local/bin:/usr/bin:/bin:/usr/local/mysql/bin" echo "#PATH=\${PATH}:FULL_PATH_TO_YOUR_DIR_CONTAINING_MYSQL:FULL_PATH_TO_YOUR_DIR_CONTAINING_MYSQLDUMP" echo "" echo "# Basic Settings" echo "" echo "# Username to access the MySQL server e.g. dbuser" if isSet USERNAME; then printf '%s=%q\n' CONFIG_mysql_dump_username "${USERNAME-}" else echo "#CONFIG_mysql_dump_username='root'" fi echo "" echo "# Password to access the MySQL server e.g. password" if isSet PASSWORD; then printf '%s=%q\n' CONFIG_mysql_dump_password "${PASSWORD-}" else echo "#CONFIG_mysql_dump_password=''" fi echo "" echo "# Host name (or IP address) of MySQL server e.g localhost" if isSet DBHOST; then printf '%s=%q\n' CONFIG_mysql_dump_host "${DBHOST-}" else echo "#CONFIG_mysql_dump_host='localhost'" fi echo "" echo "# \"Friendly\" host name of MySQL server to be used in email log" echo "# if unset or empty (default) will use CONFIG_mysql_dump_host instead" if isSet CONFIG_mysql_dump_host_friendly; then printf '%s=%q\n' CONFIG_mysql_dump_host_friendly "${CONFIG_mysql_dump_host_friendly-}" else echo "#CONFIG_mysql_dump_host_friendly=''" fi echo "" echo "# Backup directory location e.g /backups" if isSet BACKUPDIR; then printf '%s=%q\n' CONFIG_backup_dir "${BACKUPDIR-}" else echo "#CONFIG_backup_dir='/var/backup/db'" fi echo "" echo "# This is practically a moot point, since there is a fallback to the compression" echo "# functions without multicore support in the case that the multicore versions aren't" echo "# present in the system. Of course, if you have the latter installed, but don't want" echo "# to use them, just choose no here." echo "# pigz -> gzip" echo "# pbzip2 -> bzip2" echo "#CONFIG_multicore='yes'" echo "" echo "# Number of threads (= occupied cores) you want to use. You should - for the sake" echo "# of the stability of your system - not choose more than (#number of cores - 1)." echo "# Especially if the script is run in background by cron and the rest of your system" echo "# has already heavy load, setting this too high, might crash your system. Assuming" echo "# all systems have at least some sort of HyperThreading, the default is 2 threads." echo "# If you wish to let pigz and pbzip2 autodetect or use their standards, set it to" echo "# 'auto'." echo "#CONFIG_multicore_threads=2" echo "" echo "# Databases to backup" echo "" echo "# List of databases for Daily/Weekly Backup e.g. ( 'DB1' 'DB2' 'DB3' ... )" echo "# set to (), i.e. empty, if you want to backup all databases" if isSet DBNAMES; then if [[ "x$DBNAMES" = "xall" ]]; then echo "#CONFIG_db_names=()" else declare -a CONFIG_db_names for i in $DBNAMES; do CONFIG_db_names=( "${CONFIG_db_names[@]}" "$i" ) done declare -p CONFIG_db_names | sed -e 's/\[[^]]*]=//g' fi else echo "#CONFIG_db_names=()" fi echo "# You can use" echo "#declare -a MDBNAMES=( \"\${DBNAMES[@]}\" 'added entry1' 'added entry2' ... )" echo "# INSTEAD to copy the contents of \$DBNAMES and add further entries (optional)." echo "" echo "# List of databases for Monthly Backups." echo "# set to (), i.e. empty, if you want to backup all databases" if isSet MDBNAMES; then if [[ "x$MDBNAMES" = "xall" ]]; then echo "#CONFIG_db_month_names=()" else declare -a CONFIG_db_month_names for i in $MDBNAMES; do CONFIG_db_month_names=( "${CONFIG_db_month_names[@]}" "$i" ) done declare -p CONFIG_db_month_names | sed -e 's/\[[^]]*]=//g' fi else echo "#CONFIG_db_month_names=()" fi echo "" echo "# List of DBNAMES to EXLUCDE if DBNAMES is empty, i.e. ()." if isSet DBEXCLUDE; then declare -a CONFIG_db_exclude for i in $DBEXCLUDE; do CONFIG_db_exclude=( "${CONFIG_db_exclude[@]}" "$i" ) done declare -p CONFIG_db_exclude | sed -e 's/\[[^]]*]=//g' else echo "#CONFIG_db_exclude=( 'information_schema' )" fi echo "" echo "# List of tables to exclude, in the form db_name.table_name" echo "#CONFIG_table_exclude=()" echo "" echo "" echo "# Advanced Settings" echo "" echo "# Rotation Settings" echo "" echo "# Which day do you want monthly backups? (01 to 31)" echo "# If the chosen day is greater than the last day of the month, it will be done" echo "# on the last day of the month." echo "# Set to 0 to disable monthly backups." echo "#CONFIG_do_monthly=\"01\"" echo "" echo "# Which day do you want weekly backups? (1 to 7 where 1 is Monday)" echo "# Set to 0 to disable weekly backups." if isSet DOWEEKLY; then printf '%s=%q\n' CONFIG_do_weekly "${DOWEEKLY-}" else echo "#CONFIG_do_weekly=\"5\"" fi echo "" echo "# Set rotation of daily backups. VALUE*24hours" echo "# If you want to keep only today's backups, you could choose 1, i.e. everything older than 24hours will be removed." echo "#CONFIG_rotation_daily=6" echo "" echo "# Set rotation for weekly backups. VALUE*24hours" echo "#CONFIG_rotation_weekly=35" echo "" echo "# Set rotation for monthly backups. VALUE*24hours" echo "#CONFIG_rotation_monthly=150" echo "" echo "" echo "# Server Connection Settings" echo "" echo "# Set the port for the mysql connection" echo "#CONFIG_mysql_dump_port=3306" echo "" echo "# Compress communications between backup server and MySQL server?" if isSet COMMCOMP; then printf '%s=%q\n' CONFIG_mysql_dump_commcomp "${COMMCOMP-}" else echo "#CONFIG_mysql_dump_commcomp='no'" fi echo "" echo "# Use ssl encryption with mysqldump?" echo "#CONFIG_mysql_dump_usessl='yes'" echo "" echo "# For connections to localhost. Sometimes the Unix socket file must be specified." if isSet SOCKET; then printf '%s=%q\n' CONFIG_mysql_dump_socket "${SOCKET-}" else echo "#CONFIG_mysql_dump_socket=''" fi echo "" echo "# The maximum size of the buffer for client/server communication. e.g. 16MB (maximum is 1GB)" if isSet MAX_ALLOWED_PACKET; then printf '%s=%q\n' CONFIG_mysql_dump_max_allowed_packet "${MAX_ALLOWED_PACKET-}" else echo "#CONFIG_mysql_dump_max_allowed_packet=''" fi echo "" echo "# This option sends a START TRANSACTION SQL statement to the server before dumping data. It is useful only with" echo "# transactional tables such as InnoDB, because then it dumps the consistent state of the database at the time" echo "# when BEGIN was issued without blocking any applications." echo "#" echo "# When using this option, you should keep in mind that only InnoDB tables are dumped in a consistent state. For" echo "# example, any MyISAM or MEMORY tables dumped while using this option may still change state." echo "#" echo "# While a --single-transaction dump is in process, to ensure a valid dump file (correct table contents and" echo "# binary log coordinates), no other connection should use the following statements: ALTER TABLE, CREATE TABLE," echo "# DROP TABLE, RENAME TABLE, TRUNCATE TABLE. A consistent read is not isolated from those statements, so use of" echo "# them on a table to be dumped can cause the SELECT that is performed by mysqldump to retrieve the table" echo "# contents to obtain incorrect contents or fail." echo "#CONFIG_mysql_dump_single_transaction='no'" echo "" echo "# http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html#option_mysqldump_master-data" echo "# --master-data[=value] " echo "# Use this option to dump a master replication server to produce a dump file that can be used to set up another" echo "# server as a slave of the master. It causes the dump output to include a CHANGE MASTER TO statement that indicates" echo "# the binary log coordinates (file name and position) of the dumped server. These are the master server coordinates" echo "# from which the slave should start replicating after you load the dump file into the slave." echo "#" echo "# If the option value is 2, the CHANGE MASTER TO statement is written as an SQL comment, and thus is informative only;" echo "# it has no effect when the dump file is reloaded. If the option value is 1, the statement is not written as a comment" echo "# and takes effect when the dump file is reloaded. If no option value is specified, the default value is 1." echo "#" echo "# This option requires the RELOAD privilege and the binary log must be enabled. " echo "#" echo "# The --master-data option automatically turns off --lock-tables. It also turns on --lock-all-tables, unless" echo "# --single-transaction also is specified, in which case, a global read lock is acquired only for a short time at the" echo "# beginning of the dump (see the description for --single-transaction). In all cases, any action on logs happens at" echo "# the exact moment of the dump." echo "# ==================================================================================================================" echo "# possible values are 1 and 2, which correspond with the values from mysqldump" echo "# VARIABLE= , i.e. no value, turns it off (default)" echo "#" echo "#CONFIG_mysql_dump_master_data=" echo "" echo "# Included stored routines (procedures and functions) for the dumped databases in the output. Use of this option" echo "# requires the SELECT privilege for the mysql.proc table. The output generated by using --routines contains" echo "# CREATE PROCEDURE and CREATE FUNCTION statements to re-create the routines. However, these statements do not" echo "# include attributes such as the routine creation and modification timestamps. This means that when the routines" echo "# are reloaded, they will be created with the timestamps equal to the reload time." echo "#" echo "# If you require routines to be re-created with their original timestamp attributes, do not use --routines. Instead," echo "# dump and reload the contents of the mysql.proc table directly, using a MySQL account that has appropriate privileges" echo "# for the mysql database. " echo "#" echo "# This option was added in MySQL 5.0.13. Before that, stored routines are not dumped. Routine DEFINER values are not" echo "# dumped until MySQL 5.0.20. This means that before 5.0.20, when routines are reloaded, they will be created with the" echo "# definer set to the reloading user. If you require routines to be re-created with their original definer, dump and" echo "# load the contents of the mysql.proc table directly as described earlier." echo "#" echo "#CONFIG_mysql_dump_full_schema='yes'" echo "" echo "# Backup dump settings" echo "" echo "# Include CREATE DATABASE in backup?" if isSet CREATE_DATABASE; then printf '%s=%q\n' CONFIG_mysql_dump_create_database "${CREATE_DATABASE-}" else echo "#CONFIG_mysql_dump_create_database='no'" fi echo "" echo "# Separate backup directory and file for each DB? (yes or no)" if isSet SEPDIR; then printf '%s=%q\n' CONFIG_mysql_dump_use_separate_dirs "${SEPDIR-}" else echo "#CONFIG_mysql_dump_use_separate_dirs='yes'" fi echo "" echo "# Choose Compression type. (gzip or bzip2)" if isSet COMP; then printf '%s=%q\n' CONFIG_mysql_dump_compression "${COMP-}" else echo "#CONFIG_mysql_dump_compression='gzip'" fi echo "" echo "# Store an additional copy of the latest backup to a standard" echo "# location so it can be downloaded by third party scripts." if isSet LATEST; then printf '%s=%q\n' CONFIG_mysql_dump_latest "${LATEST-}" else echo "#CONFIG_mysql_dump_latest='no'" fi echo "" echo "# Remove all date and time information from the filenames in the latest folder." echo "# Runs, if activated, once after the backups are completed. Practically it just finds all files in the latest folder" echo "# and removes the date and time information from the filenames (if present)." echo "#CONFIG_mysql_dump_latest_clean_filenames='no'" echo "" echo "# Notification setup" echo "" echo "# What would you like to be mailed to you?" echo "# - log : send only log file" echo "# - files : send log file and sql files as attachments (see docs)" echo "# - stdout : will simply output the log to the screen if run manually." echo "# - quiet : Only send logs if an error occurs to the MAILADDR." if isSet MAILCONTENT; then printf '%s=%q\n' CONFIG_mailcontent "${MAILCONTENT-}" else echo "#CONFIG_mailcontent='stdout'" fi echo "" echo "# Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs])" if isSet MAXATTSIZE; then printf '%s=%q\n' CONFIG_mail_maxattsize "${MAXATTSIZE-}" else echo "#CONFIG_mail_maxattsize=4000" fi echo "" echo "# Email Address to send mail to? (user@domain.com)" if isSet MAILADDR; then printf '%s=%q\n' CONFIG_mail_address "${MAILADDR-}" else echo "#CONFIG_mail_address='root'" fi echo "" echo '# Create differential backups. Master backups are created weekly at #$CONFIG_do_weekly weekday. Between master backups,' echo "# diff is used to create differential backups relative to the latest master backup. In the Manifest file, you find the" echo "# following structure" echo '# $filename md5sum $md5sum diff_id $diff_id rel_id $rel_id' echo "# where each field is separated by the tabular character '\t'. The entries with $ at the beginning mean the actual values," echo "# while the others are just for readability. The diff_id is the id of the differential or master backup which is also in" echo "# the filename after the last _ and before the suffixes begin, i.e. .diff, .sql and extensions. It is used to relate" echo '# differential backups to master backups. The master backups have 0 as $rel_id and are thereby identifiable. Differential' echo '# backups have the id of the corresponding master backup as $rel_id.' echo "#" echo '# To ensure that master backups are kept long enough, the value of $CONFIG_rotation_daily is set to a minimum of 21 days.' echo "#" echo "#CONFIG_mysql_dump_differential='no'" echo "" echo "# Encryption" echo "" echo "# Do you wish to encrypt your backups using openssl?" echo "#CONFIG_encrypt='no'" echo "" echo "# Choose a password to encrypt the backups." echo "#CONFIG_encrypt_password='password0123'" echo "" echo "# Other" echo "" echo "# Backup local files, i.e. maybe you would like to backup your my.cnf (mysql server configuration), etc." echo "# These files will be tar'ed, depending on your compression option CONFIG_mysql_dump_compression compressed and" echo "# depending on the option CONFIG_encrypt encrypted." echo "#" echo "# Note: This could also have been accomplished with CONFIG_prebackup or CONFIG_postbackup." echo "#CONFIG_backup_local_files=()" echo "" echo "# Command to run before backups (uncomment to use)" if isSet PREBACKUP; then printf '%s=%q\n' CONFIG_prebackup "${PREBACKUP-}" else echo "#CONFIG_prebackup=\"/etc/mysql-backup-pre\"" fi echo "" echo "# Command run after backups (uncomment to use)" if isSet POSTBACKUP; then printf '%s=%q\n' CONFIG_postbackup "${POSTBACKUP-}" else echo "#CONFIG_postbackup=\"/etc/mysql-backup-post\"" fi echo "" echo "# Uncomment to activate! This will give folders rwx------" echo "# and files rw------- permissions." echo "#umask 0077" echo "" echo "# dry-run, i.e. show what you are gonna do without actually doing it" echo "# inactive: =0 or commented out" echo "# active: uncommented AND =1" echo "#CONFIG_dryrun=1" removeIO mv "$temp" "${1}_converted" return 0 ) } parse_config_file () { printf 'Found config file %s. ' "$1" if head -n1 "$1" | egrep -o 'version=.*' >& /dev/null; then version=`head -n1 "$1" | egrep -o 'version=.*' | awk -F"=" '{print $2}'` if [[ "$version" =~ 3.* ]]; then printf 'Version 3.* determined. No conversion necessary.\n' else printf 'Unknown version. Can not convert it. You have to convert it manually.\n' fi else printf 'No version information on first line of config file. Assuming the version is <3.\n' while true; do read -p "Convert? [Y/n] " yn [[ "x$yn" = "x" ]] && { upgrade_config_file "$1" || echo "Failed to convert."; break; } case $yn in [Yy]* ) upgrade_config_file "$1" || echo "Failed to convert."; break;; [Nn]* ) break;; * ) echo "Please answer yes or no.";; esac done fi } #precheck echo "### Checking archive files for existence, readability and integrity." echo precheck_files=( automysqlbackup 447c33d2546181d07d0c0d69d76b189b automysqlbackup.conf d525efa3da15ce9fea96893e5a8ce6d5 README b17740fcd3a5f8579b907a42249a83cd LICENSE 39bba7d2cf0ba1036f2a6e2be52fe3f0 ) n=$(( ${#precheck_files[@]}/2 )) i=0 while [ $i -lt $n ]; do printf "${precheck_files[$((2*$i))]} ... " if [ -r "${precheck_files[$((2*$i))]}" ]; then printf "exists and is readable ... " else printf "failed\n" exit 1 fi if echo "${precheck_files[$((2*$i+1))]} ${precheck_files[$((2*$i))]}" | md5sum --check >/dev/null 2>&1; then printf "md5sum okay :)\n" else printf "md5sum failed :(\n" exit 1 fi let i+=1 done echo printf 'Select the global configuration directory [/etc/automysqlbackup]: ' read configdir configdir="${configdir%/}" # strip trailing slash if there [[ "x$configdir" = "x" ]] && configdir='/etc/automysqlbackup' printf 'Select directory for the executable [/usr/local/bin]: ' read bindir bindir="${bindir%/}" # strip trailing slash if there [[ "x$bindir" = "x" ]] && bindir='/usr/local/bin' #create global config directory echo "### Creating global configuration directory ${configdir}:" echo if [ -d "${configdir}" ]; then echo "exists already ... searching for config files:" for i in "${configdir}"/*.conf; do [[ "x$(basename $i)" = "xautomysqlbackup.conf" ]] && continue parse_config_file "$i" done else if mkdir "${configdir}" >/dev/null 2>&1; then #testing for permissions if [ -r "${configdir}" -a -x "${configdir}" ]; then printf "success\n" else printf "directory successfully created but has wrong permissions, trying to correct ... " if chmod +rx "${configdir}" >/dev/null 2>&1; then printf "corrected\n" else printf "failed. Aborting. Make sure you run the script with appropriate permissions.\n" fi fi else printf "failed ... check permissions.\n" fi fi echo #copying files echo "### Copying files." echo cp -i automysqlbackup.conf LICENSE README "${configdir}"/ cp -i automysqlbackup.conf "${configdir}"/myserver.conf cp -i automysqlbackup "${bindir}"/ [[ -f "${bindir}"/automysqlbackup ]] && [[ -x "${bindir}"/automysqlbackup ]] || chmod +x "${bindir}"/automysqlbackup || echo " failed - make sure you make the program executable, i.e. run 'chmod +x ${bindir}/automysqlbackup'" echo if echo $PATH | grep "${bindir}" >/dev/null 2>&1; then printf "if you are running automysqlbackup under the same user as you run this install script,\nyou should be able to access it by running 'automysqlbackup' from the command line.\n" printf "if not, you have to check if 'echo \$PATH' has ${bindir} in it\n" printf "\nSetup Complete!\n" else printf "if running under the current user, you have to use the full path ${bindir}/automysqlbackup since /usr/local/bin is not in 'echo \$PATH'\n" printf "\nSetup Complete!\n" fiautomysqlbackup/README0000600000175000017500000001555011661730045014533 0ustar centoscentosAutomysqlBackup ------------------------- .. INDEX ------------------------- Disclaimer Install Usage Configuration Options Encryption Backup rotation Restoring .. DISCLAIMER ------------------------- I take no resposibility for any data loss or corruption when using this script. This script will not help in the event of a hard drive crash. If a copy of the backup has not been stored offline or on another PC. You should copy your backups offline regularly for best protection. Happy backing up... .. INSTALL ------------------------- Extract the package into a directory. If you are reading this you have probably done this already. To install the Automysqlbackup the easy way. 1. Run the install.sh script. 2. Edit the /etc/automysqlbackup/myserver.conf file to customise your settings. 3. See usage section. To install it manually (the hard way). 1. Create the /etc/automysqlbackup directory. 2. Copy in the automysqlbackup.conf file. 3. Copy the automysqlbackup file to /usr/local/bin and make executable. 4. cp /etc/automysqlbackup/automysqlbackup.conf /etc/automysqlbackup/myserver.conf 5. Edit the /etc/automysqlbackup/myserver.conf file to customise your settings. 6. See usage section. .. USAGE ------------------------- Automysqlbackup can be run a number of ways, you can choose which is best for you. 1. Create a script as below called runmysqlbackup using the lines below: #~~~~ Copy From Below Here ~~~~ #!/bin/sh /usr/local/bin/automysqlbackup /etc/automysqlbackup/myserver.conf chown root.root /var/backup/db* -R find /var/backup/db* -type f -exec chmod 400 {} \; find /var/backup/db* -type d -exec chmod 700 {} \; #~~~~~ Copy To Above Here ~~~~ 2. Save it to a suitable location or copy it to your /etc/cron.daily folder. 3. Make it executable, i.e. chmod +x /etc/cron.daily/runmysqlbackup. The backup can be run from the command line simply by running the following command. automysqlbackup /etc/automysqlbackup/myserver.conf If you don't supply an argument for automysqlbackup, the default configuration in the program automysqlbackup will be used unless a global file CONFIG_configfile="/etc/automysqlbackup/automysqlbackup.conf" exists. You can just copy the supplied automysqlbackup.conf as many times as you want and use for separate configurations, i.e. for example different mysql servers. !!! NEW !!! ---------- As of version 3.0 we have added differential backups using the program diff. In an effort to make the reconstruction of the full archives more user friendly, we added new functionality to the script. Therefore, while preserving the old syntax, we created options for the script, so that the new functions can be accessed. Usage automysqlbackup options -cblh -c CONFIG_FILE Specify optional config file. -b Use backup method. -l List manifest entries. -h Show this help. If you use these options, you have to specify everything according to them and can't mix the old syntax with the new one. Example: before (still valid!): >> automysqlbackup "myconfig.conf" now: >> automysqlbackup -c "myconfig.conf" -b which is equivalent to >> automysqlbackup -bc "myconfig.conf" or in English: The order of the options doesn't matter, however those options expecting arguments, have to be placed right before the argument (as seen above). The option '-l' (List manifest entries) finds all Manifest files in your configuration directory (you need to specify your optional config file - otherwise a fallback will be used: global config file -> program internal default options). It then filters from which databases these are and presents you with a list (you can select more than one!) of them. Once you have chosen, you will be given a list of Manifest files, from which you choose again and after that from which you choose differential files. When you have completed all your selections, a list of selected differential files will be shown. You may then choose what you want to be done with/to those files. At the moment the options are: - create full backup out of differential one - remove the differential backup and its Manifest entry. .. CONFIGURATION OPTIONS ------------------------- !! "automysqlbackup" program contains a default configuration that should not be changed: The global config file which overwrites the default configuration is located here "/etc/automysqlbackup/automysqlbackup.conf" by default. Please take a look at the supplied "automysqlbackup.conf" for information about the configuration options. Default configuration CONFIG_configfile="/etc/automysqlbackup/automysqlbackup.conf" CONFIG_backup_dir='/var/backup/db' CONFIG_do_monthly="01" CONFIG_do_weekly="5" CONFIG_rotation_daily=6 CONFIG_rotation_weekly=35 CONFIG_rotation_monthly=150 CONFIG_mysql_dump_usessl='yes' CONFIG_mysql_dump_username='root' CONFIG_mysql_dump_password='' CONFIG_mysql_dump_host='localhost' CONFIG_mysql_dump_socket='' CONFIG_mysql_dump_create_database='no' CONFIG_mysql_dump_use_separate_dirs='yes' CONFIG_mysql_dump_compression='gzip' CONFIG_mysql_dump_commcomp='no' CONFIG_mysql_dump_latest='no' CONFIG_mysql_dump_max_allowed_packet='' CONFIG_db_names=() CONFIG_db_month_names=() CONFIG_db_exclude=( 'information_schema' ) CONFIG_mailcontent='log' CONFIG_mail_maxattsize=4000 CONFIG_mail_address='root' CONFIG_encrypt='no' CONFIG_encrypt_password='password0123' !! automysqlbackup (the shell program) accepts one parameter, the filename of a configuration file. The entries in there will supersede all others. Please take a look at the supplied "automysqlbackup.conf" for information about the configuration options. .. ENCRYPTION ------------------------- To decrypt run (replace bz2 with gz if using gzip): openssl enc -aes-256-cbc -d -in encrypted_file_name(ex: *.enc.bz2) -out outputfilename.bz2 -pass pass:PASSWORD-USED-TO-ENCRYPT .. BACKUP ROTATION ------------------------- Daily Backups are rotated weekly. Weekly Backups are run on fridays, unless otherwise specified via CONFIG_do_weekly. Weekly Backups are rotated on a 5 week cycle, unless otherwise specified via CONFIG_rotation_weekly. Monthly Backups are run on the 1st of the month, unless otherwise specified via CONFIG_do_monthly. Monthly Backups are rotated on a 5 month cycle, unless otherwise specified via CONFIG_rotation_monthly. Suggestion: It may be a good idea to copy monthly backups offline or to another server. .. RESTORING ------------------------- Firstly you will need to uncompress the backup file and decrypt it if encryption was used (see encryption section). eg. gunzip file.gz (or bunzip2 file.bz2) Next you will need to use the mysql client to restore the DB from the sql file. eg. mysql --user=username --pass=password --host=dbserver database < /path/file.sql or mysql --user=username --pass=password --host=dbserver -e "source /path/file.sql" database NOTE: Make sure you use "<" and not ">" in the above command because you are piping the file.sql to mysql and not the other way around. automysqlbackup/LICENSE0000600000175000017500000004325311626701310014653 0ustar centoscentos GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License.