[PC-BSD Commits] r4925 - in pcbsd/trunk/lifePreserver: . scripts
svn at pcbsd.org
svn at pcbsd.org
Mon Nov 9 09:53:06 PST 2009
Author: kris
Date: 2009-11-09 09:53:05 -0800 (Mon, 09 Nov 2009)
New Revision: 4925
Added:
pcbsd/trunk/lifePreserver/scripts/backup-rsync.sh
pcbsd/trunk/lifePreserver/scripts/functions.sh
Modified:
pcbsd/trunk/lifePreserver/lPreserve.pro
pcbsd/trunk/lifePreserver/lifePreserverMain.cpp
pcbsd/trunk/lifePreserver/lifePreserverWizard.cpp
pcbsd/trunk/lifePreserver/lifePreserverWizard.ui
pcbsd/trunk/lifePreserver/preserver.cpp
pcbsd/trunk/lifePreserver/preserver.h
pcbsd/trunk/lifePreserver/scripts/start-backup.sh
Log:
Large update to lifePreserver backup tool, now able to schedule via cron, keep track of results, and
set other conf settings for each backup
Modified: pcbsd/trunk/lifePreserver/lPreserve.pro
===================================================================
--- pcbsd/trunk/lifePreserver/lPreserve.pro 2009-11-09 10:03:34 UTC (rev 4924)
+++ pcbsd/trunk/lifePreserver/lPreserve.pro 2009-11-09 17:53:05 UTC (rev 4925)
@@ -24,7 +24,7 @@
TARGET=/usr/PCBSD/lifePreserver/bin/life-preserver
scripts.path=/usr/PCBSD/lifePreserver/scripts
-scripts.files=scripts/setup-ssh-keys.sh scripts/start-backup.sh
+scripts.files=scripts/setup-ssh-keys.sh scripts/start-backup.sh scripts/functions.sh scripts/backup-rsync.sh
conf.path=/usr/PCBSD/lifePreserver/conf
conf.files=conf/rsync-excludes
Modified: pcbsd/trunk/lifePreserver/lifePreserverMain.cpp
===================================================================
--- pcbsd/trunk/lifePreserver/lifePreserverMain.cpp 2009-11-09 10:03:34 UTC (rev 4924)
+++ pcbsd/trunk/lifePreserver/lifePreserverMain.cpp 2009-11-09 17:53:05 UTC (rev 4925)
@@ -76,8 +76,8 @@
}
QString removeHost = treePreservers->currentItem()->text(0);
- QString cmd = "rm -rf '" + LIFEPRESERVERSDIR + "/" + removeHost + "'";
- system(cmd.toLatin1());
+ PRESERVER myPreserver(removeHost);
+ myPreserver.remove();
// Reload our list
loadPreservers();
@@ -160,15 +160,26 @@
QString lifePreserver::getPreserverLastBackup(QString host)
{
- return QString("Unknown");
+ PRESERVER myPreserver(host);
+ return myPreserver.getLastBackup();
}
QString lifePreserver::getPreserverFrequency(QString host)
{
- return QString("Not scheduled");
+ PRESERVER myPreserver(host);
+ return myPreserver.getFrequency();
}
QString lifePreserver::getPreserverStatus(QString host)
{
- return QString("Not Running");
+ QString result;
+ PRESERVER myPreserver(host);
+ int pid = myPreserver.getPID();
+ if ( pid == -1 )
+ {
+ result="Not running";
+ } else {
+ result="Running";
+ }
+ return result;
}
Modified: pcbsd/trunk/lifePreserver/lifePreserverWizard.cpp
===================================================================
--- pcbsd/trunk/lifePreserver/lifePreserverWizard.cpp 2009-11-09 10:03:34 UTC (rev 4924)
+++ pcbsd/trunk/lifePreserver/lifePreserverWizard.cpp 2009-11-09 17:53:05 UTC (rev 4925)
@@ -157,11 +157,36 @@
// Function which saves the Preserver into the registry
void lifePreserverWizard::savePreserver()
{
+ int hour;
+ QString tmp, tmp2;
+
QString connectString = lineUserName->text() + "@" + lineHostName->text();
PRESERVER myPreserver(connectString);
myPreserver.setHost(connectString);
myPreserver.setPort(22);
+ myPreserver.setType("rsync");
+ myPreserver.setNumBackups(7);
+ if ( radioDaily->isChecked() )
+ {
+ hour = spinDailyHour->value();
+ if ( comboDailyTime->currentIndex() == 1 )
+ hour = hour + 12;
+ if ( hour == 24)
+ hour = 0;
+ myPreserver.setCron(tmp.setNum(hour));
+ }
+
+ if ( radioWeekly->isChecked() )
+ {
+ hour = spinWeeklyHour->value();
+ if ( comboWeeklyTime->currentIndex() == 1 )
+ hour = hour + 12;
+ if ( hour == 24)
+ hour = 0;
+ myPreserver.setCron(tmp.setNum(hour), tmp2.setNum(comboWeeklyDay->currentIndex()) );
+ }
+
}
bool lifePreserverWizard::doesPreserverExist()
Modified: pcbsd/trunk/lifePreserver/lifePreserverWizard.ui
===================================================================
--- pcbsd/trunk/lifePreserver/lifePreserverWizard.ui 2009-11-09 10:03:34 UTC (rev 4924)
+++ pcbsd/trunk/lifePreserver/lifePreserverWizard.ui 2009-11-09 17:53:05 UTC (rev 4925)
@@ -299,6 +299,11 @@
<widget class="QComboBox" name="comboWeeklyDay">
<item>
<property name="text">
+ <string>Sunday</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
<string>Monday</string>
</property>
</item>
@@ -327,11 +332,6 @@
<string>Saturday</string>
</property>
</item>
- <item>
- <property name="text">
- <string>Sunday</string>
- </property>
- </item>
</widget>
</item>
<item>
Modified: pcbsd/trunk/lifePreserver/preserver.cpp
===================================================================
--- pcbsd/trunk/lifePreserver/preserver.cpp 2009-11-09 10:03:34 UTC (rev 4924)
+++ pcbsd/trunk/lifePreserver/preserver.cpp 2009-11-09 17:53:05 UTC (rev 4925)
@@ -33,17 +33,125 @@
PRESERVER::PRESERVER(QString name) : QObject()
{
connectHost = name;
+
+ // Create our new Preserver Directory
+ QDir newPreserverDir;
+ newPreserverDir.setPath(LIFEPRESERVERSDIR + "/" + connectHost);
+ if ( ! newPreserverDir.exists() )
+ {
+ newPreserverDir.mkpath(newPreserverDir.path());
+ }
+
}
PRESERVER::~PRESERVER()
{
}
+// Function which sets the backup to run daily at certian hour
+void PRESERVER::setCron(QString hour)
+{
+ removeCron();
+
+ // Save the updated config
+ QFile cronFile( "/etc/crontab" );
+ if ( cronFile.open( QIODevice::WriteOnly | QIODevice::Append) )
+ {
+ QTextStream streamout( &cronFile );
+ streamout << "0 " << hour << " * * *" \
+ << " root " << LIFEPRESERVERPATH << "/scripts/start-backup.sh " << connectHost << "\n";
+ cronFile.close();
+ }
+
+ // Restart cron now
+ system("/etc/rc.d/cron restart");
+
+}
+
+// Function which sets the backup to run weekly on certian day / hour
+void PRESERVER::setCron(QString hour, QString day)
+{
+ removeCron();
+
+ // Save the updated config
+ QFile cronFile( "/etc/crontab" );
+ if ( cronFile.open( QIODevice::WriteOnly | QIODevice::Append) )
+ {
+ QTextStream streamout( &cronFile );
+ streamout << "0 " << hour << " * * " << day \
+ << " root " << LIFEPRESERVERPATH << "/scripts/start-backup.sh " << connectHost << "\n";
+ cronFile.close();
+ }
+
+ // Restart cron now
+ system("/etc/rc.d/cron restart");
+
+}
+
+// Function which removes this preservers settings from the app
+void PRESERVER::remove()
+{
+ removeCron();
+
+ if ( ! connectHost.isEmpty() )
+ {
+ QString cmd = "rm -rf '" + LIFEPRESERVERSDIR + "/" + connectHost + "'";
+ system(cmd.toLatin1());
+ }
+
+}
+
+// Function which removes a cron entry for this backup
+void PRESERVER::removeCron()
+{
+ QStringList updatedCron;
+
+ QFile cronFile( "/etc/crontab" );
+ if ( cronFile.open( QIODevice::ReadOnly ) )
+ {
+ QTextStream stream( &cronFile );
+ QString line;
+ while ( !stream.atEnd() ) {
+ line = stream.readLine(); // line of text excluding '\n'
+ // Check if we have a match for this backup
+ if ( line.indexOf(connectHost) == -1 ) {
+ updatedCron << line;
+ }
+ }
+ cronFile.close();
+ }
+
+ // Save the updated config
+ if ( cronFile.open( QIODevice::WriteOnly ) )
+ {
+ QTextStream streamout( &cronFile );
+ for (int i = 0; i < updatedCron.size(); ++i)
+ {
+ streamout << updatedCron.at(i) << "\n";
+ }
+ cronFile.close();
+ }
+
+ // Restart cron now
+ system("/etc/rc.d/cron restart");
+
+}
+
void PRESERVER::setHost(QString userHost)
{
setConfValue("host", userHost);
}
+void PRESERVER::setType(QString type)
+{
+ setConfValue("type", type);
+}
+
+void PRESERVER::setNumBackups(int num)
+{
+ setConfValue("keepbackups", num);
+}
+
void PRESERVER::setPort(int port)
{
setConfValue("port", port);
@@ -54,25 +162,75 @@
return getConfValue("host").toString();
}
+int PRESERVER::getNumBackups()
+{
+ return getConfValue("keepbackups").toInt();
+}
+
int PRESERVER::getPort()
{
return getConfValue("port").toInt();
}
-QVariant PRESERVER::getConfValue(QString key)
+// Function which returns the last backup status + date
+QString PRESERVER::getLastBackup()
{
- QString value;
+ QString result = "Unknown";
- // Create our new Preserver Directory
- QDir newPreserverDir;
- newPreserverDir.setPath(LIFEPRESERVERSDIR + "/" + connectHost);
- if ( ! newPreserverDir.exists() )
+ // Read in the result file
+ QFile pidFile( LIFEPRESERVERSDIR + "/" + connectHost + "/last-result" );
+ if ( pidFile.open( QIODevice::ReadOnly ) )
{
- newPreserverDir.mkpath(newPreserverDir.path());
+ QTextStream stream( &pidFile );
+ QString line;
+ while ( !stream.atEnd() ) {
+ line = stream.readLine(); // line of text excluding '\n'
+ // Return the first line
+ return line;
+ }
}
+ return result;
+}
+
+// Function which returns the cron status of this backup
+QString PRESERVER::getFrequency()
+{
+ QString schedule = "Unknown";
+ return schedule;
+}
+
+// Function which returns the PID of the current backup, or -1 if none is running
+int PRESERVER::getPID()
+{
+ bool ok;
+ int pid;
+
+ // Read in the pid file
+ QFile pidFile( LIFEPRESERVERSDIR + "/" + connectHost + "/preserver.pid" );
+ if ( pidFile.open( QIODevice::ReadOnly ) )
+ {
+ QTextStream stream( &pidFile );
+ QString line;
+ while ( !stream.atEnd() ) {
+ line = stream.readLine(); // line of text excluding '\n'
+ pid = line.toInt(&ok);
+ if ( ok )
+ {
+ return pid;
+ }
+ }
+ }
+
+ return -1;
+}
+
+QVariant PRESERVER::getConfValue(QString key)
+{
+ QString value;
+
// Read in the old config and change the target key
- QFile confFile( newPreserverDir.path() + "/settings.conf" );
+ QFile confFile( LIFEPRESERVERSDIR + "/" + connectHost + "/settings.conf" );
if ( confFile.open( QIODevice::ReadOnly ) )
{
QTextStream stream( &confFile );
Modified: pcbsd/trunk/lifePreserver/preserver.h
===================================================================
--- pcbsd/trunk/lifePreserver/preserver.h 2009-11-09 10:03:34 UTC (rev 4924)
+++ pcbsd/trunk/lifePreserver/preserver.h 2009-11-09 17:53:05 UTC (rev 4925)
@@ -39,13 +39,24 @@
//Getters
virtual QString getHost();
virtual int getPort();
+ virtual int getNumBackups();
+ virtual QString getLastBackup();
+ virtual QString getFrequency();
+ virtual int getPID();
//Setters
virtual void setHost(QString userhost);
virtual void setPort(int port);
+ virtual void setNumBackups(int num);
+ virtual void setType(QString type);
+ virtual void setCron(QString hour);
+ virtual void setCron(QString hour, QString day);
+ virtual void remove();
+
private:
void setConfValue(QString key, QVariant value);
+ virtual void removeCron();
QVariant getConfValue(QString key);
QString connectHost;
Property changes on: pcbsd/trunk/lifePreserver/scripts/backup-rsync.sh
___________________________________________________________________
Added: svn:executable
+ *
Modified: pcbsd/trunk/lifePreserver/scripts/start-backup.sh
===================================================================
--- pcbsd/trunk/lifePreserver/scripts/start-backup.sh 2009-11-09 10:03:34 UTC (rev 4924)
+++ pcbsd/trunk/lifePreserver/scripts/start-backup.sh 2009-11-09 17:53:05 UTC (rev 4925)
@@ -1,67 +1,33 @@
#!/bin/sh
# Script for Life-Preserver which starts a backup on the specified host
###########################################################################
-SSHHOST=$1
+PRESERVER=$1
-if [ -z "${SSHHOST}" ]
+if [ -z "${PRESERVER}" ]
then
echo "ERROR: Usage start-backup.sh <user>@<host>"
exit 150
fi
-DATE=`date "+%Y-%m-%dT%H_%M_%S"`
PROGDIR="/PCBSD/lifePreserver"
-HOSTDIR="${PROGDIR}/preservers/${SSHHOST}"
-BACKDIR="life-preserver"
-BACKLOG="${HOSTDIR}/logs/${SSHHOST}${DATE}.log"
-EXCLUDELIST="/PCBSD/lifePreserver/conf/rsync-excludes"
+PDIR="${PROGDIR}/preservers/${PRESERVER}"
+SETTINGS="${PDIR}/settings.conf"
+# Source our functions
+. ${PROGDIR}/scripts/functions.sh
+
# Make sure we've been given a valid life preserver
-if [ ! -d "${HOSTDIR}" ]
+if [ ! -d "${PDIR}" ]
then
- echo "ERROR: Invalid preserver: ${SSHHOST}"
+ echo "ERROR: Invalid preserver: ${PHOST}"
exit 1
fi
-# Make sure this host has its logs dir
-if [ ! -d "${HOSTDIR}/logs" ]
-then
- mkdir ${HOSTDIR}/logs
-fi
-
-# Make sure we have the BACKDIR created
-ssh ${SSHHOST} "mkdir -p ${BACKDIR}"
-
-rsync -avvz \
- --delete \
- --delete-excluded \
- --rsync-path="rsync --fake-super" \
- --perms --chmod=o-t \
- --log-file=${BACKLOG} \
- --exclude-from ${EXCLUDELIST} \
- -e ssh \
- --link-dest=../current \
- / ${SSHHOST}:${BACKDIR}/incomplete_back-$DATE \
- && ssh ${SSHHOST} \
- "mv ${BACKDIR}/incomplete_back-$DATE ${BACKDIR}/back-$DATE \
- && rm -f ${BACKDIR}/current \
- && ln -s back-${DATE} ${BACKDIR}/current"
-
+cat ${SETTINGS} | grep "type: rsync" >/dev/null 2>/dev/null
if [ "$?" = "0" ]
then
- bzip2 ${BACKLOG}
- # Success!
- # Copy the logfile to the backup dir
-sftp ${SSHHOST} << EOF
-cd ${BACKDIR}/current
-put ${BACKLOG}.bz2 backup.log.bz2
-quit
-EOF
-
+ ${PROGDIR}/scripts/backup-rsync.sh ${PRESERVER}
else
- # Failed backup!
- echo "ERROR: Backup failed!"
-
+ ${PROGDIR}/scripts/backup-tar.sh ${PRESERVER}
fi
-
More information about the Commits
mailing list