Add support for mysql databases

This commit is contained in:
Rob Pearce 2021-06-26 11:26:08 +10:00
parent 3782cdf528
commit 02140f07db
1 changed files with 60 additions and 19 deletions

81
bare.sh
View File

@ -93,7 +93,8 @@ function usage-repo() {
echo " restic Backup using restic (default repo is Backblaze B2 bucket \$B2_BUCKET_PREFIX-<reponame>)"
echo " rclone Backup using rclone (default repo is Backblaze B2 bucket \$B2_BUCKET_PREFIX-<reponame>)"
echo " rsync Backup using rsync (default repo is \$DEF_RSYNC_USER@\$DEF_RSYNC_SERVER:\$DEF_RSYNC_DIR/<reponame>)"
echo " postgres Dump all postgres databases. Instead of local file path, specify database name to backup. If empty, all dbs are backed up."
echo " postgres Dump postgres databases. Instead of local file path, specify database name to backup. If empty, all dbs are backed up."
echo " mysql Dump mysql databases. Instead of local file path, specify database name to backup. If empty, all dbs are backed up."
echo ""
echo "Pre-backup commands to access local files:"
echo " nfs Mount \$DEF_NFS_SERVER:\$DEF_NFS_SERVER_BASE/<sharename> to <repopath>"
@ -362,9 +363,8 @@ function do_mount() {
fi
fi
fi
checktag "$f" postgres # datapath is used for sql filename in pgsql backups
if [[ $? -eq 0 ]]; then
# datapath is used for sql filename in pgsql/mysql backups
if checktag "$f" postgres; then
# Make sure the db exists
if ! [[ -z ${DATAPATH} ]]; then
psql -lqt | cut -d \| -f 1 | grep -qw ${DATAPATH}
@ -373,6 +373,15 @@ function do_mount() {
return 1
fi
fi
elif checktag "$f" mysql; then
# Make sure the db exists
if ! [[ -z ${DATAPATH} ]]; then
mysql -e 'show databases' | grep -v "^Database$" | grep -qw ${DATAPATH}
if [[ $? -ne 0 ]]; then
log "Error: MySQL database '${DATAPATH}' doesn't exist"
return 1
fi
fi
else
if ! [[ -e ${DATAPATH} ]]; then
log "Error: ${DATAPATH} doesn't exist"
@ -385,6 +394,7 @@ function do_mount() {
return 1
fi
fi
return 0
}
@ -676,6 +686,8 @@ if [[ $CMD == "repos" ]]; then
if [[ $tags == *postgres* && -z $thispath ]]; then
thispath="(all dbs)"
elif [[ $tags == *mysql* && -z $thispath ]]; then
thispath="(all dbs)"
fi
line=$(printf "$format" "$thisrepo" "$thispath" "$thisrpath" "$tags")
@ -754,7 +766,10 @@ for f in $REPO_LIST; do
if [[ -z $DATAPATH ]]; then
checktag "$f" postgres
if [[ $? -ne 0 ]]; then
pgrv=$?
checktag "$f" mysql
myrv=$?
if [[ $pgrv -ne 0 && $myrv -ne 0 ]]; then
log "error - can't find matching repo for $f - make sure it is listed in ${REPOFILE}"
continue
fi
@ -899,6 +914,10 @@ for f in $REPO_LIST; do
if [[ $? -eq 0 ]]; then
CONNECTIONSOPTS="$OTHEROPTS --tag postgres"
fi
checktag "$f" mysql
if [[ $? -eq 0 ]]; then
CONNECTIONSOPTS="$OTHEROPTS --tag mysql"
fi
if [[ $VERBOSE -eq 1 ]]; then
OTHEROPTS="$OTHEROPTS -v"
@ -919,6 +938,19 @@ for f in $REPO_LIST; do
DBOPTS="$DBOPTS ${DATAPATH}" # Last dbdump option is database name
fi
fi
checktag "$f" mysql
if [[ $? -eq 0 ]]; then
DBOPTS="$DBOPTS --all-databases --master-data --single-transaction"
OTHEROPTS="$OTHEROPTS --stdin"
if [[ -z ${DATAPATH} ]]; then
DBDUMPCMD="mysqldump"
DBFILENAME="mysql_alldbs.sql"
else
DBDUMPCMD="mysqldump"
DBFILENAME="mysql_${DATAPATH}.sql"
DBOPTS="$DBOPTS ${DATAPATH}" # Last dbdump option is database name
fi
fi
export RESTIC_EXCLUDEFILE=`getrepoexcludefile "$f"`
if [[ ! -z $RESTIC_EXCLUDEFILE ]]; then
@ -1026,12 +1058,18 @@ for f in $REPO_LIST; do
fi
elif [[ $mode == "restic" ]]; then
if [[ $CMD == "go" ]]; then
if checktag "$f" postgres; then
btype=postgres
elif checktag "$f" mysql; then
btype=mysql
else
btype=normal
fi
if [[ $CRONMODE -eq 1 ]]; then
if [[ $VERBOSE -eq 1 ]]; then
echo "debug: running: [${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $OTHEROPTS $DATAPATH" >>${LOG}
echo "debug: full log is in ${FULLLOGFILE}" >>${LOG}
checktag "$f" postgres
if [[ $? -eq 0 ]] ; then
if [[ $btype == "postgres" || $btype == "mysql" ]] ; then
$DBDUMPCMD $DBOPTS | ${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $OTHEROPTS --stdin-filename $DBFILENAME 2>&1 >> ${FULLLOGFILE}
rv=$?
else
@ -1040,18 +1078,16 @@ for f in $REPO_LIST; do
fi
egrep "^(Added|processed|snapshot)" ${FULLLOGFILE} >>${LOG}
else
checktag "$f" postgres
if [[ $? -eq 0 ]] ; then
${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $OTHEROPTS $DATAPATH 2>&1 | egrep "^(Added|processed|snapshot)" >> ${LOG}
rv=${PIPESTATUS[0]}
else
if [[ $btype == "postgres" || $btype == "mysql" ]] ; then
$DBDUMPCMD $DBOPTS | ${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $OTHEROPTS --stdin-filename $DBFILENAME 2>&1 | egrep "^(Added|processed|snapshot)" >> ${LOG}
rv=${PIPESTATUS[0]}
else
${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $OTHEROPTS $DATAPATH 2>&1 | egrep "^(Added|processed|snapshot)" >> ${LOG}
rv=${PIPESTATUS[0]}
fi
fi
else
checktag "$f" postgres
if [[ $? -eq 0 ]] ; then
if [[ $btype == "postgres" || $btype == "mysql" ]] ; then
[[ $VERBOSE -eq 1 ]] && log "debug: running: [$DBDUMPCMD $DBOPTS | ${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $OTHEROPTS --stdin-filename $DBFILENAME"
$DBDUMPCMD $DBOPTS | ${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $OTHEROPTS --stdin-filename $DBFILENAME 2>&1 >> ${LOG}
rv=$?
@ -1064,9 +1100,10 @@ for f in $REPO_LIST; do
elif [[ $CMD == "get" ]]; then
RESTOREDIR="$RESTOREDIRBASE/$f/"
snaps=$(${RESTIC} snapshots $AUTHOPTS $CONNECTIONSOPTS 2>&1 | grep -v ID | awk '(NF >= 4) { print }')
checktag "$f" postgres
if [[ $? -ne 0 ]]; then
if checktag "$f" postgres; then
snaps=$(echo "$snaps" | grep -v postgres)
elif checktag "$f" mysql; then
snaps=$(echo "$snaps" | grep -v mysql)
fi
snapids=$(echo "$snaps" | awk '{ print $1 }')
defsid=$(echo "$snapids" | tail -1)
@ -1294,10 +1331,12 @@ for f in $REPO_LIST; do
fi
elif [[ $mode == "rsync" ]]; then
REMOTE=""
checktag "$f" postgres
if [[ $? -eq 0 ]] ; then
if checktag "$f" postgres; then
log "Error: postgres backups not supported for rsync mode."
rv=1
elif checktag "$f" mysql; then
log "Error: mysql backups not supported for rsync mode."
rv=1
elif [[ $CMD == "go" ]]; then
${RSYNC} ${RSYNC_OPTIONS} $DATAPATH/ ${RSYNC_REPOSITORY} 2>&1 >> ${LOG}
rv=$?
@ -1334,10 +1373,12 @@ for f in $REPO_LIST; do
else
# rclone
REMOTE=""
checktag "$f" postgres
if [[ $? -eq 0 ]] ; then
if checktag "$f" postgres; then
log "Error: postgres backups not supported for $mode mode."
rv=1
elif checktag "$f" mysql; then
log "Error: mysql backups not supported for $mode mode."
rv=1
elif [[ $CMD == "go" ]]; then
${RCLONE} sync $RCLONEOPTS $CONNECTIONSOPTS $SPEEDOPTS $OTHEROPTS $DATAPATH $RCLONE_REPOSITORY 2>&1 >> ${LOG}
rv=$?