From fcb6d0ed35fc19e0466b385ba125df5902709191 Mon Sep 17 00:00:00 2001 From: Jon Bonilla Date: Tue, 20 Apr 2010 12:21:37 +0000 Subject: [PATCH] * Improvements to sipwise-cleanup-tools Changed path for the perl scripts to /usr/sbin Cron file added Configuration file added --- acc_backup.pl | 73 +++++++++++++++++++++++----------------------- binlog-purge.pl | 28 ++++++++++++++++-- cleanup-tools.conf | 40 +++++++++++++++++++++++++ cleanup-tools.cron | 2 ++ debian/changelog | 8 +++++ debian/control | 4 +-- debian/rules | 6 ++-- 7 files changed, 119 insertions(+), 42 deletions(-) create mode 100644 cleanup-tools.conf create mode 100644 cleanup-tools.cron diff --git a/acc_backup.pl b/acc_backup.pl index 2f4e44b..47dd2ce 100755 --- a/acc_backup.pl +++ b/acc_backup.pl @@ -4,38 +4,37 @@ use strict; use warnings; use DBI; -# backing up all data older than $MONTHS months -# Note: if set for example to 1, this will backup the overall month, -# not only the records older than 30 days. This logic applies generally, -# so it will go back $MONTHS months, and starting from there, backing up -# the full months back to $MONTHS_BACK -my $MONTHS = 3; -# from the starting month above, also go back $MONTHS_BACK months and -# backup them -my $MONTHS_BACK = 6; - -# archive tables older than $ARCHIVE_MONTHS will be dumped to a file, -# gzipped and then dropped afterwards -my $ARCHIVE_MONTHS = 12; -# drop the dump files into $ARCHIVE_DIR -my $ARCHIVE_DIR = "/tmp"; - -# accounting database on proxies (openser) or db1 (accounting) -my $ACC_DB = "accounting"; -# accounting tables on proxies (acc) or db1 (acc acc_backup acc_trash) -my @ACC_TABLES = qw(acc acc_backup acc_trash); -# cdr database (accounting) -my $CDR_DB = "accounting"; -# cdr tables on proxies (empty) or db1 (cdr) -my @CDR_TABLES = qw(cdr); - -# how many entries to move and delete at the same time -my $BATCH = 1000; - -# DB access credentials -my $DBUSER = "root"; -my $DBPASS = "1freibier!"; -my $DBHOST = "localhost"; +our $MONTHS; +our $MONTHS_BACK; +our $ARCHIVE_MONTHS; +our $ARCHIVE_DIR; +our $ACC_DB; +our $ACC_TABLES; +our $CDR_DB; +our $CDR_TABLES; +our $BATCH; +our $DBUSER; +our $DBPASS; +our $DBHOST; + + +my $config_file = "/etc/cleanup-tools.conf"; +open CONFIG, "$config_file" or die "Program stopping, couldn't open the configuration file '$config_file'.\n"; + +while () { + chomp; # no newline + s/#.*//; # no comments + s/^\s+//; # no leading white + s/\s+$//; # no trailing white + next unless length; # anything left? + my ($var, $value) = split(/\s*=\s*/, $_, 2); + no strict 'refs'; + $$var = $value; + print "El valor de $var es $value - $$var\n" +} +close CONFIG; + + ######################################################################## @@ -119,8 +118,10 @@ sub backup_table { ######################################################################## -backup_table(\@ACC_TABLES, $ACC_DB, "time") or die(); -backup_table(\@CDR_TABLES, $CDR_DB, "start_time") or die(); -archive_dump(\@ACC_TABLES, $ACC_DB); -archive_dump(\@CDR_TABLES, $CDR_DB); + +backup_table($ACC_TABLES, $ACC_DB, "time") or die(); +backup_table($CDR_TABLES, $CDR_DB, "start_time") or die(); + +archive_dump($ACC_TABLES, $ACC_DB); +archive_dump($CDR_TABLES, $CDR_DB); diff --git a/binlog-purge.pl b/binlog-purge.pl index 53b0b27..c684f32 100755 --- a/binlog-purge.pl +++ b/binlog-purge.pl @@ -5,9 +5,33 @@ use warnings; use POSIX; use DBI; +our $DBUSER; +our $DBPASS; +our $DBREMOTEUSER; +our $DBREMOTEPASS;; + + +my $config_file = "/etc/cleanup-tools.conf"; +open CONFIG, "$config_file" or die "Program stopping, couldn't open the configuration file '$config_file'.\n"; + +no strict 'refs'; +while () { + chomp; # no newline + s/#.*//; # no comments + s/^\s+//; # no leading white + s/\s+$//; # no trailing white + next unless length; # anything left? + my ($var, $value) = split(/\s*=\s*/, $_, 2); + $$var = $value; +} +use strict 'refs'; +close CONFIG; + + my (undef, $me) = uname(); -my @creds = qw(dbi:mysql: root 1freibier!); -my @remotecreds = qw(dbi:mysql: replicator wait4Data); +my @creds = 'dbi:mysql:'.",".$DBUSER.",".$DBPASS; +my @remotecreds = 'dbi:mysql:'.",".$DBREMOTEUSER.",".$DBREMOTEPASS; + my $dbh = DBI->connect(@creds) or die; diff --git a/cleanup-tools.conf b/cleanup-tools.conf new file mode 100644 index 0000000..2faa126 --- /dev/null +++ b/cleanup-tools.conf @@ -0,0 +1,40 @@ +#Sipwise cleanup-tools configuration file + + +# Note: if set for example to 1, this will backup the overall month, +# not only the records older than 30 days. This logic applies generally, +# so it will go back $MONTHS months, and starting from there, backing up +# the full months back to $MONTHS_BACK +MONTHS = 3 +# from the starting month above, also go back $MONTHS_BACK months and +# backup them +MONTHS_BACK = 6 + +# archive tables older than $ARCHIVE_MONTHS will be dumped to a file, +# gzipped and then dropped afterwards +ARCHIVE_MONTHS = 12 +# drop the dump files into $ARCHIVE_DIR +ARCHIVE_DIR = /tmp + +# accounting database on proxies (openser) or db1 (accounting) +ACC_DB = accounting +# accounting tables on proxies (acc) or db1 (acc acc_backup acc_trash) +ACC_TABLES = acc,acc_backup,acc_trash +# cdr database (accounting) +CDR_DB = accounting +# cdr tables on proxies (empty) or db1 (cdr) +CDR_TABLES = cdr + +# how many entries to move and delete at the same time +BATCH = 1000 + +# DB access credentials +# Shared by both scripts +DBUSER = root +DBPASS = 1freibier! +DBHOST = localhost + + +# DB access credentials for binlog cleanup +DBREMOTEUSER = replicator +DBREMOTEPASS = wait4Data diff --git a/cleanup-tools.cron b/cleanup-tools.cron new file mode 100644 index 0000000..5ec9ec6 --- /dev/null +++ b/cleanup-tools.cron @@ -0,0 +1,2 @@ +00 03 * * * root /usr/sbin/acc_backup.pl > /dev/null 2>&1 +05 03 * * * root /usr/sbin/binlog-purge.pl > /dev/null 2>&1 diff --git a/debian/changelog b/debian/changelog index 9d164c7..c18b4d8 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,11 @@ +sipwise-cleanup-tools (0.2-1) unstable; urgency=low + + * Config file support added + * Cronjob file added for daily invocation + * Changed install path to /usr/sbin for the scripts + + -- Jon Bonilla Tue, 13 Oct 2009 23:46:31 +0200 + sipwise-cleanup-tools (0.1) unstable; urgency=low * Initial release. diff --git a/debian/control b/debian/control index 35ef0db..d1580b7 100644 --- a/debian/control +++ b/debian/control @@ -11,5 +11,5 @@ Vcs-Browser: https://dev.sipwise.com/svn/voip-tools/cleanup-tools Package: sipwise-cleanup-tools Architecture: all Depends: ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends} -Description: TODO - TODO +Description: Database entry and binlog cleanup + Erase and dump databse entries and mysql binlogs diff --git a/debian/rules b/debian/rules index 1c8eb5e..18f9867 100755 --- a/debian/rules +++ b/debian/rules @@ -30,8 +30,10 @@ install: build dh_installdirs usr/bin # Add here commands to install the package into debian/sipwise-cleanup-tools. - install -m 755 acc_backup.pl debian/sipwise-cleanup-tools/usr/bin/acc_backup.pl - install -m 755 binlog-purge.pl debian/sipwise-cleanup-tools/usr/bin/binlog-purge.pl + install -m 755 acc_backup.pl debian/sipwise-cleanup-tools/usr/sbin/acc_backup.pl + install -m 755 binlog-purge.pl debian/sipwise-cleanup-tools/usr/sbin/binlog-purge.pl + install -m 644 cleanup-tools.conf debian/sipwise-cleanup-tools/etc/cleanup-tools.conf + install -m 644 cleanup-tools.cron debian/sipwise-cleanup-tools/etc/cron.d/cleanup-tools # Build architecture-dependent files here. binary-arch: install