Last active
December 21, 2015 19:19
-
-
Save malias/b031ddd84794d3ab3bd8 to your computer and use it in GitHub Desktop.
Make daily mysql backups and delete them after 30 days
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Mit diesem Script koennen diverse Datenbank mit einem Schritt gesichert werden. | |
# Das Zielverzeichnis (backupTargetPath) muss zuerst erstellt werden: | |
# $ cd /home/BENUTZERNAME/ | |
# $ mkdir mysqlbackup | |
# Beispiel fuer mehrere Datenbanken ... | |
# databases='oliveror_db1 oliveror_joomla oliveror_wordpress'; | |
databases='DATENBANKEN'; | |
# Logininformationen | |
username='BENUTZER'; | |
password='PASSWORD'; | |
# Datum; Bitte nicht anpassen | |
date=`/bin/date "+%Y-%m-%d-%H%M%S"` | |
# Pfad Zielverzeichnis | |
backupTargetPath='/home/BENUTZERNAME/mysqlbackup/'; | |
# Default: 30 ; Alle backups, die aelter als 30 Tage sind werden geloescht. | |
keepBackupDays=30 | |
# Backup wird erstellt; Bitte nicht anpassen | |
for i in $databases; do | |
/usr/bin/mysqldump $i -u $username --password=$password | gzip > $backupTargetPath/$i.$date.gz | |
done | |
# Dateien im Zielverzeichnis, die aelter als 'keepBackupDays(Default 30)' werden geloescht; Bitte nicht anpassen | |
ls -1 $backupTargetPath | while read file | |
do | |
file=${backupTargetPath}${file} | |
if [[ -f $file ]] && [[ "${file}" =~ ".*\.gz$" ]] && [[ `stat -c %Y $file` < `date --date="${keepBackupDays} days ago" +%s` ]] | |
then | |
rm -fv $file | |
fi | |
done; | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment