-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_build.pl
126 lines (102 loc) · 4 KB
/
check_build.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/perl
#
# Check if there is a new build.
# Setting 'Y' will backup to /home/sysop/backup
# And the number of backups you want to save
#
# Only for the Mojo branch
#
# You need to install the rsync package
# apt install rsync
#
# Include the following line in the crontab:
# 0 4 * * 1,2,3,4,5 run_cmd("check_build <Y/N> <num_backups>")
#
# If you want to keep the check_build.pl tool up to date tool,
# add the following to your dxspider crontab (Thanks for the idea Keith G6NHU):
# 30 0 * * * spawn('cd /spider/local_cmd; wget -q https://raw.githubusercontent.com/EA3CV/dxspider_info/main/check_build.pl -O /spider/local_cmd/check_build.pl')
# 32 0 * * * run_cmd('load/cmd')
#
# Kin EA3CV, ea3cv@cronux.net
#
# 20230322 v1.14
#
use DXDebug;
use strict;
use warnings;
my ($self, $line) = @_;
my @args = split /\s+/, $line;
my $bckup = $args[0];
my $max_copies = ($args[1] - 1) // 10; # Default 10 copies
return 1 unless $self->{priv} >= 9;
my @out;
my $res;
# Change the working directory to /spider
chdir "$main::root";
push @out, "Verifying ...";
system('git remote update');
my $local_repo = `git rev-parse \@`;
my $remote_repo = `git rev-parse \@{u}`;
if ($local_repo ne $remote_repo) {
$res = "There is a new build";
dbg('DXCron::spawn: $res') if isdbg('cron');
push @out, $res;
if ($bckup =~ /Y/i) {
$res = "Backup begins ...";
push @out, $res;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$year += 1900;
$mon++;
my $date = sprintf('%04d%02d%02d.%02d%02d%02d', $year, $mon, $mday, $hour, $min, $sec);
my $backup_dir = "../spider.backup";
unless (-d $backup_dir) {
mkdir $backup_dir;
}
my $load = "*$self->{mycall}* 💾 *Backup Starts*";
is_tg($load);
my @exclude = qw(
--exclude=local_data/debug
--exclude=local_data/log
--exclude=local_data/spots
--exclude=local_data/wwv
--exclude=local_data/wcy
);
system("rsync", "-avh", @exclude, '.', "../spider.backup/$date");
chdir "../spider.backup/";
# Delete oldest backups if the maximum limit is exceeded
my @backup_files = sort grep { /^spider\.\d{8}\.\d{6}\.tar\.gz$/ } glob("*");
if (scalar @backup_files > $max_copies) {
my $num_files_to_delete = (scalar @backup_files) - $max_copies;
for (1..$num_files_to_delete) {
my $backup_file = shift @backup_files;
unlink $backup_file;
}
}
system("tar", "-czvf", "spider.$date.tar.gz", "--remove-files", "$date/");
$res = "Backup completed.";
push @out, $res;
$load = "*$self->{mycall}* 🆗 *Backup Completed*";
is_tg($load);
}
chdir "$main::root";
# Reset and update the Git repository
system('git reset --hard origin/mojo') == 0 or die push @out,"Failed to reset Git repository: $!";
system('git pull') == 0 or die push @out,"Failed to pull updates from Git repository: $!";
DXCron::run_cmd('shut');
} elsif ($local_repo eq $remote_repo) {
$res = "There is no new build";
push @out, $res;
dbg('DXCron::spawn: $res') if isdbg('cron');
}
# Routine for sending a message via Telegram bot
# Needs to be enabled in Local.pm and DXVars.pm
sub is_tg
{
my $msg = shift;
if (defined &Local::telegram) {
my $r;
eval { $r = Local::telegram($msg); };
return if $r;
}
}
return (1, @out);