diff --git a/debian/README.Debian b/debian/README.Debian new file mode 100644 index 0000000000000000000000000000000000000000..d86419463c9ceb5a753745180745ca9c2779a76c --- /dev/null +++ b/debian/README.Debian @@ -0,0 +1,15 @@ +Debian package provides some additional options in config-file: + + +* file_descriptors = INTVALUE + + Init script will do 'ulimit -f INTVALUE' command before starting tarantool. + +* save_snapshots = COUNT + + Count of snapshots to save (default = 10). COUNT=0 disables removing + old snapshots. + + +There are script tarantool_snapshot_rotate (1) that is started every day +using cron.daily. diff --git a/debian/changelog b/debian/changelog index c690e21b8079b923c4c6a3667e0d0bd063807f31..ad1cb363103f28c13e38dbfbb4cd72cf9b6e755f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +tarantool (1.4.7+20120803-1) UNRELEASED; urgency=low + + * New upstream version. + + Add WAL rotate script. + + -- Dmitry E. Oboukhov <unera@debian.org> Fri, 03 Aug 2012 12:09:14 +0400 + tarantool (1.4.7+20120714-1) unstable; urgency=low * Fixed memcached space and some troubles in build system. diff --git a/debian/scripts/tarantool_snapshot_rotate b/debian/scripts/tarantool_snapshot_rotate new file mode 100644 index 0000000000000000000000000000000000000000..6b24336cc39e211226ea5bf69d6e7c784ad2ebd7 --- /dev/null +++ b/debian/scripts/tarantool_snapshot_rotate @@ -0,0 +1,220 @@ +#!/usr/bin/perl + +use warnings; +use strict; + +use constant CONFIG_DIR => '/var/lib/tarantool/started'; +use constant PID_DIR => '/var/run/tarantool'; +use constant SNAPSHOT_DIR => '/var/lib/tarantool/snapshot'; +use File::Spec::Functions 'catfile'; +use File::Basename 'basename', 'dirname'; +use IO::Socket::INET; +use Getopt::Long; +use Pod::Usage; +use Data::Dumper; + + +pod2usage(-exitstatus => 0, -verbose => 2) unless + GetOptions + 'help|h' => \my $help, + 'verbose|v' => \my $verbose, + 'snapshots|s=i' => \my $snapshots, +; +$snapshots //= 10; + +sub DEBUGF($;@) { + return unless $verbose; + my ($fmt, @arg) = @_; + $fmt =~ s/\s*$/\n/; + printf STDERR $fmt, @arg; +} + + +sub list_files($) { + my $sndir = shift; + unless (-d $sndir) { + DEBUGF 'Snapshot directory "%s" was not found', $sndir; + return; + } + + unless (-w $sndir) { + DEBUGF 'Can not write into directory %s', $sndir; + return; + } + + my $dh; + opendir $dh, $sndir; + my @files = sort + grep /\.(snap|xlog)$/, + grep { -r $_ and -f $_ } + map { catfile $sndir, $_ } + readdir $dh; + return @files; +} + +sub rotate_snapshot($) { + my ($pidfile) = @_; + + my $pid; + + + if (open my $ph, '<', $pidfile) { + $pid = <$ph>; + $pid =~ s/\D+//g; + } else { + warn "Can't open file $pidfile: $!\n"; + return; + } + + unless($pid) { + warn "PID was not received\n"; + return; + } + + my $sndir = catfile SNAPSHOT_DIR, basename $pidfile, '.pid'; + my @files = list_files $sndir; + goto FINISH unless @files; + + while(@files and $files[0] =~ /\.xlog$/) { + DEBUGF 'Remove orphaned %s', $files[0]; + unless (unlink $files[0]) { + DEBUGF "Can't unlink file %s: %s", $files[0], $!; + return; + } + shift @files; + } + + if (kill USR1 => $pid) { + goto FINISH unless @files; + for (my $i = 0; $i < 5; $i++) { + sleep 1; + my @inpr = sort glob catfile SNAPSHOT_DIR, '*.snap.inprogress'; + last unless @inpr; + if ($inpr[-1] and $inpr[-1] gt $files[-1]) { + DEBUGF "snapshot %s is still in progress...", $inpr[-1]; + next; + } + } + } else { + warn "Error while sending snapshot signal: $!"; + return; + } + + if ($snapshots) { + @files = list_files $sndir; + my $snaps = grep /\.snap$/, @files; + if ($snaps > $snapshots) { + my $to_remove = $snaps - $snapshots; + + while (@files) { + my $file = shift @files; + $to_remove-- if $file =~ /\.snap$/; + DEBUGF "Unlink file: %s...", $file; + unless (unlink $file) { + DEBUGF "Can't unlink file %s: %s", $file, $!; + return; + } + last unless $to_remove > 0; + } + while(@files and $files[0] =~ /\.xlog$/) { + DEBUGF 'Remove orphaned %s', $files[0]; + unless (unlink $files[0]) { + DEBUGF "Can't unlink file %s: %s", $files[0], $!; + return; + } + shift @files; + } + } + } + + FINISH: +} + + +DEBUGF "Looking through %s...", PID_DIR; +for (glob catfile PID_DIR, '*.pid') { + my $cfg = catfile CONFIG_DIR, basename $_, '.pid'; + + unless(-r $cfg) { + warn "Config file '$cfg' is not found\n"; + next; + } + DEBUGF 'Found instance "%s" (%s)', basename($cfg), basename $_; + + if (open my $fh, '<:encoding(UTF-8)', $cfg) { + my @lines = <$fh>; + + my ($user_snapshots) = + grep /^\s*save_snapshots\s*=\s*\d+\s*(?:#.*)?$/, + reverse @lines; + + if ($user_snapshots) { + for ($user_snapshots) { + s/#.*//; + s/\D+//g; + } + DEBUGF "Found user's option save_snapshots=%s, use it", + $user_snapshots; + $snapshots = $user_snapshots; + } else { + DEBUGF "Use default value: save_snapshots=%s", $snapshots; + } + + rotate_snapshot $_; + + + } else { + warn "Can't open file $cfg: $!\n"; + next; + } +} + +exit 0 unless -x PID_DIR; +exit 0 unless -x CONFIG_DIR; + +=head1 NAME + +tarantool_snapshot_rotate - script to creates/rotates snapshots + +=head1 SYNOPSIS + + tarantool_snapshot_rotate + tarantool_snapshot_rotate --verbose + +=head1 DESCRIPTION + +The script passes through all started tarantool instances and creates +snapshots for each instance. + +The script understands some additional options in tarantool.cfg: + +=over + +=item save_snapshots = COUNT + +Count of snapshots to save (default = 10). COUNT=0 disables removing +old snapshots. + +=item + +=back + +=head1 OPTIONS + +=over + +=item -h | --help + +show the helpscreen + +=item -v | --verbose + +log process to B<STDOUT> + +=item -s | --snapshots COUNT + +redefines B<save_snapshots> option of config file + +=back + +=cut diff --git a/debian/tarantool-common.cron.daily b/debian/tarantool-common.cron.daily new file mode 100644 index 0000000000000000000000000000000000000000..8c57a6b0dc55735d93b52c7851c4144163b73fe8 --- /dev/null +++ b/debian/tarantool-common.cron.daily @@ -0,0 +1,6 @@ +#!/bin/sh + +test -x /usr/bin/tarantool_box || exit 0 +test -x /usr/sbin/tarantool_snapshot_rotate || exit 0 + +/usr/sbin/tarantool_snapshot_rotate diff --git a/debian/tarantool-common.install b/debian/tarantool-common.install index 710b54af12438e0b370b8737d87cfc87d5d5d9a0..affb73c50f62b85a49a3c98afeb1482e936d4600 100644 --- a/debian/tarantool-common.install +++ b/debian/tarantool-common.install @@ -1,3 +1,4 @@ debian/etc/example.cfg /etc/tarantool/instances.available/ -debian/scripts/tarantool_instance /usr/sbin +debian/scripts/tarantool_instance /usr/sbin/ +debian/scripts/tarantool_snapshot_rotate /usr/sbin/ debian/scripts/tarantool_logrotate /usr/lib/tarantool/