1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
#!@PERL@
$prefix = @prefix@;
# Usage: backup [-<option>] [userfile]
# backup v0.2 - backup fileareas using tar
# last updated: 29/o8 1995 audun
# options: -t tapebackup (flag)
# -s scan userdir (and backup all users)
# -f destination dir
# -d recursive backup of this directory
# -x exclude file (default = /local/mutils/bin/backup.exclude)
# -a add directory to default (or userfile)
# -p fix protection bits (flag)
# -e emulate tar (no backup)
# --help display help
# example: backup -f /spare/backup -d /iu/nexus/u1 -d /iu/nexus/u2
# backup -s /iu/nexus/u1
# todo: check filnavn, check hdplass->mail drift
# tar: tar -cvX ./exclude -f hei qwk
# defaults
$backupgroup = "iu";
$exclude = "@prefix@/lib/cfengine/bin/backup.exclude";
$destination = "/iu/nexus/backup";
$sourcedir = "/iu/nexus/u1/ /iu/nexus/u2/";
$gnutar = "@prefix@/bin/tar --atime-preserve -czvpX $exclude -f ";
$chown = "@prefix@/bin/chown";
$chgrp = "@prefix/bin/chgrp";
$chmod = "@prefix@/bin/chmod";
# exlude these users (-s option)
$excludeusers = 'ftp ide lost+found sdt';
@excludeusers = split(" ", $excludeusers);
# options and subprocedures
%optionlist =
(
'-t', 'tape_option',
'-f', 'file_option',
'-d', 'dirs_option',
'-x', 'excl_option',
'-a', 'addd_option',
'-s', 'scan_option',
'-p', 'prot_option',
'-e', 'emul_option',
'--help', 'help_option',
);
# misc
$syntax = "syntax: backup [-<options>] [userfile]";
$options = "options: [-f dir] [-x file] [-d dir] [-a dir] [-s dir] [--help]";
&parse;
&backup;
sub parse
{
# check for arguments
if ($#ARGV >= 0)
{
undef $userfile;
undef $newdir;
undef $prot_enable;
undef $emul_enable;
# parse all supplied arguments
for ($arg = 0; $arg <= $#ARGV; $arg++)
{
if (substr($ARGV[$arg], 0, 1) ne "-")
{
&userfile;
}
else
{
&options;
}
}
}
}
sub backup
{
# add datestamp to backupfile
($sec, $min, $hour, $mday, $mon, $year) = localtime(time);
$mon++;
$destdate = "backup.$mday.$mon.$year";
# check for existing file (not bloody likely)
# die "error: backup file already exists!\n" if (-e "$destination");
# check for directory override (-d option)
$sourcedir = $newdir if ($newdir);
# for every defined dir
@sourcedir = split(" ", $sourcedir);
foreach $sourcedir (@sourcedir)
{
# replace "/" with "."
($destpostfix = $sourcedir) =~ tr/\//./;
if (substr($destpostfix, 0, 1) eq ".")
{
$destpostfix = substr($destpostfix, 1, length($destpostfix));
}
if (substr($destpostfix, -1, 1) eq ".")
{
chop($destpostfix);
}
if (!$emul_enable)
{
system "$gnutar $destination/$destdate.$destpostfix $sourcedir >/dev/null 2>&1";
}
else
{
print "$gnutar $destination/$destdate.$destpostfix $sourcedir\n";
}
}
# do protection bits
&fixprotection if ($prot_enable);
}
sub userfile
{
# check for excessive use of arguments!
die "too many arguments!\n$syntax\n" if ($userfile);
$userfile = $ARGV[$arg];
# throw away default dirs
undef $sourcedir;
# load list of userdirs
open (IN, "$userfile");
while ($line = <IN>)
{
if ($line && substr($line, 0, 1) ne "\n")
{
# check for leading / in absolute pathname
die "userfile error: no leading / in absolute pathname\n" if (substr($line, 0, 1) ne "/");
chop($line) if (substr($line, -1) eq "\n");
$sourcedir .= $line;
$sourcedir .= " ";
}
}
close(IN);
}
sub options
{
# check supplied options and sub to command if defined
die "option not defined!\n$options\n" if (!$optionlist{$ARGV[$arg]});
$sub = $optionlist{$ARGV[$arg]};
&$sub;
}
sub prot_option
{
$prot_enable = 1;
}
sub emul_option
{
$emul_enable = 1;
}
sub fixprotection
{
opendir(SDIR, "$destination/");
# reading the directory, scanning for backup
@backupfiles = grep(/^backup./, readdir(SDIR));
foreach $file (@backupfiles)
{
# can't split on "." (won't work)
for ($i = length($file); $i > 0; $i--)
{
last if (substr($file, $i, 1) eq ".");
}
$owner = substr($file, $i + 1, (length($file) - $i));
$bfile = $defsource;
$bfile .= $file;
$file = $bfile;
system "$chown $owner $destination/$file";
system "$chmod 600 $destination/$file";
system "$chgrp $backupgroup $destination/$file";
}
closedir(SDIR);
}
sub scan_option
{
# scan userdirs
$arg++;
# check for syntax error
$next = substr($ARGV[$arg], 0, 1);
die "directory name missing!\n$options\n" if ($next eq "-") || ($next eq "");
# read directory entries
opendir(SDIR, $ARGV[$arg]);
# reading the userdirectory, avoding . and .*
@allusers = grep(!/^\.\.*/, readdir(SDIR));
closedir(SDIR);
undef $sourcedir if (!$scan);
$scan = 1;
# checking the userdirectory for users to exclude
foreach $user (@allusers)
{
$found = 0;
foreach $xuser (@excludeusers)
{
if ($user eq $xuser)
{
# entry will be excluded
$found = 1;
last;
}
}
if ($found == 0)
{
# add this entry to the sourcedir list
$newuser = $ARGV[$arg];
chop($newuser) if (substr($newuser, -1, 1) eq "\n");
$newuser .= "/" if (substr($newuser, -1, 1) ne "/");
$newuser .= $user;
$newuser .= " ";
$sourcedir .= $newuser;
}
}
}
sub tape_option
{
# backup to tape
die "function not yet implemented, sorry!\n$options\n";
}
sub file_option
{
# set destination
$arg++;
# check for syntax error
die "filename missing!\n$options\n" if (substr($ARGV[$arg], 0, 1) eq "-");
$destination = $ARGV[$arg];
}
sub dirs_option
{
$arg++;
# check for syntax error
die "directory name missing!\n$options\n" if (substr($ARGV[$arg], 0, 1) eq "-");
# add this directory to multiple directory list
$newdir .= $ARGV[$arg];
$newdir .= " ";
}
sub excl_option
{
$arg++;
# check for syntax error
die "exclude name missing!\n$options\n" if (substr($ARGV[$arg], 0, 1) eq "-");
# use this as exclude file for tar
$exclude = $ARGV[$arg];
}
sub addd_option
{
$arg++;
# add a directory to backup in addition to default (or userfile) dirs
# check for syntax error
die "directory name missing!\n$options\n" if (substr($ARGV[$arg], 0, 1) eq "-");
# add this directory to multiple directory list
$sourcedir .= $ARGV[$arg];
}
sub help_option
{
print "Usage: backup [-<options>] [userfile]\n";
print "backup v0.2 - backup fileareas using tar\n";
print "last updated: 29/o8 1995 audun\n";
print "options: -t tapebackup (flag)\n";
print " -f destination dir\n";
print " -d recursive backup of this directory\n";
print " -x exclude file (default = /local/mutils/bin/backup.exclude)\n";
print " -a add directory to default (or userfile)\n";
print " -s userdirectory\n";
print " -p fix protection bits (flag)";
print " -e emulate tar (no backup)";
print " --help display help\n";
print "example: backup -f /spare/backup -d /iu/nexus/u1 -d /iu/nexus/u2\n";
print " backup -s /iu/nexus/u1\n";
exit;
}
# only amiga makes it possible!
|