I'm a newbie on bash script, what i'm try to do is to make a bash script for running 2 kind of process of backup for mysql, first is to dump all mysql database into single dump file and another is to backup each database into single file, i search around to get some tips to make a bash script and the result as below, but seem is not working as my expectation, i try to execute the script but nothing happen, glad some one help me to fix it.
#!/bin/bash TIMESTAMP=$(date "+%d-%m-%Y") BACKUP_DIR="/nas/mysql" MYSQL=/usr/bin/mysql MYSQLDUMP=/usr/bin/mysqldump backup_all_dump(){ find /nas/mysql/all_dump -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \; mkdir -p "$BACKUP_DIR/all_dump/$TIMESTAMP" $MYSQLDUMP --force --events --all-databases | gzip > "/$BACKUP_DIR/all_dump/$TIMESTAMP/dump.sql.gz" } backup_all_users(){ find /nas/mysql/all_users -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \; mkdir -p "$BACKUP_DIR/all_users/$TIMESTAMP" databases=`$MYSQL -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"` for db in $databases; do $MYSQLDUMP --force --opt --databases "$db" | gzip > "$BACKUP_DIR/all_users/$TIMESTAMP/$db.gz" done }
答え1
#!/bin/bash TIMESTAMP=$(date "+%d-%m-%Y") BACKUP_DIR="/nas/mysql" MYSQL=/usr/bin/mysql MYSQLDUMP=/usr/bin/mysqldump backup_all_dump(){ find /nas/mysql/all_dump -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \; mkdir -p "$BACKUP_DIR/all_dump/$TIMESTAMP" $MYSQLDUMP --force --events --all-databases | gzip > "/$BACKUP_DIR/all_dump/$TIMESTAMP/dump.sql.gz" } backup_all_users(){ find /nas/mysql/all_users -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \; mkdir -p "$BACKUP_DIR/all_users/$TIMESTAMP" databases=`$MYSQL -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"` for db in $databases; do $MYSQLDUMP --force --opt --databases "$db" | gzip > "$BACKUP_DIR/all_users/$TIMESTAMP/$db.gz" done }
You need to call the function
backup_all_dump
You need to call the function
backup_all_users