Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

I'd do this:

#!/bin/bash

backuppath=/var/mysql_backup
mkdir -p "$backuppath"

shopt -s nullglob

# Remove old compressed archive
backups=( "$backuppath"/*.gz )
if [[ ${#backups[@]} -gt 0 ]]; then
    stat -c "%Y %n" "${backups[@]}" |
      sort -rn |
      sed -e '1,7d' -e 's/^[0-9]\+ //' |
      xargs -r rm
fi

# Compress all the logs
logs=( "$backuppath"/*.sql )
if [[ ${#logs[@]} -gt 0 ]]; then
    gzip "${logs[@]}"
fi

/usr/bin/mysqldump -u db_user -p'password' --single-transaction --all-databases \
    > "$backuppath/"$(date "+%Y.%m.%d-%H.%M.%S")_dump.sql

Notes:

  • don't use ALLCAPSVARS -- leave those for the shell. I've seen people accidentally do PATH=... and then wonder why their script is broken.
  • don't parse ls -- I'm using arrays to hold the results of the glob expansion, and checking how many elements are in the array.
  • always quote variables, except when you know exactly why not. See http://unix.stackexchange.com/q/171346/4667https://unix.stackexchange.com/q/171346/4667
  • I'm assuming you want to compress any log.sql file, not just the first found
  • I'm assuming you want to keep 7 compressed logs and remove any older ones.
  • I'm assuming you have the GNU coreutils (xargs -r, those stat options)

I'd do this:

#!/bin/bash

backuppath=/var/mysql_backup
mkdir -p "$backuppath"

shopt -s nullglob

# Remove old compressed archive
backups=( "$backuppath"/*.gz )
if [[ ${#backups[@]} -gt 0 ]]; then
    stat -c "%Y %n" "${backups[@]}" |
      sort -rn |
      sed -e '1,7d' -e 's/^[0-9]\+ //' |
      xargs -r rm
fi

# Compress all the logs
logs=( "$backuppath"/*.sql )
if [[ ${#logs[@]} -gt 0 ]]; then
    gzip "${logs[@]}"
fi

/usr/bin/mysqldump -u db_user -p'password' --single-transaction --all-databases \
    > "$backuppath/"$(date "+%Y.%m.%d-%H.%M.%S")_dump.sql

Notes:

  • don't use ALLCAPSVARS -- leave those for the shell. I've seen people accidentally do PATH=... and then wonder why their script is broken.
  • don't parse ls -- I'm using arrays to hold the results of the glob expansion, and checking how many elements are in the array.
  • always quote variables, except when you know exactly why not. See http://unix.stackexchange.com/q/171346/4667
  • I'm assuming you want to compress any log.sql file, not just the first found
  • I'm assuming you want to keep 7 compressed logs and remove any older ones.
  • I'm assuming you have the GNU coreutils (xargs -r, those stat options)

I'd do this:

#!/bin/bash

backuppath=/var/mysql_backup
mkdir -p "$backuppath"

shopt -s nullglob

# Remove old compressed archive
backups=( "$backuppath"/*.gz )
if [[ ${#backups[@]} -gt 0 ]]; then
    stat -c "%Y %n" "${backups[@]}" |
      sort -rn |
      sed -e '1,7d' -e 's/^[0-9]\+ //' |
      xargs -r rm
fi

# Compress all the logs
logs=( "$backuppath"/*.sql )
if [[ ${#logs[@]} -gt 0 ]]; then
    gzip "${logs[@]}"
fi

/usr/bin/mysqldump -u db_user -p'password' --single-transaction --all-databases \
    > "$backuppath/"$(date "+%Y.%m.%d-%H.%M.%S")_dump.sql

Notes:

  • don't use ALLCAPSVARS -- leave those for the shell. I've seen people accidentally do PATH=... and then wonder why their script is broken.
  • don't parse ls -- I'm using arrays to hold the results of the glob expansion, and checking how many elements are in the array.
  • always quote variables, except when you know exactly why not. See https://unix.stackexchange.com/q/171346/4667
  • I'm assuming you want to compress any log.sql file, not just the first found
  • I'm assuming you want to keep 7 compressed logs and remove any older ones.
  • I'm assuming you have the GNU coreutils (xargs -r, those stat options)

I'd do this:

#!/bin/bash

backuppath=/var/mysql_backup
mkdir -p "$backuppath"

shopt -s nullglob

# Remove old compressed archive
backups=( "$backuppath"/*.gz )
if [[ ${#backups[@]} -gt 0 ]]; then
    stat -c "%Y %n" "${backups[@]}" |
      sort -rn |
      sed -e '1,7d' -e 's/^[0-9]\+ //' |
      xargs -r rm
fi

# compressCompress all the logs
logs=( "$backuppath"/*.sql )
if [[ ${#logs[@]} -gt 0 ]]; then
    gzip "${logs[@]}"
fi
 

/usr/bin/mysqldump -u db_user -p'password' --single-transaction --all-databases \
    > "$backuppath/$"$(date "+%Y.%m.%d-%H.%M.%S")_dump.sql

Notes:

  • don't use ALLCAPSVARS -- leave those for the shell. I've seen people accidentally do PATH=... and then wonder why their script is broken.
  • don't parse ls -- I'm using arrays to hold the results of the glob expansion, and checking how many elements are in the array.
  • always quote variables, except when you know exactly why not. See http://unix.stackexchange.com/q/171346/4667
  • I'm assuming you want to compress any log.sql file, not just the first found
  • I'm assuming you want to keep 7 compressed logs and remove any older ones.
  • I'm assuming you have the GNU coreutils (xargs -r, those stat options)

I'd do this:

#!/bin/bash

backuppath=/var/mysql_backup
mkdir -p "$backuppath"

shopt -s nullglob

# Remove old compressed archive
backups=( "$backuppath"/*.gz )
if [[ ${#backups[@]} -gt 0 ]]; then
    stat -c "%Y %n" "${backups[@]}" |
      sort -rn |
      sed -e '1,7d' -e 's/^[0-9]\+ //' |
      xargs -r rm
fi

# compress all logs
logs=( "$backuppath"/*.sql )
if [[ ${#logs[@]} -gt 0 ]]; then
    gzip "${logs[@]}"
fi
 

/usr/bin/mysqldump -u db_user -p'password' --single-transaction --all-databases \
    > "$backuppath/$(date "+%Y.%m.%d-%H.%M.%S")_dump.sql

Notes:

  • don't use ALLCAPSVARS -- leave those for the shell. I've seen people accidentally do PATH=... and then wonder why their script is broken.
  • don't parse ls -- I'm using arrays to hold the results of the glob expansion, and checking how many elements are in the array.
  • always quote variables, except when you know exactly why not. See http://unix.stackexchange.com/q/171346/4667
  • I'm assuming you want to compress any log.sql file, not just the first found
  • I'm assuming you want to keep 7 compressed logs and remove any older ones.
  • I'm assuming you have the GNU coreutils (xargs -r, those stat options)

I'd do this:

#!/bin/bash

backuppath=/var/mysql_backup
mkdir -p "$backuppath"

shopt -s nullglob

# Remove old compressed archive
backups=( "$backuppath"/*.gz )
if [[ ${#backups[@]} -gt 0 ]]; then
    stat -c "%Y %n" "${backups[@]}" |
      sort -rn |
      sed -e '1,7d' -e 's/^[0-9]\+ //' |
      xargs -r rm
fi

# Compress all the logs
logs=( "$backuppath"/*.sql )
if [[ ${#logs[@]} -gt 0 ]]; then
    gzip "${logs[@]}"
fi

/usr/bin/mysqldump -u db_user -p'password' --single-transaction --all-databases \
    > "$backuppath/"$(date "+%Y.%m.%d-%H.%M.%S")_dump.sql

Notes:

  • don't use ALLCAPSVARS -- leave those for the shell. I've seen people accidentally do PATH=... and then wonder why their script is broken.
  • don't parse ls -- I'm using arrays to hold the results of the glob expansion, and checking how many elements are in the array.
  • always quote variables, except when you know exactly why not. See http://unix.stackexchange.com/q/171346/4667
  • I'm assuming you want to compress any log.sql file, not just the first found
  • I'm assuming you want to keep 7 compressed logs and remove any older ones.
  • I'm assuming you have the GNU coreutils (xargs -r, those stat options)
deleted 1 character in body
Source Link
glenn jackman
  • 2.6k
  • 13
  • 13

I'd do this:

#!/bin/bash

backuppath=/var/mysql_backup
mkdir -p "$backuppath"

shopt -s nullglob

# Remove old compressed archive
backups=( "$backuppath"/*.gz"gz )
if [[ ${#backups[@]} -gt 0 ]]; then
    stat -c "%Y %n" "${backups[@]}" |
      sort -rn |
      sed -e '1,7d' -e 's/^[0-9]\+ //' |
      xargs -r rm
fi

# compress all logs
logs=( "$backuppath"/*.sql )
if [[ ${#logs[@]} -gt 0 ]]; then
    gzip "${logs[@]}"
fi


/usr/bin/mysqldump -u db_user -p'password' --single-transaction --all-databases \
    > "$backuppath/$(date "+%Y.%m.%d-%H.%M.%S")_dump.sql

Notes:

  • don't use ALLCAPSVARS -- leave those for the shell. I've seen people accidentally do PATH=... and then wonder why their script is broken.
  • don't parse ls -- I'm using arrays to hold the results of the glob expansion, and checking how many elements are in the array.
  • always quote variables, except when you know exactly why not. See http://unix.stackexchange.com/q/171346/4667
  • I'm assuming you want to compress any log.sql file, not just the first found
  • I'm assuming you want to keep 7 compressed logs and remove any older ones.
  • I'm assuming you have the GNU coreutils (xargs -r, those stat options)

I'd do this:

#!/bin/bash

backuppath=/var/mysql_backup
mkdir -p "$backuppath"

shopt -s nullglob

# Remove old compressed archive
backups=( "$backuppath"/*.gz" )
if [[ ${#backups[@]} -gt 0 ]]; then
    stat -c "%Y %n" "${backups[@]}" |
      sort -rn |
      sed -e '1,7d' -e 's/^[0-9]\+ //' |
      xargs -r rm
fi

# compress all logs
logs=( "$backuppath"/*.sql )
if [[ ${#logs[@]} -gt 0 ]]; then
    gzip "${logs[@]}"
fi


/usr/bin/mysqldump -u db_user -p'password' --single-transaction --all-databases \
    > "$backuppath/$(date "+%Y.%m.%d-%H.%M.%S")_dump.sql

Notes:

  • don't use ALLCAPSVARS -- leave those for the shell. I've seen people accidentally do PATH=... and then wonder why their script is broken.
  • don't parse ls -- I'm using arrays to hold the results of the glob expansion, and checking how many elements are in the array.
  • always quote variables, except when you know exactly why not. See http://unix.stackexchange.com/q/171346/4667
  • I'm assuming you want to compress any log.sql file, not just the first found
  • I'm assuming you want to keep 7 compressed logs and remove any older ones.
  • I'm assuming you have the GNU coreutils (xargs -r, those stat options)

I'd do this:

#!/bin/bash

backuppath=/var/mysql_backup
mkdir -p "$backuppath"

shopt -s nullglob

# Remove old compressed archive
backups=( "$backuppath"/*.gz )
if [[ ${#backups[@]} -gt 0 ]]; then
    stat -c "%Y %n" "${backups[@]}" |
      sort -rn |
      sed -e '1,7d' -e 's/^[0-9]\+ //' |
      xargs -r rm
fi

# compress all logs
logs=( "$backuppath"/*.sql )
if [[ ${#logs[@]} -gt 0 ]]; then
    gzip "${logs[@]}"
fi


/usr/bin/mysqldump -u db_user -p'password' --single-transaction --all-databases \
    > "$backuppath/$(date "+%Y.%m.%d-%H.%M.%S")_dump.sql

Notes:

  • don't use ALLCAPSVARS -- leave those for the shell. I've seen people accidentally do PATH=... and then wonder why their script is broken.
  • don't parse ls -- I'm using arrays to hold the results of the glob expansion, and checking how many elements are in the array.
  • always quote variables, except when you know exactly why not. See http://unix.stackexchange.com/q/171346/4667
  • I'm assuming you want to compress any log.sql file, not just the first found
  • I'm assuming you want to keep 7 compressed logs and remove any older ones.
  • I'm assuming you have the GNU coreutils (xargs -r, those stat options)
Source Link
glenn jackman
  • 2.6k
  • 13
  • 13
Loading