-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync-dir
executable file
·72 lines (58 loc) · 1.4 KB
/
sync-dir
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
#!/usr/bin/perl
use strict;
use warnings;
sub syncDir($$);
sub run(@);
my $backupDir = "$ENV{HOME}/Code/n9/backup";
my $backups = {
logs => ["/home/user/.cache/logs", [], []],
emumaster => ["/home/user/.emumaster", ["--del"], []],
swype => ["/home/user/.swype", ["--del"], []],
fbreader => ["/home/user/.FBReader", ["--del"], [qw(
cache
)]],
klomp => ["/home/user/.klomp", ["--del", "--no-perms"], [qw(
db
datecache
lib
)]],
};
my @rsyncOpts = qw(
-avP --no-owner --no-group
);
my $usage = "$0 NAME backup|restore\n";
sub main(@){
die $usage if @_ != 2
or not defined $$backups{$_[0]}
or $_[1] !~ /^(backup|restore)$/;
my ($backup, $cmd) = @_;
syncDir $backup, $cmd;
}
sub syncDir($$){
my ($backup, $cmd) = @_;
my $host = `n9`;
chomp $host;
my $now = time;
my $localDir = "$backupDir/$backup";
my ($remoteDir, $extraOpts, $excludes) = @{$$backups{$backup}};
my @rsyncCmd = ("rsync", @rsyncOpts, @$extraOpts);
for my $exclude(@$excludes){
push @rsyncCmd, "--exclude=$exclude";
}
if($cmd eq 'backup'){
run @rsyncCmd, "user\@$host:$remoteDir/", $localDir;
}elsif($cmd eq 'restore'){
run "n9", "
if [ -e $remoteDir ]; then
cp -ar $remoteDir $remoteDir-backup-$now
fi
";
run @rsyncCmd, "$localDir/", "user\@$host:$remoteDir/";
}
}
sub run(@){
print "@_\n";
system @_;
die "failed" if $? != 0;
}
&main(@ARGV);