Skip to content

Instantly share code, notes, and snippets.

@asmoore82
Created May 3, 2019 21:42
Show Gist options
  • Save asmoore82/172a9bc60fbd9e52b8bd30580fd272d8 to your computer and use it in GitHub Desktop.
Save asmoore82/172a9bc60fbd9e52b8bd30580fd272d8 to your computer and use it in GitHub Desktop.
BASH script for a 2 week rotation of MySQL backups with a standardized name - run as cronjob every 2 or 4 hours.
#!/bin/bash
#Copyright (c) 2019 Alamance-Burlington Schools, MIT License
cd "$HOME/backups"
usernm="usernm"
passwd="passwd"
table="dbtable"
week="$( date +%-V )"
day="$( date +%u )"
hour="$( date +%-H )"
#Use modulo 2 to flatten rotating week numbers
(( week = 2 - ( week % 2 ) ))
#Use integer division to floor to the even hour
(( hour = hour / 2 * 2 ))
mysqldump --quick --single-transaction -u"$usernm" -p"$passwd" "$table" | gzip > "$table.$week-$day-$hour.sql.gz"
#End of File
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment