Skip to content

Instantly share code, notes, and snippets.

@zahidhassanshaikot
Last active June 6, 2024 10:37
Show Gist options
  • Select an option

  • Save zahidhassanshaikot/b52fccb283e654496f1c455f17f34b00 to your computer and use it in GitHub Desktop.

Select an option

Save zahidhassanshaikot/b52fccb283e654496f1c455f17f34b00 to your computer and use it in GitHub Desktop.
bash file for db backup and if the backup file is older than 1 day it will automatically delete those files.
#!/bin/bash
#your database info
DBHOST='localhost'
DBUSER='db_user'
DBPW='db_password'
DBNAME=('db_name')
#get recent version of databases from array
for i in "${DBNAME[@]}"
do
mysqldump --opt --user=$DBUSER --password=$DBPW --host=$DBHOST --no-tablespaces $i --lock-tables=false > /var/www/backups/$i.`date +\%Y-\%m-\%d_\%H-\%M-\%S`.sql
done
#remove db files older than 1 day (for 30 min: -mmin +30)
find /var/www/backups/*.sql -type f -daystart -mtime +0 -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment