-
Notifications
You must be signed in to change notification settings - Fork 3
/
sldbBackup.pl
executable file
·84 lines (63 loc) · 2.69 KB
/
sldbBackup.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
#!/usr/bin/perl -w
#
# This file implements automatic hot backup for SLDB database, it is part of
# SLDB.
#
# sldbBackup performs a hot backup of the SLDB database, stores the archive
# locally and sends it to a remote server using FTP. sldbBackup is supposed to
# be executed automatically through crontab for example, but it requires an
# account with sufficient privileges to run mysqlhotcopy.
#
# Copyright (C) 2013 Yann Riou <[email protected]>
#
# SLDB 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.
#
# SLDB 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 SLDB. If not, see <http://www.gnu.org/licenses/>.
#
# Version 0.3 (2020/04/29)
use strict;
use File::Path;
use Time::Piece;
################################################################################
# Configuration #
################################################################################
# name of the SLDB database
my $sldbName='sldb';
# absolute directory where backup archives will be stored locally
my $backupDir='/home/sldb/var';
# path of mysqlhotcopy utility
my $mysqlhotcopyBin='/usr/bin/mysqlhotcopy';
# LFTP bookmark to use when sending backup archives through FTP
my $lftpBookmark='sldbbackup';
# remote directory where backup archives will be stored
my $ftpDir='backup';
# bandwidth usage limit when uploading files through FTP (in bytes)
my $ftpSpeedLimit=512000;
################################################################################
my $verbose=0;
$verbose=1 if(@ARGV);
print "Creating temporary backup directory.\n" if($verbose);
my $tmpDir="$backupDir/backup_".(localtime->strftime('%Y%m%d_%H%M%S'));
mkpath($tmpDir);
print "Performing backup.\n" if($verbose);
system("$mysqlhotcopyBin -q --noindices $sldbName $tmpDir");
print "Archiving backup.\n" if($verbose);
system("tar c -C $tmpDir -f $tmpDir.tar $sldbName");
print "Removing temporary files.\n" if($verbose);
rmtree("$tmpDir");
print "Compressing backup.\n" if($verbose);
system("gzip $tmpDir.tar");
print "Sending backup by FTP.\n" if($verbose);
my $redir='';
$redir=' >/dev/null 2>&1' unless($verbose);
system("lftp $lftpBookmark -e \"set net:limit-total-rate $ftpSpeedLimit;cd $ftpDir; put $tmpDir.tar.gz;quit\"$redir");
print "Done.\n" if($verbose);