Skip to content

Instantly share code, notes, and snippets.

@katsube
Last active September 18, 2020 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsube/c7a0ad2e211f0a726e7a44f55cb289e4 to your computer and use it in GitHub Desktop.
Save katsube/c7a0ad2e211f0a726e7a44f55cb289e4 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Backup MySQL and Remine files to S3
#
# Author: M.Katsube
# Date: 2020-09-15
echo 'Redmine Backuper'
echo ''
echo '*** start ***'
echo `date '+%y/%m/%d %H:%M:%S'`
echo '----------------------------'
#---------------------------------------
# 設定
#---------------------------------------
# ローカルに残すファイル数
period=3
# ローカルのバックアップの格納先
dirpath='/var/tmp/backup/redmine/'
# YYYYMMDD
today=`date +%Y%m%d`
yyyymm=`date +%Y%m`
# MySQLユーザー名
mysqluser='backuper'
# MySQLパスワード
mysqlpass='** your password **'
# S3 Bucket
s3bucket='s3://backup.redmine.example.com'
# AWSのcredentialファイル
export AWS_SHARED_CREDENTIALS_FILE=/home/bitnami/.aws/credentials
#---------------------------------------
# Check directory
#---------------------------------------
if [ ! -d $dirpath ]; then
mkdir -p $dirpath
fi
#---------------------------------------
# MySQL Dump
#---------------------------------------
sqlfilename="mysql_${today}.sql.gz"
# ダンプしgzipで固める
/opt/bitnami/mysql/bin/mysqldump -u$mysqluser -p$mysqlpass -h localhost -x --all-databases --events | gzip > $dirpath/$sqlfilename
if [ $? -eq 0 ]; then
# n日前のファイルを削除
olddate=`date --date "$period days ago" +%Y%m%d`
oldsqlfile="mysql_${olddate}.sql.gz"
if [ -f $dirpath/$oldsqlfile ]; then
sudo rm -f $dirpath/$oldsqlfile
fi
# パーミションを少し厳し目に
sudo chmod 0600 $dirpath/$sqlfilename
fi
#---------------------------------------
# Redmine Files
#---------------------------------------
tarfilename="files_${today}.tar.gz"
# Redmineのファイルをtarで固める
sudo tar zcf $dirpath/$tarfilename -C /opt/bitnami/apps/redmine/htdocs files config
sudo chown bitnami:bitnami $dirpath/$tarfilename
sudo chmod 0600 $dirpath/$tarfilename
# n日前のファイルを削除
oldtarfile="files_${olddate}.tar.gz"
if [ -f $dirpath/$oldtarfile ]; then
sudo rm -f $dirpath/$oldtarfile
fi
#---------------------------------------
# Send S3
#---------------------------------------
if [ -f $dirpath/$sqlfilename ]; then
/usr/bin/aws s3 cp $dirpath/$sqlfilename $s3bucket/$yyyymm/$sqlfilename
fi
if [ -f $dirpath/$tarfilename ]; then
/usr/bin/aws s3 cp $dirpath/$tarfilename $s3bucket/$yyyymm/$tarfilename
fi
echo '----------------------------'
echo '*** end ***'
echo `date '+%y/%m/%d %H:%M:%S'`
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment