| 1 |
joko |
1.1 |
#!/usr/bin/perl |
| 2 |
|
|
|
| 3 |
|
|
=head1 NAME |
| 4 |
|
|
|
| 5 |
|
|
rsync-here.pl |
| 6 |
|
|
|
| 7 |
|
|
|
| 8 |
|
|
=head1 SYNOPSIS |
| 9 |
|
|
|
| 10 |
|
|
Wrapper around rsync program to provide an easy calling syntax for rsyncing against a predefined target. |
| 11 |
|
|
|
| 12 |
|
|
|
| 13 |
|
|
=head1 DESCRIPTION |
| 14 |
|
|
|
| 15 |
|
|
Before starting, your need to configure some specific variables in this file. Please see section CONFIGURATION. |
| 16 |
|
|
rsync-here.pl expects a single argument on the command line. Give it a full directory path and |
| 17 |
|
|
it will run the rsync program in the following style: (abstract) |
| 18 |
|
|
|
| 19 |
|
|
cd /$root; \ # go to one level on top of the specified source path |
| 20 |
|
|
$rsync -azuv -R -e ssh --progress $source $target |
| 21 |
|
|
|
| 22 |
|
|
|
| 23 |
|
|
=head1 CONFIGURATION |
| 24 |
|
|
|
| 25 |
|
|
Please edit "rsync-here.pl" to configure it, there is no ".ini"-style configuration file yet. |
| 26 |
|
|
|
| 27 |
|
|
=over 4 |
| 28 |
|
|
|
| 29 |
|
|
=item - |
| 30 |
|
|
$sh: /path/to/your/sh |
| 31 |
|
|
|
| 32 |
|
|
=item - |
| 33 |
|
|
$rsync: /path/to/your/rsync |
| 34 |
|
|
|
| 35 |
|
|
=item - |
| 36 |
|
|
$target_prefix: username@host.mydomain.org |
| 37 |
|
|
|
| 38 |
|
|
=item - |
| 39 |
|
|
$target_base: /path/to/your/backup-repository-base |
| 40 |
|
|
|
| 41 |
|
|
=back |
| 42 |
|
|
|
| 43 |
|
|
The hostname of your machine will be appended to "$target_base", on windows also the volumename. |
| 44 |
|
|
|
| 45 |
|
|
|
| 46 |
|
|
=head1 PREREQUISITES |
| 47 |
|
|
|
| 48 |
|
|
=over 4 |
| 49 |
|
|
|
| 50 |
|
|
=item * |
| 51 |
|
|
Perl |
| 52 |
|
|
|
| 53 |
|
|
=item * |
| 54 |
|
|
Rsync |
| 55 |
|
|
|
| 56 |
|
|
=item * |
| 57 |
|
|
SSH |
| 58 |
|
|
|
| 59 |
|
|
=back |
| 60 |
|
|
|
| 61 |
|
|
You also may find it convenient to setup ssh working with keys. (use ssh-keygen [-d]) |
| 62 |
|
|
|
| 63 |
|
|
|
| 64 |
|
|
=head1 COPYRIGHT |
| 65 |
|
|
|
| 66 |
|
|
This program is free software. You may copy or |
| 67 |
|
|
redistribute it under the same terms as Perl itself. |
| 68 |
|
|
|
| 69 |
|
|
|
| 70 |
|
|
=head1 TODO |
| 71 |
|
|
|
| 72 |
|
|
=over 4 |
| 73 |
|
|
|
| 74 |
|
|
=item |
| 75 |
|
|
+ windows compatibility |
| 76 |
|
|
|
| 77 |
|
|
=item |
| 78 |
|
|
+ waiting for keypress on exit: better handling |
| 79 |
|
|
|
| 80 |
|
|
=item |
| 81 |
|
|
+ write output to STDOUT while working! |
| 82 |
|
|
|
| 83 |
|
|
=item |
| 84 |
|
|
- ease ssh-key-setup for non-interactive use, include doc at min! |
| 85 |
|
|
|
| 86 |
|
|
=item |
| 87 |
|
|
- read "user - config" variables from %ENV |
| 88 |
|
|
|
| 89 |
|
|
=item |
| 90 |
|
|
- make it possible to sync single files |
| 91 |
|
|
|
| 92 |
|
|
=back |
| 93 |
|
|
|
| 94 |
|
|
=cut |
| 95 |
|
|
|
| 96 |
|
|
use strict; |
| 97 |
|
|
use warnings; |
| 98 |
|
|
use Sys::Hostname; |
| 99 |
|
|
|
| 100 |
|
|
# user - config |
| 101 |
|
|
my $sh = "sh"; |
| 102 |
|
|
my $rsync = "rsync"; |
| 103 |
|
|
my $target_prefix = 'joko@martha'; |
| 104 |
|
|
my $target_base = '~/backup'; |
| 105 |
|
|
my $bool_dry_run = 1; |
| 106 |
|
|
|
| 107 |
|
|
|
| 108 |
|
|
# used variables |
| 109 |
|
|
my $rootpath; |
| 110 |
|
|
my $rootpath_append; |
| 111 |
|
|
my $hostname; |
| 112 |
|
|
my $source; |
| 113 |
|
|
my $source_volume; |
| 114 |
|
|
my $target; |
| 115 |
|
|
|
| 116 |
|
|
# runtime - config |
| 117 |
|
|
BEGIN { sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } } |
| 118 |
|
|
|
| 119 |
|
|
$rootpath = "/"; |
| 120 |
|
|
if (RUNNING_IN_HELL) { |
| 121 |
|
|
$rootpath = "/cygdrive"; |
| 122 |
|
|
} |
| 123 |
|
|
|
| 124 |
|
|
|
| 125 |
|
|
# main - get and validate arguments |
| 126 |
|
|
$source = shift; |
| 127 |
|
|
if (!$source) { |
| 128 |
|
|
print "no source path given, exit.", "\n"; |
| 129 |
|
|
exit; |
| 130 |
|
|
} |
| 131 |
|
|
|
| 132 |
|
|
# main - modify source |
| 133 |
|
|
if (RUNNING_IN_HELL) { |
| 134 |
|
|
$source =~ s/^(\w)://i; |
| 135 |
|
|
$source_volume = $1; |
| 136 |
|
|
if (!$source_volume) { |
| 137 |
|
|
print "we are running on windows, please give full path including volume. (e.g. \"c:\\home\")"; |
| 138 |
|
|
exit; |
| 139 |
|
|
} |
| 140 |
|
|
# convert backslashes to slashes |
| 141 |
|
|
$source =~ s/\\/\//g; |
| 142 |
|
|
$source_volume = lc $source_volume; |
| 143 |
|
|
#$sourcePath = "/cygdrive/$sourcePath_volume$sourcePath"; |
| 144 |
|
|
$rootpath_append = "/$source_volume/"; |
| 145 |
|
|
} |
| 146 |
|
|
# strip leading and trailing slash, if any |
| 147 |
|
|
$source =~ s/^\///; |
| 148 |
|
|
$source =~ s/\/$//; |
| 149 |
|
|
|
| 150 |
|
|
|
| 151 |
|
|
# main - modify target |
| 152 |
|
|
$hostname = hostname; |
| 153 |
|
|
$hostname ||= "DUMMY"; |
| 154 |
|
|
$target = "$target_prefix:$target_base/$hostname/"; |
| 155 |
|
|
if (RUNNING_IN_HELL) { |
| 156 |
|
|
$target .= "$source_volume/"; |
| 157 |
|
|
} |
| 158 |
|
|
|
| 159 |
|
|
|
| 160 |
|
|
# main - build rsync command |
| 161 |
|
|
my $cmd; |
| 162 |
|
|
|
| 163 |
|
|
my $opt_dry_run = ""; |
| 164 |
|
|
$opt_dry_run = "--dry-run" if ($bool_dry_run); |
| 165 |
|
|
|
| 166 |
|
|
if ($rootpath_append) { $rootpath .= $rootpath_append; } |
| 167 |
|
|
|
| 168 |
|
|
$cmd = "$sh -c 'cd $rootpath; $rsync -azuv -e ssh -R --progress --times $opt_dry_run \"$source\" \"$target\"'"; |
| 169 |
|
|
print "synchronizing directories with...", "\n"; |
| 170 |
|
|
print "cmd: ", $cmd, "\n"; |
| 171 |
|
|
|
| 172 |
|
|
# main - run rsync |
| 173 |
|
|
open(STDERR,'>&STDOUT'); # redirect error to out |
| 174 |
|
|
$| = 1; # no buffering |
| 175 |
|
|
system($cmd); # execute system command |
| 176 |
|
|
|
| 177 |
|
|
# wait for pressing <ENTER> |
| 178 |
|
|
print "\n"; |
| 179 |
|
|
print "sleeping, press <ENTER> to continue..."; |
| 180 |
|
|
read STDIN, (my $content), 1; |
| 181 |
|
|
|