0

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 Answer 1

0
#!/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
2
  • Dear Sir, is it possible i just execute the script and running those process without calling function? Glad for help and TQ so much Sep 23, 2016 at 2:24
  • Add these two lines at the bottom of your code to execute the functions when the code is executed: backup_all_dump [[ line break ]] backup_all_users
    – ackwdw123
    Sep 27, 2016 at 11:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .