#!/bin/bash

host="localhost"
username="root"
pass="root"
database="book"

backup_dir="/root/backup"
mkdir -p $backup_dir

date_stamp=$(date +%Y%m%d_%H%M%S)
backup_file="$backup_dir/$database-$date_stamp.sql"

mysqldump --single-transaction --host="$host" --user="$username" --password="$pass" "$database" > "$backup_file"

if [ $? -eq 0 ];then
  echo "successfully: $backup_file"
else
  echo "failed"
fi

max_age=7
find "$backup_dir" -name "$database-*.sql" -type f -mtime +$max_age -delete
echo "delete successfully."