-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbsaf.pl
99 lines (97 loc) · 3.33 KB
/
bsaf.pl
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
#!/usr/bin/perl
# ======================================================================
#
# Perl Source File -- Created with BurnSoft BurnPad
#
# NAME: <bsaf.pl>
#
# AUTHOR: BurnSoft www.burnsoft.net
# DATE : 10/24/2008
#
# PURPOSE: This script was created to help automate the automatic archiving of
# MySQL Backups on a Linux Machine. It will copy the *.sql files
# to a sub folder that is named after the current year and month,
# then after it copies all the files it will compress that directory
# into 1 file. If was designed to run daily before the end of the day
# so if the year_month.tar.gz exists, it will append to that file.
# BurnSoft Archive Files (bsaf)
#
# ======================================================================
use File::Path;
use Net::FTP;
my $VERSION = '1.0.2'; #Version of Script
my $foldername =""; #place holder
my $destdir ="/shares/backups/MySQL/archived"; #Destination Directory
my $sourcedir ="/shares/backups/MySQL/*.sql"; #Source Directory with File Extension
my $DEBUG =0; #Debug Mode (Display Messages)
my $MOVEONLY =1; #Enable Move Files
my $DOZIP =1; #Enable Zip compression on folder
my $DOCLEANUP =1; #Delete the year_month folder after compressing
my $DestDirFinal=""; #place holder
my $ZipDest =""; #place holder
&GetDataFormat;
$DestDirFinal= "$destdir\/$foldername/";
$ZipDest= "$destdir\/$foldername.tar";
&ShowDebug("New Backup Directory is $DestDirFinal\n");
&ShowDebug("New Zipped Archived is $ZipDest\n");
if ($MOVEONLY) {
&CreateDirectory;
&MoveFiles;
}
if ($DOZIP) {
&ZipDir;
}
if ($DOCLEANUP) {
&DeleteDirectory;
}
# ======================================================================
sub GetDataFormat
{
$date_time = localtime($^T);
$vmonth = substr($date_time, 4, 3);
$vyear = substr($date_time, 20, 4);
$foldername = "$vyear\_$vmonth";
}
# ======================================================================
sub CreateDirectory
{
my @created = mkpath($DestDirFinal,{verbose => 1, mode => 0750},);
print "created $_\n" for @created;
}
# ======================================================================
sub DeleteDirectory
{
rmtree( $DestDirFinal, {keep_root => 1} );
}
# ======================================================================
sub MoveFiles
{
system("mv $sourcedir $DestDirFinal");
}
# ======================================================================
sub ZipDir
{
if (-w "$ZipDest.gz")
{
&ShowDebug("File exist! Running gunzip\n");
system("gunzip $ZipDest.gz");
&ShowDebug("Appending extra files to current archive.\n");
system("tar -uvf $ZipDest $DestDirFinal");
#print "File does not exist!\n";
}
else {
#print "File exist!\n";
&ShowDebug("File does not exist!\n");
&ShowDebug("Creating new archive.\n");
system("tar -cvf $ZipDest $DestDirFinal");
}
system("gzip -f $ZipDest");
}
# ======================================================================
sub ShowDebug{
($msg) = @_;
if ($DEBUG){
print "DEBUG: $msg\n";
}
}
# ======================================================================