From 2c5b4acdd979d38d603ba893add7ead24696d527 Mon Sep 17 00:00:00 2001 From: Fred Gleason Date: Tue, 8 Oct 2024 18:39:26 -0400 Subject: [PATCH] 2024-10-08 Fred Gleason * Added an rdautoback(8) utility. Signed-off-by: Fred Gleason --- ChangeLog | 2 + configure.ac | 1 + utils/Makefile.am | 1 + utils/rdautorest/Makefile.am | 37 ++++++++++ utils/rdautorest/rdautorest.py | 128 +++++++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 utils/rdautorest/Makefile.am create mode 100755 utils/rdautorest/rdautorest.py diff --git a/ChangeLog b/ChangeLog index f8d3973e..dce02b58 100644 --- a/ChangeLog +++ b/ChangeLog @@ -24905,3 +24905,5 @@ if the destination drive was already mounted. 2024-10-08 Fred Gleason * Added a rdautoback(8) man page. +2024-10-08 Fred Gleason + * Added an rdautoback(8) utility. diff --git a/configure.ac b/configure.ac index 3062cef9..2abc64d2 100644 --- a/configure.ac +++ b/configure.ac @@ -688,6 +688,7 @@ AC_CONFIG_FILES([rivendell.spec \ utils/Makefile \ utils/rdalsaconfig/Makefile \ utils/rdautoback/Makefile \ + utils/rdautorest/Makefile \ utils/rdcheckcuts/Makefile \ utils/rdcleandirs/Makefile \ utils/rdclilogedit/Makefile \ diff --git a/utils/Makefile.am b/utils/Makefile.am index 2f631682..c4791707 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -25,6 +25,7 @@ if ALSA_RD_AM endif SUBDIRS = $(ALSACONFIG_RD_OPT)\ rdautoback\ + rdautorest\ rdcheckcuts\ rdcleandirs\ rdclilogedit\ diff --git a/utils/rdautorest/Makefile.am b/utils/rdautorest/Makefile.am new file mode 100644 index 00000000..c93c613c --- /dev/null +++ b/utils/rdautorest/Makefile.am @@ -0,0 +1,37 @@ +## Makefile.am +## +## utils/rdautorest Makefile.am for Rivendell +## +## (C) Copyright 2024 Fred Gleason +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License version 2 as +## published by the Free Software Foundation. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public +## License along with this program; if not, write to the Free Software +## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +## +## +## Use automake to process this into a Makefile.in + +install-exec-local: + mkdir -p $(DESTDIR)/usr/sbin + ../../helpers/install_python.sh rdautorest.py $(DESTDIR)/usr/sbin/rdautorest + +uninstall-local: + rm -f $(DESTDIR)/usr/sbin/rdautorest + +bin_SCRIPTS = rdautorest.py + +EXTRA_DIST = rdautorest.py + +CLEANFILES = *~ + +MAINTAINERCLEANFILES = *~\ + Makefile.in diff --git a/utils/rdautorest/rdautorest.py b/utils/rdautorest/rdautorest.py new file mode 100755 index 00000000..a1498ff0 --- /dev/null +++ b/utils/rdautorest/rdautorest.py @@ -0,0 +1,128 @@ +#!%PYTHON_BANGPATH% + +# rdautorest.py +# +# Unattended restore script for Rivendell +# +# (C) Copyright 2024 Fred Gleason +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public +# License along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# + +import configparser +import os +from pathlib import Path +import sys +import syslog + +USAGE='USAGE: rdautorest.py [--yes] ' + +yes=False + +if(len(sys.argv)==2): + mountpoint=sys.argv[1] +else: + if(len(sys.argv)==3): + if(sys.argv[1]=='--yes'): + yes=True + mountpoint=sys.argv[2] + else: + if(sys.argv[2]=='--yes'): + yes=True + mountpoint=sys.argv[1] + else: + print(USAGE) + exit(1) + else: + print(USAGE) + exit(1) + +if(not yes): + print('WARNING: This operation will COMPLETELY OVERWRITE the existing') + print(' Rivendell data on this system, replacing it with the') + print(' contents of the specified data backup. This operation') + print(' cannot be undone!') + print('') + resp=input(' Are you sure you want to proceed (y/N)?') + if((resp.upper()!='Y')and(resp.upper()!='YES')): + exit(0) + +# +# Load rd.conf(5) +# +rd_config=configparser.ConfigParser(interpolation=None) +rd_config.read_file(open('/etc/rd.conf')) + +# +# Open the syslog +# +syslog.openlog('rdautorest.py',logoption=syslog.LOG_PID|syslog.LOG_PERROR,facility=int(rd_config.get('Identity','SyslogFacility',fallback=syslog.LOG_USER))) +syslog.syslog(syslog.LOG_INFO,'Starting Rivendell backup to "'+mountpoint+'"') + + +# +# Mount backup device +# +result=os.system(command='findmnt '+mountpoint) +if(os.waitstatus_to_exitcode(result)!=0): + Path(mountpoint).mkdir(parents=True,exist_ok=True) + result=os.system(command='mount '+mountpoint) + if(os.waitstatus_to_exitcode(result)!=0): + syslog.syslog(syslog.LOG_ERR,'unable to mount backup drive') + exit(1) + os.system(command='sleep 5') + +# +# Stop Rivendell service +# +result=os.system(command='/bin/systemctl stop rivendell') +if(os.waitstatus_to_exitcode(result)!=0): + syslog.syslog(syslog.LOG_ERR,'unable to stop Rivendell service') + exit(1) + +# +# Create new database +# +sql='drop\ database\ '+rd_config.get('mySQL','Database')+'\;' +cmd='echo '+sql+' | mysql -h '+rd_config.get('mySQL','Hostname')+' -u '+rd_config.get('mySQL','Loginname')+' -p'+rd_config.get('mySQL','Password')+' '+rd_config.get('mySQL','Database') +os.system(command=cmd) + +sql='create\ database\ '+rd_config.get('mySQL','Database')+'\;' +cmd='echo '+sql+' | mysql -h '+rd_config.get('mySQL','Hostname')+' -u '+rd_config.get('mySQL','Loginname')+' -p'+rd_config.get('mySQL','Password') +os.system(command=cmd) + +cmd='gzip -cd '+mountpoint+'/db.sql.gz | mysql -h '+rd_config.get('mySQL','Hostname')+' -u '+rd_config.get('mySQL','Loginname')+' -p'+rd_config.get('mySQL','Password')+' '+rd_config.get('mySQL','Database') +os.system(command=cmd) + +# +# Copy Audio Store +# +cmd='rsync -av --delete '+mountpoint+'/snd/ /var/snd ' +os.system(command=cmd) + +# +# Start Rivendell service +# +result=os.system(command='/bin/systemctl restart rivendell') +if(os.waitstatus_to_exitcode(result)!=0): + syslog.syslog(syslog.LOG_ERR,'unable to start Rivendell service') + exit(1) + +# +# Unmount backup device +# +os.system(command='umount '+mountpoint) +os.rmdir(mountpoint) + +syslog.syslog(syslog.LOG_INFO,'Completed Rivendell restore from "'+mountpoint+'"')