Skip to content

Instantly share code, notes, and snippets.

@Robiussani152
Created December 29, 2021 11:12
Show Gist options
  • Save Robiussani152/142fe0bde09876eec93b9a3bec4650b9 to your computer and use it in GitHub Desktop.
Save Robiussani152/142fe0bde09876eec93b9a3bec4650b9 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