unix mysql 백업 (shell script)
2018.10.14 08:39
#!/bin/bash # Simple script to backup MySQL databases # Parent backup directory backup_parent_dir="/root/backup" # MySQL settings mysql_user="root" mysql_password="" # Read MySQL password from stdin if empty if [ -z "${mysql_password}" ]; then echo -n "Enter MySQL ${mysql_user} password: " read -s mysql_password echo fi # Check MySQL password 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 # Create backup directory and set permissions #backup_date=`date +%Y_%m_%d_%H_%M` backup_date=`date +%Y%m%d-%H%M` #backup_dir="${backup_parent_dir}/${backup_date}" backup_dir="${backup_parent_dir}" echo "Backup directory: ${backup_dir}" mkdir -p "${backup_dir}" chmod 700 "${backup_dir}" # Get MySQL databases mysql_databases=`echo 'show databases' | mysql --user=${mysql_user} --password=${mysql_password} -B | sed /^Database$/d` # Backup and compress each database for database in $mysql_databases do if [ "${database}" == "information_schema" ] || [ "${database}" == "performance_schema" ] || [ "${database}" == "mysql" ]; then # 기본 경로는 백업하지 않기로 additional_mysqldump_params="--skip-lock-tables" continue 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}/${backup_date}_${database}.sql.gz" chmod 600 "${backup_dir}/${backup_date}_${database}.sql.gz" done
굿
링크 원본: {백업경로}/{백업날짜}/{DB}.gz
내 수정본: {백업경로}/{백업날짜}.sql.gz
댓글 0
번호 | 제목 | 날짜 | 조회 수 |
---|---|---|---|
81 | windows hardware encrypt ssd | 2024.01.23 | 610 |
80 | php 에서 비동기 처리하는 방법 | 2023.06.22 | 782 |
79 | mysql DELETE 쿼리 사용할 때 실제로 rows 삭제가 발생했는지 알 수 있는가? | 2023.06.21 | 3675 |
78 | gearman 은 무엇인가? | 2023.06.21 | 658 |
77 | apache2 AllowOverride config | 2023.05.23 | 729 |
76 | mysqldump from remote db to my local server. | 2023.05.04 | 540 |
75 | To download large files with rsync command. | 2023.05.04 | 152 |
74 | The best configuration of php7.4-fpm / 8GB | 2023.05.04 | 206 |
73 | The best configuration of php7.4-fpm / 1.5GB | 2023.05.04 | 145 |
72 | net::ERR_CONNECTION_REFUSED | 2021.10.11 | 0 |
71 | php-fpm tcp to socket | 2020.01.25 | 0 |
70 | netdata 이용한 centos 모니터링 | 2020.01.25 | 2833 |
69 | vmware with centos 7 | 2019.08.23 | 0 |
68 | xe 에디터 컴포넌트 트리거 제거 | 2019.05.16 | 10046 |
67 | my.cnf | 2019.04.11 | 1 |
66 | certbot apache multiple domain | 2019.04.10 | 1568 |
65 | yum install latest redis | 2019.03.27 | 1825 |
64 | yum install php-apcu | 2019.03.12 | 1714 |
63 | 웹 백업 (쉘 스크립트) | 2018.10.14 | 1332 |
» | mysql 백업 (shell script) | 2018.10.14 | 801 |