Skip to main content
edited title
Link
Mast
  • 13.8k
  • 12
  • 57
  • 127

How can I make my Weekly MySQL database backup script generic, faster and safer?

Copy edited (e.g. ref. <https://en.wikipedia.org/wiki/MySQL>, <https://en.wikipedia.org/wiki/Unix>, <https://en.wiktionary.org/wiki/back_up#Verb>, <https://en.wikipedia.org/wiki/Apache_HTTP_Server>, and <https://en.wiktionary.org/wiki/file_size#Noun>). Expanded. Added some context.
Source Link

How can I make my mysqlMySQL database backup script generic, faster and safer?

I haven't used unixUnix, etc. for 40 years, so I am a bit rusty. I need to backupback up the databases on my website(s) once a week and then retrieve them automatically from my development machine. So this is step one and is working fine as a crontabcrontab job on apacheApache. It emails me a message with status/error.

  1. I am creating a folder with the date as name and keeping 14 backups.
  2. I am not using a loop for the databases, because each one has a different user/pwd combocombination.
  3. I don't see it dangerous to have the username and passwords here. If someone has root access, they can access the database anyway.
  4. The filesizefile size is returned as well.
  5. The start time and finish time are returned, so I can calculate how long the job took.

How can make it more generic, or faster, or safer  ? Am I catching all the errors?

#!/usr/bin/bash

TIMESTAMP=$(date +"%F")
BACKUP_DIR=backup
MYSQLDUMP=/usr/bin/mysqldump
STAT=/usr/bin/stat
EMAIL=***
KEEP="+14"
MSG=tmp/backup_msg

echo "Date: $(date)" > $MSG
echo "Hostname: $(hostname)" >> $MSG
echo " " >> $MSG
echo " " >> $MSG

mkdir -p "$BACKUP_DIR/$TIMESTAMP" 2>> $MSG

DB=xxx_directory
BKP=$BACKUP_DIR/$TIMESTAMP/db_biz_directory.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP  2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

DB=xxx_software
BKP=$BACKUP_DIR/$TIMESTAMP/db_swr_software.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP 2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

echo "Removing excess backups" >> $MSG
find $BACKUP_DIR* -mtime $KEEP -exec rm {} \;  >> $MSG 2>> $MSG

echo "Finished: $(date)" >> $MSG
mail  -s "MySQL Backup script has run" "$EMAIL" <$MSG
rm -f $MSG

How can I make my mysql database backup script generic, faster and safer

I haven't used unix etc for 40 years, so a bit rusty. I need to backup the databases on my website(s) once a week and then retrieve them automatically from my development machine. So this is step one and is working fine as a crontab job on apache. It emails me a message with status/error.

  1. I am creating a folder with the date as name and keeping 14 backups.
  2. I am not using a loop for the databases, because each one has a different user/pwd combo.
  3. I don't see it dangerous to have the username and passwords here. If someone has root access, they can access the database anyway.
  4. The filesize is returned as well.
  5. The start time and finish time are returned, so I can calculate how long the job took.

How can make it more generic, or faster, or safer  ? Am I catching all the errors?

#!/usr/bin/bash

TIMESTAMP=$(date +"%F")
BACKUP_DIR=backup
MYSQLDUMP=/usr/bin/mysqldump
STAT=/usr/bin/stat
EMAIL=***
KEEP="+14"
MSG=tmp/backup_msg

echo "Date: $(date)" > $MSG
echo "Hostname: $(hostname)" >> $MSG
echo " " >> $MSG
echo " " >> $MSG

mkdir -p "$BACKUP_DIR/$TIMESTAMP" 2>> $MSG

DB=xxx_directory
BKP=$BACKUP_DIR/$TIMESTAMP/db_biz_directory.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP  2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

DB=xxx_software
BKP=$BACKUP_DIR/$TIMESTAMP/db_swr_software.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP 2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

echo "Removing excess backups" >> $MSG
find $BACKUP_DIR* -mtime $KEEP -exec rm {} \;  >> $MSG 2>> $MSG

echo "Finished: $(date)" >> $MSG
mail  -s "MySQL Backup script has run" "$EMAIL" <$MSG
rm -f $MSG

How can I make my MySQL database backup script generic, faster and safer?

I haven't used Unix, etc. for 40 years, so I am a bit rusty. I need to back up the databases on my website(s) once a week and then retrieve them automatically from my development machine. So this is step one and is working fine as a crontab job on Apache. It emails me a message with status/error.

  1. I am creating a folder with the date as name and keeping 14 backups.
  2. I am not using a loop for the databases, because each one has a different user/pwd combination.
  3. I don't see it dangerous to have the username and passwords here. If someone has root access, they can access the database anyway.
  4. The file size is returned as well.
  5. The start time and finish time are returned, so I can calculate how long the job took.

How can make it more generic, or faster, or safer? Am I catching all the errors?

#!/usr/bin/bash

TIMESTAMP=$(date +"%F")
BACKUP_DIR=backup
MYSQLDUMP=/usr/bin/mysqldump
STAT=/usr/bin/stat
EMAIL=***
KEEP="+14"
MSG=tmp/backup_msg

echo "Date: $(date)" > $MSG
echo "Hostname: $(hostname)" >> $MSG
echo " " >> $MSG
echo " " >> $MSG

mkdir -p "$BACKUP_DIR/$TIMESTAMP" 2>> $MSG

DB=xxx_directory
BKP=$BACKUP_DIR/$TIMESTAMP/db_biz_directory.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP  2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

DB=xxx_software
BKP=$BACKUP_DIR/$TIMESTAMP/db_swr_software.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP 2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

echo "Removing excess backups" >> $MSG
find $BACKUP_DIR* -mtime $KEEP -exec rm {} \;  >> $MSG 2>> $MSG

echo "Finished: $(date)" >> $MSG
mail  -s "MySQL Backup script has run" "$EMAIL" <$MSG
rm -f $MSG
Tweeted twitter.com/StackCodeReview/status/1559193215772811265
Became Hot Network Question
add a blank line after the code block so the ``` don't show up as part of the code
Source Link
chicks
  • 2.9k
  • 3
  • 18
  • 30

I haven't used unix etc for 40 years, so a bit rusty. I need to backup the databases on my website(s) once a week and then retrieve them automatically from my development machine. So this is step one and is working fine as a crontab job on apache. It emails me a message with status/error.

  1. I am creating a folder with the date as name and keeping 14 backups.
  2. I am not using a loop for the databases, because each one has a different user/pwd combo.
  3. I don't see it dangerous to have the username and passwords here. If someone has root access, they can access the database anyway.
  4. The filesize is returned as well.
  5. The start time and finish time are returned, so I can calculate how long the job took.

How can make it more generic, or faster, or safer ? Am I catching all the errors?

#!/usr/bin/bash

TIMESTAMP=$(date +"%F")
BACKUP_DIR=backup
MYSQLDUMP=/usr/bin/mysqldump
STAT=/usr/bin/stat
EMAIL=***
KEEP="+14"
MSG=tmp/backup_msg

echo "Date: $(date)" > $MSG
echo "Hostname: $(hostname)" >> $MSG
echo " " >> $MSG
echo " " >> $MSG

mkdir -p "$BACKUP_DIR/$TIMESTAMP" 2>> $MSG

DB=xxx_directory
BKP=$BACKUP_DIR/$TIMESTAMP/db_biz_directory.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP  2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

DB=xxx_software
BKP=$BACKUP_DIR/$TIMESTAMP/db_swr_software.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP 2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

echo "Removing excess backups" >> $MSG
find $BACKUP_DIR* -mtime $KEEP -exec rm {} \;  >> $MSG 2>> $MSG

echo "Finished: $(date)" >> $MSG
mail  -s "MySQL Backup script has run" "$EMAIL" <$MSG
rm -f $MSG
```

I haven't used unix etc for 40 years, so a bit rusty. I need to backup the databases on my website(s) once a week and then retrieve them automatically from my development machine. So this is step one and is working fine as a crontab job on apache. It emails me a message with status/error.

  1. I am creating a folder with the date as name and keeping 14 backups.
  2. I am not using a loop for the databases, because each one has a different user/pwd combo.
  3. I don't see it dangerous to have the username and passwords here. If someone has root access, they can access the database anyway.
  4. The filesize is returned as well.
  5. The start time and finish time are returned, so I can calculate how long the job took.

How can make it more generic, or faster, or safer ? Am I catching all the errors?

#!/usr/bin/bash

TIMESTAMP=$(date +"%F")
BACKUP_DIR=backup
MYSQLDUMP=/usr/bin/mysqldump
STAT=/usr/bin/stat
EMAIL=***
KEEP="+14"
MSG=tmp/backup_msg

echo "Date: $(date)" > $MSG
echo "Hostname: $(hostname)" >> $MSG
echo " " >> $MSG
echo " " >> $MSG

mkdir -p "$BACKUP_DIR/$TIMESTAMP" 2>> $MSG

DB=xxx_directory
BKP=$BACKUP_DIR/$TIMESTAMP/db_biz_directory.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP  2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

DB=xxx_software
BKP=$BACKUP_DIR/$TIMESTAMP/db_swr_software.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP 2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

echo "Removing excess backups" >> $MSG
find $BACKUP_DIR* -mtime $KEEP -exec rm {} \;  >> $MSG 2>> $MSG

echo "Finished: $(date)" >> $MSG
mail  -s "MySQL Backup script has run" "$EMAIL" <$MSG
rm -f $MSG
```

I haven't used unix etc for 40 years, so a bit rusty. I need to backup the databases on my website(s) once a week and then retrieve them automatically from my development machine. So this is step one and is working fine as a crontab job on apache. It emails me a message with status/error.

  1. I am creating a folder with the date as name and keeping 14 backups.
  2. I am not using a loop for the databases, because each one has a different user/pwd combo.
  3. I don't see it dangerous to have the username and passwords here. If someone has root access, they can access the database anyway.
  4. The filesize is returned as well.
  5. The start time and finish time are returned, so I can calculate how long the job took.

How can make it more generic, or faster, or safer ? Am I catching all the errors?

#!/usr/bin/bash

TIMESTAMP=$(date +"%F")
BACKUP_DIR=backup
MYSQLDUMP=/usr/bin/mysqldump
STAT=/usr/bin/stat
EMAIL=***
KEEP="+14"
MSG=tmp/backup_msg

echo "Date: $(date)" > $MSG
echo "Hostname: $(hostname)" >> $MSG
echo " " >> $MSG
echo " " >> $MSG

mkdir -p "$BACKUP_DIR/$TIMESTAMP" 2>> $MSG

DB=xxx_directory
BKP=$BACKUP_DIR/$TIMESTAMP/db_biz_directory.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP  2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

DB=xxx_software
BKP=$BACKUP_DIR/$TIMESTAMP/db_swr_software.sql
echo "Backing up '$DB' to '$BKP'" >> $MSG
$MYSQLDUMP -u username -ppassword $DB > $BKP 2>> $MSG
SIZE=$($STAT -c%s $BKP) 2>> $MSG
echo "Filesize: $SIZE" >> $MSG
echo " " >> $MSG

echo "Removing excess backups" >> $MSG
find $BACKUP_DIR* -mtime $KEEP -exec rm {} \;  >> $MSG 2>> $MSG

echo "Finished: $(date)" >> $MSG
mail  -s "MySQL Backup script has run" "$EMAIL" <$MSG
rm -f $MSG
Source Link
Rohit Gupta
  • 268
  • 2
  • 3
  • 14
Loading