User Tools

Site Tools


scripts:perl:format-backup-disk.pl
format-backup-disk.pl
#!/usr/bin/perl
# Written by Thomas York
# This script takes a single argument, which is the filesystem type.
# As of now, this script only supports ext3 and jfs
#
# Version 0.0.1
 
# Use
use strict;
use warnings;
use Switch;
use Term::Prompt;
 
# Variables
my $version="0.0.1";
my $filesystem="";
my $devnode="";
my $fdiskinstructions="o\nn\np\n1\n\n\n\nw\nq";
my $fdiskfile="tmpfdisk.txt";
my $logfile="format-log.txt";
 
# Some information...
print "format-backup-disks.pl v" . $version . "\n";
print "Written by Thomas York\n";
print "Last modified date : 09/12/2011\n\n";
 
# See if the user passed the file system type
if (@ARGV < 2) {
	# User didn't pass anything..
	print "ERROR : You didn't pass a valid filesystem and/or device node!\n";
	exit();
} else {
	# Let's go ahead and parse what the user gave us..
	switch (lc($ARGV[0])) {
		# EXT3
		case "ext3" {
			$filesystem = "ext3";
		}
 
		# JFS
		case "jfs" {
			$filesystem = "jfs";
		}
 
		# Anything else
		else {
			print "ERROR : You didn't pass a valid filesystem!\n";
			exit();
		}
	}
 
 
	# Check to see if the backup disk is present
	if (!-e $ARGV[1]) {
		print "ERROR : There isn't a valid disk in the dock!\n";
		exit();
	} else {
		$devnode = $ARGV[1];
	}
}
 
# Present the user with our current config and confirm that they want to format the disk
print "This is the current running config...\n";
print "Device node : " . $devnode . "\n";
print "Filesystem : " . $filesystem . "\n";
 
# Prompt
my $answer = &prompt("y", "Are you sure you want to format this drive?", "", "n");
 
if (!$answer) {
	# They selected no
	print "Operation cancelled. Exiting...\n";
	exit();
}
 
# Write fdisk instructions to file
print "\nWriting instructions to file, please wait...\n";
open (FDISKFILE, '>' . $fdiskfile);
print FDISKFILE $fdiskinstructions;
close (FDISKFILE);
 
# Partition the drive
print "Repartioning the drive, please wait...\n";
my $tmpfdiskoutput = `/sbin/fdisk $devnode < $fdiskfile &>> $logfile`;
 
# Delete the instructions file
print "Cleaning up, please wait...\n";
unlink ($fdiskfile);
 
# Format the new partition
print "Writing file system to disk, please wait...\n";
my $fsnode = $devnode . "1";
my $tmpmkfsoutput = `/sbin/mkfs.$filesystem -q $fsnode &>> $logfile`;
 
# exit
print "Format of disk complete! Have a nice day!\n";
exit();
/var/www/vhost/www.fuhell.com/data/pages/scripts/perl/format-backup-disk.pl.txt · Last modified: 2014/09/01 15:00 (external edit)