-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-rotate-db.pl
More file actions
executable file
·86 lines (69 loc) · 2.39 KB
/
backup-rotate-db.pl
File metadata and controls
executable file
·86 lines (69 loc) · 2.39 KB
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
#!/usr/bin/perl
#
# Dump and rotate MySQL database
#
# Copyright (C) 2015 Travis Foster <travees@ddv.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
use strict;
#
# Config
#
my $dump_dir = "/tmp";
my $db_host = "localhost";
my $db_port = "";
my $db_user = "";
my $db_pass = "";
my $db_name = "";
my $dump_file="${dump_dir}/${db_name}.dump";
my $dumps_to_keep=3;
my $gzip=1;
my $DEBUG = 0;
my $suffix=".gz" if $gzip;
#
# Main script
#
my ($mv_return, $oldfile, $move_to_num, $newfile, $gzip_return, $dump_return);
$dump_return = system("mysqldump -h $db_host -P $db_port --user=$db_user --password=$db_pass $db_name > ${dump_file}.tmp");
if ($dump_return == 0) {
for ($dumps_to_keep--; $dumps_to_keep >= 0; $dumps_to_keep--) {
my $oldfile = "${dump_file}.${dumps_to_keep}${suffix}";
if ( -e $oldfile ) {
print "$oldfile exists\n" if $DEBUG;
$move_to_num = $dumps_to_keep+1;
$newfile = "${dump_file}.${move_to_num}${suffix}";
$mv_return = system("mv $oldfile $newfile");
print "Could not move $oldfile -> $newfile\n" &&
exit 1 if ($mv_return != 0);
}
}
if ( -e $dump_file ) {
$mv_return = system("mv $dump_file ${dump_file}.1");
print "Could not move $dump_file to ${dump_file}.1\n" &&
exit 1 if ($mv_return != 0);
}
if ( -e "${dump_file}.tmp" ) {
$mv_return = system("mv ${dump_file}.tmp $dump_file");
print "Could not move ${dump_file}.tmp to ${dump_file}\n" &&
exit 1 if ($mv_return != 0);
}
$gzip_return = system("gzip ${dump_file}.1") if $gzip == 1;
print "I'm going to gzip ${dump_file}.1\n" if $DEBUG;
print "Could not gzip ${dump_file}.1: $!\n" if $gzip_return != 0;
} else {
print "Mysql dump failed: $!\n";
exit 1;
}
exit 0;