Database Backup Script

Scenario: Create a backup script to take Backup of mysql databases on remote server to your local backup server and remove existing backups older than 7 days.

Solution:


#!/bin/bash

DB_BACKUP="/Databases/`date +%A`"
DB_USER="root"
DB_PASSWD="Pass@7rt5_890#"
DB_HOST='172.24.25.154'

# Remove backups older than 7 days
find /Databases -maxdepth 1 -type d -mtime +7 -exec rm -rf {} \;

# Create the backup directory
mkdir -p $DB_BACKUP

for db in $(mysql -h $DB_HOST  -u $DB_USER -p$DB_PASSWD -e 'show databases' -s --skip-column-names|grep -vi information_schema);
do mysqldump -h $DB_HOST -u $DB_USER -p$DB_PASSWD --opt  --skip-add-locks --routines $db | gzip > "$DB_BACKUP/$db-$(date +%Y-%m-%d).gz";

done

You can leave a response, or trackback from your own site.

Leave a Reply