Welcome to mirror list, hosted at ThFree Co, Russian Federation.

apt-mirror - github.com/Stifler6996/apt-mirror.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5ea700f83c1bb55ba4de075b4b94d13874c87ab9 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#!/usr/bin/perl -w

=pod

=head1 NAME

apt-mirror - apt sources mirroring tool

=head1 SYNOPSIS

apt-mirror [configfile]

=head1 DESCRIPTION

A small and efficient tool that lets you mirror a part of or
the whole Debian GNU/Linux distribution or any other apt sources.

Main features:
 * It uses a config similar to apts F<sources.list>
 * It's fully pool comply
 * It supports multithreaded downloading
 * It supports multiple architectures at the same time
 * It can automatically remove unneeded files
 * It works well on overloaded channel to internet
 * It never produces an inconsistent mirror including while mirroring
 * It works on all POSIX complied systems with perl and wget

=head1 COMMENTS

apt-mirror uses F</etc/apt/mirror.list> as a configuration file.
By default it is tuned to official Debian or Ubuntu mirrors. Change
it for your needs.

After you setup the configuration file you may run as root:

    # su - apt-mirror -c apt-mirror

Or uncomment line in F</etc/cron.d/apt-mirror> to enable daily mirror updates.

=head1 FILES

F</etc/apt/mirror.list>
        Main configuration file

F</etc/cron.d/apt-mirror>
        Cron configuration template

F</var/spool/apt-mirror/mirror>
        Mirror places here

F</var/spool/apt-mirror/skel>
        Place for temporarily downloaded indexes

F</var/spool/apt-mirror/var>
        Log files placed here. URLs and MD5 summs also here.

=head1 CONFIGURATION EXAMPLES

The mirror.list configuration supports many options, the file is well commented explinging each option.
here are some sample mirror configuration lines showing the various supported ways :

Normal:
deb http://example.com/debian stable main contrib non-free

Arch Specific: ( many other arch's are supported )
deb-powerpc http://example.com/debian stable main contrib non-free

HTTP and FTP Auth or non-standard port:
deb http://user:pass@example.com:8080/debian stable main contrib non-free

Source Mirroring:
deb-src http://example.com/debian stable main contrib non-free

=head1 ORIGINAL AUTHOR

Dmitry N. Hramtsov E<lt>hdn@nsu.ruE<gt>

=head1 MAINTAINERS

Dmitry N. Hramtsov E<lt>hdn@nsu.ruE<gt>
Brandon Holtsclaw E<lt>brandon@imbrandon.comE<gt>

=cut

use strict;
use File::Copy;
use File::Path;
use File::Basename;

my $config_file;

my %config_variables = (
    "defaultarch" => `dpkg --print-installation-architecture 2>/dev/null` || 'i386',
    "nthreads"    => 20,
    "base_path"   => '/var/spool/apt-mirror',
    "mirror_path" => '$base_path/mirror',
    "skel_path"   => '$base_path/skel',
    "var_path"    => '$base_path/var',
    "cleanscript" => '$var_path/clean.sh',
    "_contents"   => 1,
    "_autoclean"  => 0,
    "_tilde"	  => 0,
    "limit_rate"  => '100m',
    "run_postmirror" => 1,
    "postmirror_script" => '$var_path/postmirror.sh'
);

my @config_binaries = ();
my @config_sources = ();

my @index_urls;
my @childrens = ();
my %skipclean = ();
my %clean_directory = ();


######################################################################################
## Setting up $config_file variable

$config_file = "/etc/apt/mirror.list";	# Default value
if($_ = shift) {
    die("apt-mirror: invalid config file specified") unless -f $_;
    $config_file = $_;
}

chomp $config_variables{"defaultarch"};

######################################################################################
## Common subroutines

sub round_number
{
	my $n = shift;
	my $minus = $n < 0 ? '-' : '';
	$n = abs($n);
	$n = int(($n + .05) * 10) / 10;
	$n .= '.0' unless $n =~ /\./;
	$n .= '0' if substr($n,(length($n) - 1),1) eq '.';
	chop $n if $n =~ /\.\d\d0$/;
	return "$minus$n";
}

sub format_bytes {
	my $bytes = shift;
	my $bytes_out = '0';
	my $size_name = 'bytes';
	my $KiB = 1024;
	my $MiB = 1024 * 1024;
	my $GiB = 1024 * 1024 * 1024;

	if ($bytes >= $KiB) {
		$bytes_out = $bytes / $KiB;
		$size_name = 'KiB';
		if ($bytes >= $MiB) {
			$bytes_out = $bytes / $MiB;
			$size_name = 'MiB';
			if ($bytes >= $GiB) {
				$bytes_out = $bytes / $GiB;
				$size_name = 'GiB';
			}
		}
	} else {
		$bytes_out = $bytes;
		$size_name = 'bytes';
	}

	$bytes_out = round_number($bytes_out);
	return "$bytes_out $size_name";
}

sub get_variable {
    my $value = $config_variables{shift @_};
    my $count = 16;
    while($value =~ s/\$(\w+)/$config_variables{$1}/xg) {
	die("apt-mirror: too many substitution while evaluating variable") if ($count --) < 0;
    }
    return $value;
}

sub lock_aptmirror {
	system ("touch " . get_variable("var_path") . "/apt-mirror.lock");
}

sub check_lock {
	if(-e get_varible("var_path") . /apt-mirror.lock)
	{
		die("apt-mirror is already running, exiting");
	}
}

sub unlock_aptmirror {
	system ("rm -f " . get_variable("var_path") . "/apt-mirror.lock");
}

sub download_urls {
    my $stage = shift;
    my @urls;
    my $i = 0;
    my $pid;
    my $nthreads = get_variable("nthreads");
    local $| = 1;

    @urls = @_;
    $nthreads = @urls if @urls < $nthreads;

    print "Downloading " . scalar(@urls) . " $stage files using $nthreads threads...\n";
    
    while(scalar @urls) {
	my @part = splice(@urls, 0, int(@urls / $nthreads));
	open URLS, ">" . get_variable("var_path") . "/$stage-urls.$i" or die("apt-mirror: can't write to intermediate file ($stage-urls.$i)");
	foreach (@part) { print URLS "$_\n"; }
	close URLS or die("apt-mirror: can't close intermediate file ($stage-urls.$i)");
	
	$pid = fork();
	
	die("apt-mirror: can't do fork in download_urls") if $pid < 0;
	
	if($pid == 0) {
#	    print " * Running wget $i...\n";
	    exec 'wget', '--limit-rate='.get_variable("limit_rate"), '-t', '0', '-r', '-N', '-l', 'inf', '-o', get_variable("var_path") . "/$stage-log.$i", '-i', get_variable("var_path") . "/$stage-urls.$i";
#	    print " * Can't exec $i...\n";
#	    unlink get_variable("var_path") . "/URLS.$i";
#	    unlink get_variable("var_path") . "/wget-log.$i";
#	    exit 1;
	}
	
	push @childrens, $pid;
	$i++; $nthreads--;
    }

    print "Begin time: " . localtime() . "\n[" . scalar(@childrens) . "]... ";
    while(scalar @childrens) {
	my $dead = wait();
	@childrens = grep { $_ != $dead } @childrens;
	print "[" . scalar(@childrens) . "]... ";
    }
    print "\nEnd time: " . localtime() . "\n\n";
}


######################################################################################
## Parse config

open CONFIG, "<$config_file" or die("apt-mirror: can't open config file ($config_file)");
while(<CONFIG>) {
    next if /^\s*#/;
    next unless /\S/;
    my @config_line = split;
    my $config_line = shift @config_line;

    if($config_line eq "set") {
	$config_variables{$config_line[0]} = $config_line[1];
	next;
    }
    
    if($config_line eq "deb") {
	push @config_binaries, [get_variable("defaultarch"), @config_line];
	next;
    }

    if($config_line =~ /deb-(alpha|amd64|armel|arm|hppa|hurd-i386|i386|ia64|lpia|m68k|mipsel|mips|powerpc|s390|sh|sparc)/) {
	push @config_binaries, [$1, @config_line];
	next;
    }

    if($config_line eq "deb-src") {
	push @config_sources, [@config_line];
	next;
    }

    if($config_line eq "skip-clean") {
	$config_line[0] =~ s[^(\w+)://][];
	$config_line[0] =~ s[/$][];
	$config_line[0] =~ s[~][%7E]g if get_variable("_tilde");
	$skipclean{$config_line[0]} = 1;
	next;
    }

    if($config_line eq "clean") {
	$config_line[0] =~ s[^(\w+)://][];
	$config_line[0] =~ s[/$][];
	$config_line[0] =~ s[~][%7E]g if get_variable("_tilde");
	$clean_directory{$config_line[0]} = 1;
	next;
    }
    
    die("apt-mirror: invalid line in config file ($.: $config_line ...)");
}
close CONFIG;

die("Please explicitly specify 'defaultarch' in mirror.list") unless get_variable("defaultarch");

check_lock();
lock_aptmirror();


######################################################################################
## Skel download

my %urls_to_download = ();
my ($url, $arch);

sub remove_double_slashes {
    local $_ = shift;
    while(s[/\./][/]g) {}
    while(s[(?<!:)//][/]g) {}
    while(s[(?<!:/)/[^/]+/\.\./][/]g) {}
    s/~/\%7E/g if get_variable("_tilde");
    return $_;
}

sub add_url_to_download {
    my $url = remove_double_slashes(shift);
    $urls_to_download{$url} = shift;
}

foreach (@config_sources) {
    my ($uri, $distribution, @components) = @{$_};

    if(@components) {
	$url = $uri . "/dists/" . $distribution . "/";

	add_url_to_download($url . "Release");
	add_url_to_download($url . "Release.gpg");
	foreach (@components) {
	    add_url_to_download($url . $_ . "/source/Release");
	    add_url_to_download($url . $_ . "/source/Sources.gz");
	}
    } else {
	add_url_to_download($uri . "/$distribution/Sources.gz");
    }
}

foreach (@config_binaries) {
    my ($arch, $uri, $distribution, @components) = @{$_};
    
    if(@components) {
	$url = $uri . "/dists/" . $distribution . "/";

	add_url_to_download($url . "Release");
	add_url_to_download($url . "Release.gpg");
	add_url_to_download($url . "Contents-" . $arch . ".gz") if get_variable("_contents");
	foreach (@components) {
	    add_url_to_download($url . $_ . "/binary-" . $arch . "/Release");
	    add_url_to_download($url . $_ . "/binary-" . $arch . "/Packages.gz");
	}
    } else {
	add_url_to_download($uri . "/$distribution/Packages.gz");
    }
}

chdir get_variable("skel_path") or die("apt-mirror: can't chdir to skel");
@index_urls = sort keys %urls_to_download;
download_urls("index", @index_urls);

foreach (keys %urls_to_download) {
    s[^(\w+)://][];
    s[~][%7E]g if get_variable("_tilde");
    $skipclean{$_} = 1;
    $skipclean{$_} = 1 if s[\.gz$][];
}

######################################################################################
## Main download prepair

%urls_to_download = ();

open FILES_ALL, ">" . get_variable("var_path") . "/ALL" or die("apt-mirror: can't write to intermediate file (ALL)");
open FILES_NEW, ">" . get_variable("var_path") . "/NEW" or die("apt-mirror: can't write to intermediate file (NEW)");
open FILES_MD5, ">" . get_variable("var_path") . "/MD5" or die("apt-mirror: can't write to intermediate file (MD5)");

my %stat_cache = ();

sub _stat {
  my ($filename) = shift;
  return @{$stat_cache{$filename}} if exists $stat_cache{$filename};
  my @res = stat($filename);
  $stat_cache{$filename} = \@res;
  return @res;
}

sub clear_stat_cache {
  %stat_cache = ();
}

sub need_update {
    my $filename = shift;
    my $size_on_server = shift;

    my (undef, undef, undef, undef, undef, undef, undef, $size) = _stat($filename);

    return 1 unless($size);
    return 0 if $size_on_server == $size;
    return 1;
}

sub remove_spaces($) {
    my $hashref = shift;
    foreach (keys %{$hashref}) {
	while(substr($hashref->{$_}, 0, 1) eq ' ') {
	    substr($hashref->{$_}, 0, 1) = '';
	}
    }
}

sub sanitise_uri {
    my $uri = shift;
    $uri =~ s[^(\w+)://][];
    $uri =~ s/^([^@]+)?@?// if $uri =~ /@/;
    $uri =~ s&:\d+/&/&; # and port information
    $uri =~ s/~/\%7E/g if get_variable("_tilde");
    return $uri;
}

sub proceed_index_gz {
    my $uri = shift;
    my $index = shift;
    my ($path, $package, $mirror, $files) = '';

    $path = sanitise_uri($uri);
    local $/ = "\n\n";
    $mirror = get_variable("mirror_path") . "/" . $path;
    
    if($index =~ s/\.gz$//) {
	system("gunzip < $path/$index.gz > $path/$index");
    }

    open STREAM, "<$path/$index" or die("apt-mirror: can't open index in proceed_index_gz");

    while($package = <STREAM>) {
	local $/ = "\n";
	chomp $package;
	my (undef, %lines) = split(/^([\w\-]+:)/m, $package);

	chomp(%lines);
	remove_spaces(\%lines);

	if(exists $lines{"Filename:"}) {	# Packages index
	    $skipclean{remove_double_slashes($path . "/" . $lines{"Filename:"})} = 1;
	    print FILES_ALL remove_double_slashes($path . "/" . $lines{"Filename:"}) . "\n";
	    print FILES_MD5 $lines{"MD5sum:"} . "  " . remove_double_slashes($path . "/" . $lines{"Filename:"}) . "\n";
	    if(need_update($mirror . "/" . $lines{"Filename:"}, $lines{"Size:"})) {
		print FILES_NEW remove_double_slashes($uri . "/" . $lines{"Filename:"}) . "\n";
		add_url_to_download($uri . "/" . $lines{"Filename:"}, $lines{"Size:"});
	    }
	} else {				# Sources index
	    foreach (split(/\n/, $lines{"Files:"})) {
		next if $_ eq '';
		my @file = split;
		die("apt-mirror: invalid Sources format") if @file != 3;
		$skipclean{remove_double_slashes($path . "/" . $lines{"Directory:"} . "/" . $file[2])} = 1;
		print FILES_ALL remove_double_slashes($path . "/" . $lines{"Directory:"} . "/" . $file[2]) . "\n";
		print FILES_MD5 $file[0] . "  " . remove_double_slashes($path . "/" . $lines{"Directory:"} . "/" . $file[2]) . "\n";
		if(need_update($mirror . "/" . $lines{"Directory:"} . "/" . $file[2], $file[1])) {
		    print FILES_NEW remove_double_slashes($uri . "/" . $lines{"Directory:"} . "/" . $file[2]) . "\n";
		    add_url_to_download($uri . "/" . $lines{"Directory:"} . "/" . $file[2], $file[1]);
		}
	    }
	}
    }

    close STREAM;
}

print "Proceed indexes: [";

foreach (@config_sources) {
    my ($uri, $distribution, @components) = @{$_};
    print "S";
    if(@components) {
	my $component;
	foreach $component (@components) {
	    proceed_index_gz($uri, "/dists/$distribution/$component/source/Sources.gz");
	}
    } else {
	proceed_index_gz($uri, "/$distribution/Sources.gz");
    }
}

foreach (@config_binaries) {
    my ($arch, $uri, $distribution, @components) = @{$_};
    print "P";
    if(@components) {
	my $component;
	foreach $component (@components) {
	    proceed_index_gz($uri, "/dists/$distribution/$component/binary-$arch/Packages.gz");
	}
    } else {
	proceed_index_gz($uri, "/$distribution/Packages.gz");
    }
}

clear_stat_cache();

print "]\n\n";

close FILES_ALL;
close FILES_NEW;
close FILES_MD5;


######################################################################################
## Main download

chdir get_variable("mirror_path") or die("apt-mirror: can't chdir to mirror");

my $need_bytes = 0;
foreach (values %urls_to_download) {
    $need_bytes += $_;
}

my $size_output = format_bytes($need_bytes);

print "$size_output will be downloaded into archive.\n";

download_urls("archive", sort keys %urls_to_download);


######################################################################################
## Copy skel to main archive

sub copy_file {
    my ($from, $to) = @_;
    my $dir = dirname($to);
    return unless -f $from;
    mkpath($dir) unless -d $dir;
    unless(copy($from, $to)) {
	warn("apt-mirror: can't copy $from to $to");
	return;
    }
    my ($atime,$mtime) = ( stat($from) ) [8, 9];
    utime($atime, $mtime, $to) or die("apt-mirror: can't utime $to");
}

foreach (@index_urls) {
    die("apt-mirror: invalid url in index_urls") unless s[^(\w+)://][];
    copy_file(get_variable("skel_path") . "/" . sanitise_uri("$_"), get_variable("mirror_path") . "/" . sanitise_uri("$_"));
    copy_file(get_variable("skel_path") . "/" . sanitise_uri("$_"), get_variable("mirror_path") . "/" . sanitise_uri("$_")) if(s/\.gz$//);
}


######################################################################################
## Make cleaning script

my (@rm_dirs, @rm_files) = ();
my $unnecessary_bytes = 0;

sub process_symlink {
    return 1;		# symlinks are always needed
}

sub process_file {
    my $file = shift;
    $file =~ s[~][%7E]g if get_variable("_tilde");
    return 1 if $skipclean{$file};
    push @rm_files, sanitise_uri($file);
    my (undef, undef, undef, undef, undef, undef, undef, $size, undef, undef, undef, undef, $blocks) = stat($file);
    $unnecessary_bytes += $blocks * 512;
    return 0;
}

sub process_directory {
    my $dir = shift;
    my $is_needed = 0;
    return 1 if $skipclean{$dir};
    opendir(DIR, $dir) or die "apt-mirror: can't opendir $dir: $!";
    foreach (grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR)) {
	my $item = $dir . "/". $_;
	$is_needed |= process_directory($item)	if -d $item && ! -l $item;
	$is_needed |= process_file($item)	if -f $item;
	$is_needed |= process_symlink($item)	if -l $item;
    }
    closedir DIR;
    push @rm_dirs, $dir unless $is_needed;
    return $is_needed;
}

chdir get_variable("mirror_path") or die("apt-mirror: can't chdir to mirror");

foreach (keys %clean_directory) {
    process_directory($_) if -d $_ && ! -l $_;
}

open CLEAN, ">" . get_variable("cleanscript") or die("apt-mirror: can't open clean script file");

my ($i, $total) = (0, scalar @rm_files);

if(get_variable("_autoclean")) {

    my $size_output = format_bytes($unnecessary_bytes);
    print "$size_output in $total files and " . scalar(@rm_dirs) . " directories will be freed...";

    chdir get_variable("mirror_path") or die("apt-mirror: can't chdir to mirror");

    foreach (@rm_files) { unlink $_; }
    foreach (@rm_dirs) { rmdir $_; }

} else {

    my $size_output = format_bytes($unnecessary_bytes);
    print "$size_output in $total files and " . scalar(@rm_dirs) . " directories can be freed.\n";
    print "Run " . get_variable("cleanscript") . " for this purpose.\n\n";

    print CLEAN "cd " . get_variable("mirror_path") . " || exit 1\n\n";
    print CLEAN "echo 'Removing $total unnecessary files [$unnecessary_bytes bytes]...'\n";
    foreach (@rm_files) {
	print CLEAN "rm -f '$_'\n";
	print CLEAN "echo -n '[" . int(100 * $i/$total) . "\%]'\n" unless $i % 500;
	print CLEAN "echo -n .\n" unless $i % 10;
        $i ++;
    }
    print CLEAN "echo 'done.'\n";
    print CLEAN "echo\n\n";
				
    $i = 0; $total = scalar @rm_dirs;
    print CLEAN "echo 'Removing $total unnecessary directories...'\n";
    foreach (@rm_dirs) {
	print CLEAN "rmdir '$_'\n";
        print CLEAN "echo -n '[" . int(100 * $i/$total) . "\%]'\n" unless $i % 50;
        print CLEAN "echo -n .\n";
        $i ++;
    }
    print CLEAN "echo 'done.'\n";
    print CLEAN "echo\n";
					
    close CLEAN;

}

if(get_variable("run_postmirror")) {
    print "Running the Post Mirror script ...\n";
    print "(" . get_variable("postmirror_script") . ")\n\n";
    if(-x get_variable("postmirror_script")){
        system (get_variable("postmirror_script"));        
    } else {
        system ('/bin/sh ' . get_variable("postmirror_script"));
    }
    print "\nPost Mirror script has completed. See above output for any possible errors.\n\n";
}

unlock_aptmirror();