backup.in 1 KB
Newer Older
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
#!/usr/bin/perl -wT
use English;

#
# Configure variables
#
my $TB		= "@prefix@";
my $DBNAME	= "@TBDBNAME@";

my $BACKUPDIR	= "$TB/backup";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Only real root can call this.
# 
if ($UID != 0) {
    print STDERR "You must be root to run this script!\n";
    exit(-1);
}

if (! chdir($BACKUPDIR)) {
    print STDERR "Could not chdir to $BACKUPDIR!\n";
    exit(-1);
}

#
# Format the name of the backup with date.
# Untaint it since it was constructed with date. Dopey.
# 
my $name = "tbdb-" . `date +20%y%m%d-%H.%M.%S`;

if ($name =~ /^([-\@\w.]+)$/) {
    $name = $1;
}

#
# Do a mysqldump. This will reset the log files.
#
if (system("mysqldump --all --flush-logs --lock-tables $DBNAME > $name")) {
    print STDERR "mysqldump failed!\n";
    exit(1);
}

#
# Compress it.
#
if (system("gzip $name")) {
    print STDERR "gzip failed!\n";
    exit(1);
}

print STDOUT "DB backup ($DBNAME) complete!\n";
exit 0;