12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #!/bin/bash
- backup_parent_dir="/var/backups/mysql"
- mysql_user="root"
- mysql_password="ROOTPASSWD"
- echo exit | mysql --user=${mysql_user} --password=${mysql_password} -B 2>/dev/null
- if [ "$?" -gt 0 ]; then
- echo "MySQL ${mysql_user} password incorrect"
- exit 1
- else
- echo "MySQL ${mysql_user} password correct."
- fi
- backup_date=`date +%Y_%m_%d_%H_%M`
- backup_dir="${backup_parent_dir}/${backup_date}"
- echo "Backup directory: ${backup_dir}"
- mkdir -p "${backup_dir}"
- chmod 644 "${backup_dir}"
- mysql_databases=`echo 'show databases' | mysql --user=${mysql_user} --password=${mysql_password} -B | sed /^Database$/d`
- for database in $mysql_databases
- do
- if [ "${database}" == "information_schema" ] || [ "${database}" == "performance_schema" ]; then
- additional_mysqldump_params="--skip-lock-tables --compact --no-autocommit "
- else
- additional_mysqldump_params=""
- fi
- echo "Creating backup of \"${database}\" database"
- mysqldump ${additional_mysqldump_params} --user=${mysql_user} --password=${mysql_password} ${database} | gzip > "${backup_dir}/${database}.sql.gz"
- chmod 644 "${backup_dir}/${database}.sql.gz"
- done
- find $backup_parent_dir/ -type f -mtime +60 -delete;
- find $backup_parent_dir/ -type d -empty -delete;
|