-
Notifications
You must be signed in to change notification settings - Fork 5
/
fetch-comm
executable file
·105 lines (85 loc) · 2.67 KB
/
fetch-comm
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
#!/usr/bin/perl
use strict;
use warnings;
sub reverseMode($);
sub run(@);
sub runOrDie(@);
my $MODE_N9E = "n9e";
my $MODE_SCBR = "scbr";
my $MODE_DEFAULT = "default";
my $MODE_REVERSE = "reverse";
my $defaultSmsMode = $MODE_N9E;
my $defaultCallMode = $MODE_SCBR;
my $reverseSmsMode = reverseMode $defaultSmsMode;
my $reverseCallMode = reverseMode $defaultCallMode;
my $usage = "Usage:
$0 sms [--mode=MODE]
backup SMS
$0 call [--mode=MODE]
backup calls
MODE:
$MODE_N9E: use mode=$MODE_N9E (n9export)
$MODE_SCBR: use mode=$MODE_SCBR (smsbackuprestore/callbackuprestore)
$MODE_DEFAULT: use mode=$defaultSmsMode for 'sms' and mode=$defaultCallMode for 'call'
$MODE_REVERSE: use mode=$reverseSmsMode for 'sms' and mode=$reverseCallMode for 'call'
";
sub main(@){
my $type;
if(@_ > 0 and $_[0] =~ /^(sms|call)$/){
$type = shift;
}
my $mode = $MODE_DEFAULT;
if(@_ > 0 and $_[0] =~ /^--mode=($MODE_N9E|$MODE_SCBR|$MODE_DEFAULT|$MODE_REVERSE)$/){
$mode = $1;
shift;
}
die $usage if not defined $type or @_ > 0;
$mode = $defaultSmsMode if $mode eq $MODE_DEFAULT and $type eq "sms";
$mode = $defaultCallMode if $mode eq $MODE_DEFAULT and $type eq "call";
$mode = $reverseSmsMode if $mode eq $MODE_REVERSE and $type eq "sms";
$mode = $reverseCallMode if $mode eq $MODE_REVERSE and $type eq "call";
my $dir = "$ENV{HOME}/Code/n9";
my $dest = "$dir/backup";
my $parseTool = "comm-tool.pl $type";
my $host = `n9`;
chomp $host;
my $srcDir = "/home/user/MyDocs/backup-$type";
my $destDir = "$dest/backup-$type";
my $remoteRepoDir = "$srcDir-repo";
if($mode eq $MODE_N9E){
runOrDie "n9", "-b", "backup-comm-n9e $type";
}elsif($mode eq $MODE_SCBR){
my $scbrExec = "${type}backuprestore";
run "n9", "-s", "killall -9 $scbrExec";
run "n9", "-s", "/opt/dropcache-mdn/bin/dropcache.sh --3";
runOrDie "n9", "-b", "backup-comm-scbr $type --retry 20";
}
runOrDie "mkdir", "-p", $destDir;
runOrDie "rsync", "-avP", "root\@$host:$srcDir/", $destDir;
runOrDie "perl $dir/$parseTool split";
runOrDie "perl $dir/$parseTool commit";
runOrDie "perl $dir/$parseTool join-all";
run "scp", "$destDir/all.$type", "root\@$host:$srcDir/";
run "n9", "-s", "chown user.users -R $srcDir/all.$type";
run "rsync", "-a", "$destDir/repo/", "root\@$host:$remoteRepoDir/";
run "n9", "-s", "chown user.users -R $remoteRepoDir";
}
sub reverseMode($){
my $mode = shift;
if($mode eq $MODE_N9E){
return $MODE_SCBR;
}elsif($mode eq $MODE_SCBR){
return $MODE_N9E;
}else{
die "invalid mode: $mode\n";
}
}
sub run(@){
print "@_\n";
system @_;
}
sub runOrDie(@){
run @_;
die "failed" if $? != 0;
}
&main(@ARGV);