Hello!

Here are the scripts I use to manage the removable hard drive media used 
to store daily archives of my backup servers via the GUI, instead of 
from the command line.  Briefly, the system is set up like this:

BackupPC's pool is stored on a large internal hard drive.  Every day at 
a little after 7:00 A.M., the backup server starts an archive of each 
host, which is stored on a second hard drive that is mounted in a 
removable tray.  Once this is complete, the user can shut down the 
server, remove the hard drive, replace it with a different one, and turn 
the server back on.  Once the new drive is in place, it is 
repartitioned, reformatted and remounted in place, ready for the next 
archive.

There are two scripts that make this happen.  The first one simply shuts 
the server down.  The second one handles the repartitioning, 
reformatting and remounting.

There is absolutely no reason why this couldn't be handed by simply 
ssh'ing into the server.  Except that these servers are destined for 
network administrators for whom the command line is a tremendously evil 
thing, and if you try to sell them a solution that contains instructions 
like "Use putty, log into the server and type this command", they will 
say no.  Hence, the CGI scripts...

Because the scripts will be run by the webserver, they will be run with 
its permissions, which likely do not include the ability to shut down 
the server, or other such commands.  The way I have done this is to use 
sudo, with the proper lines in sudoers.  I've tried to make the commands 
as specific as posssible, to avoid possible security issues.

To partition the drive, I am echoing responses to the fdisk command.  I 
looked into parted, but I could not find a clean way of getting it to 
create a single large partition without knowing how big the partition 
was.  Seeing as this will be used with drives of different sizes, I 
decided to stick with fdisk.

Also, the HTML files that are cat to the user are created simply by 
saving any old BackupPC HTML page to a file, and chopping the part 
before the main body DIV into the top file, and the part after the main 
body DIV into the bottom file.

If you have any suggestions as to how to make this script better, I 
would be happy to hear them.  Otherwise, I hope they are useful to 
someone else.

Tim Massey




#!/bin/sh
# shutdown.cgi - Shut down server
echo "Content-type: text/html"
echo ""
cat bpc_top.html
echo "
<div id=\"Content\">
<div class=\"h1\">Shut Down Server</div>
<p><h2>The system is being shut down!</h2></p>
<p>This will take approximately 60 seconds. Do not remove the drive before
the system has powered itself off.</p>"
cat bpc_bottom.html
sudo /sbin/shutdown -h now >/dev/null 2>&1
exit



#!/bin/sh
# instmedia.cgi - Install new media for BackupPC Archive
echo "Content-type: text/html"
echo ""
cat bpc_top.html
echo "
<div id=\"Content\">
<div class=\"h1\">Initialize Removable Media</div>
<p><h2>Initializing Removable Media</h2></p>
<p>This will take approximately 10 minutes to complete, depending upon the
size of the removable drive.  Do not naviagate away from this page.</p>"

echo "<P><B>Unmounting removable drive.</B></P>"
sudo /bin/umount /var/lib/BackupPC/removable 2>&1
if [ `sudo /bin/df /var/lib/BackupPC/removable | grep 
"/var/lib/BackupPC/removable" | wc -l` = "1"  ]; then
   echo "<P><H2>Error:  drive did not unmount.</H2></P>"
   cat bpc_bottom.html
   exit
fi

echo "<P><B>Creating proper partition on drive.</B></P>
<P>This will take approximately 45 seconds.  Please wait.</P>"
# Pipe responses for fdisk command via echo.
# This does the following:
#  The first series of lines will delete all partitions on a drive with 
up to 9
#   partitions.  It does this by having pairs of delete commands:  d9, 
d8, etc.
#   until it gets to the end.  When there's just one partition, fdisk
#   doesn't ask for a number, so the last one is just a d.
#   This will actually generate lots of errors in practice:  when there 
are no
#   more partitions left, the d's will generate an error saying that 
there are
#   no partitions to delete, and the numbers are interpreted as nonsense
#   commands.  However, this is harmless.
#  It then goes through the sequence to create a new partition:
#   n (New partition)
#   p (Primary parition)
#   1 (First partition)
#   <ENTER> (Default starting cylinder is the first one)
#   <ENTER> (Default ending cylinder is the last one)
#   w (Write the changes to disk and exit)
#  Several newlines are added at the end in case something goes wrong.
#  Three newlines in a row is interpreted by the fdisk command by
#  exiting immediately.
echo "

d
9
d
8
d
7
d
6
d
5
d
4
d
3
d
2
d
n
p
1


w




" | sudo /sbin/fdisk /dev/hdc >/dev/null

echo "<P><B>Formatting partition for use.</B></P>
<P>This can take up to 10 minutes.  Please wait.</P>"
sudo /sbin/mke2fs -j -m 1 -LRemovableData /dev/hdc1 >/dev/null

echo "<P><B>Mounting drive.</B></P>"
sudo /bin/mount /var/lib/BackupPC/removable 2>&1
if [ `sudo /bin/df /var/lib/BackupPC/removable | grep 
"/var/lib/BackupPC/removable" | wc -l` = "0"  ]; then
   echo "<P><H2>Error:  Drive did not mount.</H1></P>"
   cat bpc_bottom.html
   exit
fi

echo "<P><B>Setting permissions.</B></P>"
sudo /bin/chmod -R 750 /var/lib/BackupPC/removable 2>&1
sudo /bin/chown -R backuppc:backuppc /var/lib/BackupPC/removable 2>&1
echo "<P><H2>Drive change complete!</H2></P>"
cat bpc_bottom.html
exit


Append the following to /etc/sudoers:

# Lines added for BackupPC removable media GUI
apache,backuppc        ALL=(root)  NOPASSWD:  /sbin/shutdown -h now
apache,backuppc        ALL=(root)  NOPASSWD:  /bin/umount 
/var/lib/BackupPC/removable
apache,backuppc        ALL=(root)  NOPASSWD:  /bin/mount 
/var/lib/BackupPC/removable
apache,backuppc        ALL=(root)  NOPASSWD:  /sbin/fdisk /dev/hdc
apache,backuppc        ALL=(root)  NOPASSWD:  /sbin/mke2fs -j -m 1 
-LRemovableData /dev/hdc1
apache,backuppc        ALL=(root)  NOPASSWD:  /bin/chmod -R 750 
/var/lib/BackupPC/removable
apache,backuppc        ALL=(root)  NOPASSWD:  /bin/chown -R 
backuppc:backuppc /var/lib/BackupPC/removable
apache,backuppc        ALL=(root)  NOPASSWD:  /bin/df 
/var/lib/BackupPC/removable

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
BackupPC-users mailing list
BackupPC-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/backuppc-users
http://backuppc.sourceforge.net/

Reply via email to