# Create a database backup
# Usage: backup.sh <host> <port> <database> <backup_dir> <backup_file> <user> <password>

# Check if the host is provided
if [ -z "$1" ]; then
  echo "Host is not provided"
  exit 1
fi

# Check if the port is provided
if [ -z "$2" ]; then
  echo "Port is not provided"
  exit 1
fi

# Check if the database is provided
if [ -z "$3" ]; then
  echo "Database is not provided"
  exit 1
fi

# Check if the backup directory is provided
if [ -z "$4" ]; then
  echo "Backup directory is not provided"
  exit 1
fi

# Check if the backup file is provided
if [ -z "$5" ]; then
  echo "Backup file is not provided"
  exit 1
fi

# Check if the user is provided
if [ -z "$6" ]; then
  echo "User is not provided"
  exit 1
fi

# Check if the password is provided
if [ -z "$7" ]; then
  echo "Password is not provided"
  exit 1
fi

# Check if the backup directory exists
if [ ! -d "$4" ]; then
  echo "Backup directory $4 does not exist"
  exit 1
fi

# Check if the backup file exists
if [ -f "$4"/"$5" ]; then
  echo "Backup file $4/$5 already exists"
  exit 1
fi

# Check if the database exists
if ! mysql --host "$1" -P "$2" -u "$6" -p "$7" -e "use $3"; then
  echo "Database $3 does not exist"
  exit 1
fi

# Create a backup file
mysqldump --host "$1" -P "$2" -u "$6" -p "$7" "$3" > "$4"/"$5"

# Check if the backup file is created
if [ ! -f "$4"/"$5" ]; then
  echo "Backup file is not created"
  exit 1
fi

# Print the backup file path
echo "Backup file is created: $4/$5"