| 1 | # ----------------------------------------------------------------------------- | ||||||||||
| 2 | |||||||||||
| 3 | =encoding utf8 | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Quiq::Rsync - Aufruf von rsync von Perl aus | ||||||||||
| 8 | |||||||||||
| 9 | =head1 BASE CLASS | ||||||||||
| 10 | |||||||||||
| 11 | L<Quiq::Object> | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | use Quiq::Rsync; | ||||||||||
| 16 | |||||||||||
| 17 | # /src/dir => /dest/dir (gleicher Verzeichnisname) | ||||||||||
| 18 | |||||||||||
| 19 | Quiq::Rsync->exec('/src/dir','/dest'); | ||||||||||
| 20 | Quiq::Rsync->exec('/src/dir/','/dest/dir'); | ||||||||||
| 21 | |||||||||||
| 22 | # /src/dir1 => /dest/dir2 (unterschiedlicher Verzeichnisname) | ||||||||||
| 23 | Quiq::Rsync->exec('/src/dir1/','/dest/dir2'); | ||||||||||
| 24 | |||||||||||
| 25 | # Änderungen anzeigen, aber nicht durchführen | ||||||||||
| 26 | Quiq::Rsync->exec($src,$dest,-dryRun=>1); | ||||||||||
| 27 | |||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||
| 29 | |||||||||||
| 30 | Führe rsync(1) unter Kontrolle von Perl aus. Die Klasse stützt sich | ||||||||||
| 31 | auf die Klasse File::Rsync ab und stellt gegenüber dieser eine | ||||||||||
| 32 | spezialisierte, einfachere Schnittstelle zur Verfügung. | ||||||||||
| 33 | Eigenschaften: | ||||||||||
| 34 | |||||||||||
| 35 | =over 2 | ||||||||||
| 36 | |||||||||||
| 37 | =item * | ||||||||||
| 38 | |||||||||||
| 39 | der Aufruf von exec() ohne Optionen entspricht der | ||||||||||
| 40 | "Standard-Nutzung" von rsync | ||||||||||
| 41 | |||||||||||
| 42 | =item * | ||||||||||
| 43 | |||||||||||
| 44 | im Fehlerfall wird eine Exception geworfen | ||||||||||
| 45 | |||||||||||
| 46 | =item * | ||||||||||
| 47 | |||||||||||
| 48 | eine Ausgabe findet nur im Fehlerfall und bei Änderungen statt, | ||||||||||
| 49 | die statistische Ausgabe von rsync unterbleibt | ||||||||||
| 50 | |||||||||||
| 51 | =back | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | # ----------------------------------------------------------------------------- | ||||||||||
| 56 | |||||||||||
| 57 | package Quiq::Rsync; | ||||||||||
| 58 | 1 | ▶ | use base qw/Quiq::Object/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 59 | |||||||||||
| 60 | 1 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 62 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 63 | |||||||||||
| 64 | 1 | our $VERSION = '1.240'; | |||||||||
| 65 | |||||||||||
| 66 | 1 | ▶ | use Quiq::Option; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 1 | ▶ | use Quiq::Path; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 68 | 1 | ▶ | use File::Rsync (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 69 | |||||||||||
| 70 | # ----------------------------------------------------------------------------- | ||||||||||
| 71 | |||||||||||
| 72 | =head1 METHODS | ||||||||||
| 73 | |||||||||||
| 74 | =head2 Klassenmethoden | ||||||||||
| 75 | |||||||||||
| 76 | =head3 exec() - Führe rsync-Kommando aus | ||||||||||
| 77 | |||||||||||
| 78 | =head4 Synopsis | ||||||||||
| 79 | |||||||||||
| 80 | $output = $class->exec($src,$dest,@opt); | ||||||||||
| 81 | ($output,$cmd) = $class->exec($src,$dest,@opt); | ||||||||||
| 82 | |||||||||||
| 83 | =head4 Arguments | ||||||||||
| 84 | |||||||||||
| 85 | =over 4 | ||||||||||
| 86 | |||||||||||
| 87 | =item $src | ||||||||||
| 88 | |||||||||||
| 89 | Quell-Pfad | ||||||||||
| 90 | |||||||||||
| 91 | =item $dest | ||||||||||
| 92 | |||||||||||
| 93 | Ziel-Pfad | ||||||||||
| 94 | |||||||||||
| 95 | =back | ||||||||||
| 96 | |||||||||||
| 97 | =head4 Options | ||||||||||
| 98 | |||||||||||
| 99 | =over 4 | ||||||||||
| 100 | |||||||||||
| 101 | =item -dryRun => $bool (Default: 0) | ||||||||||
| 102 | |||||||||||
| 103 | Füge die Option --dry-run zur Kommandozeile hinzu, d.h. das | ||||||||||
| 104 | rsync-Kommando wird ausgeführt, ohne dass Änderungen vorgenommen | ||||||||||
| 105 | werden. | ||||||||||
| 106 | |||||||||||
| 107 | =item -print => $bool (Default: 1) | ||||||||||
| 108 | |||||||||||
| 109 | Liefere die Ausgabe des rsync-Kommandos nicht nur zurück, sondern | ||||||||||
| 110 | gib sie auch auf STDOUT aus. | ||||||||||
| 111 | |||||||||||
| 112 | =back | ||||||||||
| 113 | |||||||||||
| 114 | =head4 Returns | ||||||||||
| 115 | |||||||||||
| 116 | Ausgabe des rsync-Kommandos, Beschreibung siehe oben (String). Im | ||||||||||
| 117 | List-Kontext liefere zusätzlich das ausgeführte rsync-Kommando | ||||||||||
| 118 | (String, String). | ||||||||||
| 119 | |||||||||||
| 120 | =head4 Description | ||||||||||
| 121 | |||||||||||
| 122 | Führe rsync(1) für Quellpfad $src und Zielpfad $dest aus. | ||||||||||
| 123 | Ohne Angabe von Optionen wird als Kommandozeile ausgeführt: | ||||||||||
| 124 | |||||||||||
| 125 | rsync --archive --verbose --delete SRC DEST | ||||||||||
| 126 | |||||||||||
| 127 | D.h. $src und $dest werden als Verzeichnisse angesehen, wobei | ||||||||||
| 128 | Verzeichnis $dest auf exakt den gleichen Stand wie $src gebracht wird. | ||||||||||
| 129 | |||||||||||
| 130 | Schlägt das Kommando fehl, wird eine Exception geworfen. | ||||||||||
| 131 | |||||||||||
| 132 | Die Ausgabe des rsync-Kommandos wird zurück geliefert, | ||||||||||
| 133 | wobei einige Zeilen entfernt werden, so dass eine Ausgabe nur | ||||||||||
| 134 | dann erscheint, wenn Änderungen durchgeführt wurden, d.h. die | ||||||||||
| 135 | Zeilen über und unter PROTOKOLL werden entfernt: | ||||||||||
| 136 | |||||||||||
| 137 | sending incremental file list | ||||||||||
| 138 | PROTOKOLL | ||||||||||
| 139 | sent X bytes received X bytes X.00 bytes/sec | ||||||||||
| 140 | total size is X speedup is X.X | ||||||||||
| 141 | |||||||||||
| 142 | Im Dry-Run-Modus wird am Ende (DRY RUN) angezeigt. | ||||||||||
| 143 | |||||||||||
| 144 | =cut | ||||||||||
| 145 | |||||||||||
| 146 | # ----------------------------------------------------------------------------- | ||||||||||
| 147 | |||||||||||
| 148 | 1 | ▶ | sub exec { | ||||||||
|
Subroutine
exec: called CC=11 SCAR=45.7
Pod
documented
| |||||||||||
| 149 | 1 | my ($class,$src,$dest) = splice @_,0,3; | |||||||||
| 150 | # @opt | ||||||||||
| 151 | |||||||||||
| 152 | # Optionen | ||||||||||
| 153 | |||||||||||
| 154 | 1 | my $dryRun = 0; | |||||||||
| 155 | 1 | my $print = 1; | |||||||||
| 156 | |||||||||||
| 157 | 1 | Quiq::Option->extract(\@_, | |||||||||
| 158 | -dryRun => \$dryRun, | ||||||||||
| 159 | -print => \$print, | ||||||||||
| 160 | ); | ||||||||||
| 161 | |||||||||||
| 162 | 1 | my $rsy = File::Rsync->new({ | |||||||||
| 163 | 'path-to-rsync' => Quiq::Path->findProgram('rsync'), | ||||||||||
| 164 | archive => 1, | ||||||||||
| 165 | verbose => 1, | ||||||||||
| 166 | delete => 1, | ||||||||||
| 167 | dry_run => $dryRun, | ||||||||||
| 168 | src => $src, | ||||||||||
| 169 | dest => $dest, | ||||||||||
| 170 | }); | ||||||||||
| 171 | |||||||||||
| 172 | 0 | my $output = ''; | |||||||||
| 173 | 0 | ▶ | if (!$rsy->exec) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 0 | my $errA = $rsy->err; | |||||||||
| 175 | 0 | ▶ | $class->throw( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 'RSYNC-00001: Command failed', | ||||||||||
| 177 | # Cmd => $rsy->cmd, | ||||||||||
| 178 | Stderr => $errA? join('',@$errA): undef, | ||||||||||
| 179 | ); | ||||||||||
| 180 | } | ||||||||||
| 181 | else { | ||||||||||
| 182 | # Liefere die Ausgabe, die das rsync-Kommando nach stdout | ||||||||||
| 183 | # geschrieben hat, wobei die Zeilen über und unter PROTOKOLL | ||||||||||
| 184 | # entfernt werden: | ||||||||||
| 185 | # | ||||||||||
| 186 | # sending incremental file list | ||||||||||
| 187 | # PROTOKOLL | ||||||||||
| 188 | # | ||||||||||
| 189 | # sent X bytes received X bytes X.00 bytes/sec | ||||||||||
| 190 | # total size is X speedup is X.X | ||||||||||
| 191 | # | ||||||||||
| 192 | # Im Dry-Run-Modus wird am Ende (DRY RUN) angezeigt. | ||||||||||
| 193 | |||||||||||
| 194 | 0 | my @arr = $rsy->out; | |||||||||
| 195 | 0 | ▶ | if ($arr[0] =~ /^sending/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 196 | 0 | shift @arr; | |||||||||
| 197 | } | ||||||||||
| 198 | 0 | ▶ | if ($arr[-3] =~ /^$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 199 | 0 | splice @arr,-3,1; | |||||||||
| 200 | } | ||||||||||
| 201 | 0 | ▶ | if ($arr[-2] =~ /^sent/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | 0 | splice @arr,-2,1; | |||||||||
| 203 | } | ||||||||||
| 204 | 0 | ▶ | if ($arr[-1] =~ /^total/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | 0 | ▶ | if ($arr[-1] =~ /DRY RUN/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 206 | 0 | $arr[-1] = "(DRY RUN)\n"; | |||||||||
| 207 | } | ||||||||||
| 208 | else { | ||||||||||
| 209 | 0 | pop @arr; | |||||||||
| 210 | } | ||||||||||
| 211 | } | ||||||||||
| 212 | 0 | ▶ | if (@arr) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 213 | 0 | $output .= "==$dest==\n"; | |||||||||
| 214 | } | ||||||||||
| 215 | 0 | $output .= join '',@arr; | |||||||||
| 216 | } | ||||||||||
| 217 | |||||||||||
| 218 | 0 | ▶ | if ($print) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 219 | 0 | print $output; | |||||||||
| 220 | } | ||||||||||
| 221 | |||||||||||
| 222 | 0 | ▶ | return wantarray? ($output,scalar $rsy->lastcmd): $output; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 223 | } | ||||||||||
| 224 | |||||||||||
| 225 | # ----------------------------------------------------------------------------- | ||||||||||
| 226 | |||||||||||
| 227 | =head1 VERSION | ||||||||||
| 228 | |||||||||||
| 229 | 1.240 | ||||||||||
| 230 | |||||||||||
| 231 | =head1 AUTHOR | ||||||||||
| 232 | |||||||||||
| 233 | Frank Seitz, L<http://fseitz.de/> | ||||||||||
| 234 | |||||||||||
| 235 | =head1 COPYRIGHT | ||||||||||
| 236 | |||||||||||
| 237 | Copyright (C) 2026 Frank Seitz | ||||||||||
| 238 | |||||||||||
| 239 | =head1 LICENSE | ||||||||||
| 240 | |||||||||||
| 241 | This code is free software; you can redistribute it and/or modify | ||||||||||
| 242 | it under the same terms as Perl itself. | ||||||||||
| 243 | |||||||||||
| 244 | =cut | ||||||||||
| 245 | |||||||||||
| 246 | # ----------------------------------------------------------------------------- | ||||||||||
| 247 | |||||||||||
| 248 | 1 | 1; | |||||||||
| 249 | |||||||||||
| 250 | # eof | ||||||||||