Last active
August 29, 2015 14:03
-
-
Save ilyaevseev/71c18d46c5d11d65517e to your computer and use it in GitHub Desktop.
Redmine_database_backup
This file contains hidden or 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/sh | |
# | |
# /etc/cron.daily/redmine_database_backup | |
# | |
# Works under Debian/Ubuntu | |
# | |
MAIL_TO="admins" # ..see also /etc/aliases | |
REDMINE_CONFIG="/etc/redmine/default/database.yml" | |
MYSQL_CONFIG="/etc/mysql/debian.cnf" | |
DESTDIR="/home/redmine_backups" | |
DESTFILE="$DESTDIR/$(LANG=C date +%Y%m%d_%H%M%S)" | |
LOGFILE="$DESTFILE.log" | |
LS4SWEEP="/usr/local/bin/ls4sweep" | |
# ..taken from https://ls4sweep-perl.googlecode.com/hg/ls4sweep | |
Fail() { echo "$@" | mail -s "$0 failed" $MAIL_TO; exit 1; } | |
mkdir -pm700 "$DESTDIR/" | |
cd "$DESTDIR" || Fail "Destination directory $DESTDIR: failed." | |
DBMODE="$(awk '/adapter:/ {print $2;}' $REDMINE_CONFIG)" | |
DBNAME="$(awk '/database:/ {print $2;}' $REDMINE_CONFIG)" | |
case "$DBMODE" in | |
sqlite ) | |
DESTFILE="$DESTFILE.db" | |
test -s "$DBNAME" || Fail "Config $CFGFILE: database $DBNAME: missing or empty." | |
echo ".backup $DESTFILE" | sqlite3 "$DBNAME" >$LOGFILE 2>&1 | |
;; | |
mysql ) | |
DBUSER="$(awk '/^user/ {print $3; exit;}' $MYSQL_CONFIG)" | |
DBPASS="$(awk '/^password/ {print $3; exit;}' $MYSQL_CONFIG)" | |
DESTFILE="$DESTFILE.sql.xz" | |
mysqldump --opt -u"$DBUSER" -p"$DBPASS" "$DBNAME" 2>$LOGFILE | xz > $DESTFILE | |
;; | |
* ) | |
Fail "Unknown adapter $DBMODE in $REDMINE_CONFIG" ;; | |
esac | |
test -s "$DESTFILE" || Fail "Destination file $DESTFILE: failed, see $LOGFILE" | |
test -x "$LS4SWEEP" || Fail "Ls4sweep: $LS4SWEEP: no executable." | |
$LS4SWEEP 5:1,5:3,5:10,5:15,5:30,5:45,5:60,5:90,5:120,5:180,999:360 *.db | xargs -r /bin/rm | |
test -s "$LOGFILE" || /bin/rm "$LOGFILE" | |
## END ## |
This file contains hidden or 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/sh | |
wget -O /usr/local/bin/ls4sweep https://ls4sweep-perl.googlecode.com/hg/ls4sweep | |
wget -O /etc/cron.daily/redmine-database-backup https://gist.githubusercontent.com/ilyaevseev/71c18d46c5d11d65517e/raw/7ed3ce0b825e372485388d568001e65013d671f7/redmine_database_backup | |
chmod +x /etc/cron.daily/redmine-database-backup /usr/local/bin/ls4sweep | |
/etc/cron.daily/redmine-database-backup | |
ls -l /home/redmine_backups/ | |
## END ## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment