~vcs-imports/personalbackup/trunk

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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
#!/usr/bin/perl -w

# $Id: personalbackupc 177 2007-02-01 23:25:12Z linuxtuxie $

#############################################################################
# PersonalBackup -  A company-wide solution for backing up all your         #
#                   Windows machines and Samba shares                       #
#                                                                           #
# Copyright (C) 2004 Kim Kuylen / Melexis                                   #
#                                                                           #
# This program is free software; you can redistribute it and/or             #
# modify it under the terms of the GNU General Public License               #
# as published by the Free Software Foundation.                             #
#############################################################################

use lib "__libinstalldir__";
use strict;
use DBI;
use PBUtils;
use PBConfig;
use PBDatabase;
use PBMessage;

use HTML::Template;
use POSIX qw(strftime);
use Filesys::SmbClient;
use File::Find;
use File::Path;
use Net::Ping;
use IO::Socket qw(inet_aton);
use MLDBM qw(DB_File Storable);
use Fcntl;

##########################################
# configuration
##########################################
my $conf            = new PBConfig("__confdir__/personalbackup.conf");
my $installdir      = $conf->get("installdir");
my $templatedir     = $conf->get("templatedir");
my $maxclients      = $conf->get("maxclients");
my $clienttimeout   = $conf->get("clienttimeout");
my $tempdir         = $conf->get("tempdir");
my $pooldir         = $conf->get("pooldir");
my $webserver       = $conf->get("webserver");
my $workgroup       = $conf->get("workgroup");
my $messagedelay    = $conf->get("messagedelay");
my $maxretry        = $conf->get("maxretry");
my $excluderef      = $conf->get("excludefiles");
my $excludecvs      = $conf->get("excludecvs");
my $smbclient       = $conf->get("smbclient");
my $vpnrange        = $conf->get("vpnrange");
my $mkdir           = $conf->get("mkdir");
my $cp              = $conf->get("cp");
my $backupthreshold = $conf->get("backupthreshold");
my $pinginterval    = $conf->get("pinginterval");
my $DEBUG           = $conf->get("DEBUG");
my @excludefiles    = @$excluderef;
my $language        = "en";

# Getting machine id, name, ip address and ppid from personalbackupd
my $machine_id   = $ARGV[0];
my $machine_name = $ARGV[1];
my $machine_ip   = $ARGV[2];
my $ppid         = $ARGV[3];

my $pid = $$;
my $weeknumber = int( strftime( "%W", localtime ) );
( my $day, my $year, my $dow ) = (localtime)[ 3, 5, 6 ];

my (@shares,           $smb,               $local_logon,
    %remote_file_list, $backup_size,       $skip_size,
    $limit_size,       $backup_file_count, $skip_count,
    $old_backup_id,    $backup_id,         $maxbackupsize,
    $maxvpnsize,       $backup_location,   $current_backup_exists
);

##########################################
### START of main program
##########################################

if (not(   defined($machine_id)
        && defined($machine_name)
        && defined($machine_ip)
        && defined($ppid) )
    )
{
    print
        "It is not the intention to run personalbackupc directly from the command line.\n";
    exit(1);
}

# Catch SIGTERM signal
$SIG{TERM} = \&quit_client;
$SIG{ALRM} = \&alarm_client;

# If alarm overflows, something has gone wrong (like pc shutted down during backup), if this happens we commit suicide :)
alarm($clienttimeout);

# Create database connection
my $dbh = new PBDatabase();

# Reset all shares by setting all skipped shares for the specified machine to scheduled
my $query =
      "UPDATE shares"
    . "   SET backed_up = ?"
    . "     , progress = 0"
    . " WHERE machine_id = ?"
    . "   AND backed_up IN (?, ?, ?)";
$dbh->exec_dml( $query, $STATUS{"scheduled"}, $machine_id, $STATUS{"skipped"},
    $STATUS{"oneskipped"}, $STATUS{"copying"} );

# Detect if the machine is connected through a vpn connection.
my $vpn_enabled = &detectvpn($machine_ip);

# Get the list of shares for the specified machine which are enabled and scheduled for today
$query =
      "SELECT s.id"
    . "     , u.username"
    . "     , m.type"
    . "     , s.share_name"
    . "     , s.username"
    . "     , s.password"
    . "     , s.maxbackupsize"
    . "     , s.vpn"
    . "     , s.maxvpnsize"
    . "     , u.language"
    . "  FROM shares s"
    . "     , machines m"
    . "     , users u"
    . " WHERE s.machine_id = m.id"
    . "   AND m.id = '$machine_id'"
    . "   AND s.backed_up = '"
    . $STATUS{"scheduled"} . "'"
    . "   AND u.id=m.user_id"
    . "   AND (s.schedule & (2^$dow)::integer) > 0"
    . "   AND s.enabled='Y'";

my $result = $dbh->exec_select($query);
foreach my $data (@$result) {
    my ($share_id,   $user,     $type,          $share_name,
        $username,   $password, $maxbackupsize, $vpn,
        $maxvpnsize, $language
    ) = @$data;
    my %share = (
        share_id      => $share_id,
        user          => $user,
        type          => $type,
        share_name    => $share_name,
        username      => $username,
        password      => $password,
        vpn           => $vpn,
        maxvpnsize    => $maxvpnsize,
        maxbackupsize => $maxbackupsize
    );
    push @shares, \%share
        if ( $vpn_enabled eq 'N' ) || ( $vpn_enabled eq 'Y' && $vpn eq "Y" );
}

# Sending notification mail if the user wants it :)
if (@shares) { &sendnotification(); }
else {
    PBUtils->logclient( $machine_name, $pid, "info",
        "Nothing to backup for machine '$machine_name'...maybe all shares are not scheduled or disabled?"
    );
}

foreach my $share (@shares) {
    PBUtils->logclient( $machine_name, $pid, "info",
        "Processing share: '" . $share->{share_name} . "'" );
    PBUtils->logclient( $machine_name, $pid, "info",
        "PC operating system type: " . $share->{type} );
    PBUtils->logclient( $machine_name, $pid, "info",
              "Share '"
            . $share->{share_name}
            . "' scheduled for day of week "
            . $dow );
    my @folders;
    my $query =
          "SELECT f.dirname"
        . "  FROM folders f"
        . " WHERE f.share_id = '"
        . $share->{share_id} . "'";
    my $result = $dbh->exec_select($query);
    foreach my $data (@$result) {
        my ($folder) = @$data;
        push( @folders, $folder );
    }

    # If smbclient is successful
    ( my $smbresult ) = &smbclient(
        $share->{share_id}, $share->{share_name},
        $share->{username}, $share->{password}
    );
    PBUtils->logclient( $machine_name, $pid, "info",
        "Attempt to connect with SmbClient: $smbresult" );
    if ( $smbresult eq "OK" ) {
        my $start_conn = time;
        $maxbackupsize = 0;
        $maxvpnsize    = 0;
        PBUtils->logclient( $machine_name, $pid, "info",
            "SmbClient started at $start_conn" );
        &perform_backup(
            $machine_id,          $share->{share_id},
            $share->{share_name}, $share->{username},
            $share->{password},   \@folders,
            $share->{user},       $share->{maxbackupsize},
            $share->{maxvpnsize}
        );

        # Backup has been performed
        &smbclient_close( $share->{share_id}, $start_conn );
    }
    else {    # If smbclient failed
        &backup_failed( $share->{share_id},
            "Smbclient connection failed. Skipping backup of share '$share->{share_name}'"
        );
    }
    &skipshare( $share->{share_id} );
}

if (@shares) {

# Make sure that we do not get kicked out during the handling of this piece of code
    alarm($clienttimeout);

    # Set the final status of the machine
    my $result = &setmachinestatus();

# Only send a mail now if all shares are backed up successfully and if the users wants to receive it
    if ($result) { &sendresult(); }
}

##########################################
### END of main program
##########################################

##########################################
### START of subroutines
##########################################

sub process_tree {
    my ( $folder, $share_id ) = @_;
    my @folder_todo_list = ();
    if ( my $fd = $smb->opendir("smb://$machine_ip/$folder") ) {
        while ( my $f = $smb->readdir_struct($fd) ) {
            if (   ( $f->[0] == SMBC_DIR )
                && ( $f->[1] ne '.' )
                && ( $f->[1] ne '..' ) )
            {
                push @folder_todo_list, "${folder}$f->[1]/";
            }
            elsif ( $f->[0] == SMBC_FILE )
            {    # If it is not a folder...process it
                my $remote_file = "${folder}$f->[1]";
                my @tab = $smb->stat("smb://$machine_ip/$remote_file");
                my $ref = $remote_file_list{"$remote_file"};
                $ref->{mtime}  = $tab[ $SMBSTAT{"mtime"} ];
                $ref->{size}   = $tab[ $SMBSTAT{"size"} ];
                $ref->{folder} = "$folder";
                $ref->{status} = '';

                my $filetostat;
                if ((   ($current_backup_exists)
                        && ( -f "$backup_location/current.backup/$remote_file"
                        )
                        && ( $filetostat =
                            "$backup_location/current.backup/$remote_file" )
                    )
                    || (   ( defined($old_backup_id) )
                        && ( $old_backup_id ne "" )
                        && ( -f "$backup_location/$old_backup_id/$remote_file"
                        )
                        && ( $filetostat =
                            "$backup_location/$old_backup_id/$remote_file" )
                    )
                    )
                {

                    # did we already backup this file
                    my @stats = stat($filetostat);
                    $ref->{status} = 'b'
                        if (
                        (   $tab[ $SMBSTAT{"mtime"} ]
                            == $stats[ $UNIXSTAT{"mtime"} ]
                        )
                        && ( $tab[ $SMBSTAT{"size"} ]
                            == $stats[ $UNIXSTAT{"size"} ] )
                        );    # is the file same size and time
                    if ($DEBUG) {
                        my ( $datesign, $sizesign );
                        $tab[ $SMBSTAT{"mtime"} ]
                            == $stats[ $UNIXSTAT{"mtime"} ]
                            ? $datesign = "<>"
                            : $datesign = "=";
                        $tab[ $SMBSTAT{"size"} ]
                            == $stats[ $UNIXSTAT{"size"} ]
                            ? $sizesign = "<>"
                            : $sizesign = "=";
                        PBUtils->logclient( $machine_name, $pid, "info",
                            "$backup_location/current.backup/$remote_file : [date] "
                                . $tab[ $SMBSTAT{"mtime"} ]
                                . " $datesign "
                                . $stats[ $UNIXSTAT{"mtime"} ]
                                . " | [size] "
                                . $tab[ $SMBSTAT{"size"} ]
                                . " $sizesign "
                                . $stats[ $UNIXSTAT{"size"} ] );
                    }
                }

                if ($excludecvs) {
                    $ref->{status} = 's' if ( $remote_file =~ /cvs\/root/i );
                    $ref->{status} = 's'
                        if ( $remote_file =~ /cvs\/repository/i );

                    # We automatically skip all files that are under cvs
                    if ( $remote_file =~ /^(.*?)\/cvs\/entries/i ) {
                        my $basedir = $1;
                        my $entries = "";
                        $ref->{status} = 's';
                        if ( my $fd =
                            $smb->open("smb://$machine_ip/$remote_file") )
                        {
                            while ( defined( my $part = $smb->read($fd) ) ) {
                                if ( $part eq "-1" && $! != 0 ) {
                                    PBUtils->logclient( $machine_name, $pid,
                                        "err",
                                        "ERROR DETECTED DURING COPY!!: $!" );
                                }
                                $entries .= $part;
                            }
                            $smb->close($fd);

                            # Replace cr/lf with lf
                            $entries =~ s/\r\n/\n/g;

                            foreach ( split( '\n', $entries ) ) {
                                if (/^\/(.*?)\//) {
                                    my $temp     = $basedir . "/" . $1;
                                    my $temp_ref = $remote_file_list{$temp};
                                    $temp_ref->{status} = 's';
                                    $remote_file_list{$temp} = $temp_ref;
                                    $skip_count++;
                                }
                            }
                        }
                        else {
                            PBUtils->logclient( $machine_name, $pid, "err",
                                "Can't open file smb://$machine_ip/$remote_file for reading: $!"
                            );
                        }
                    }
                }

        # We automatically skip some files defined in /etc/personalbackup.conf
                for (@excludefiles) {
                    my $excludeitem = $_;
                    if ( $remote_file =~ /${excludeitem}/i ) {
                        $ref->{status} = 's';
                        $skip_count++;
                    }
                }

                if ( $remote_file =~ /;/ )
                { # We are not even going to try to backup files which have a ; in their filename!
                    $ref->{status} = 's';
                }

         # Exclude all files if the maximum (vpn) backup size has been reached
                if ( $ref->{status} eq 's' ) {
                    $skip_size += $ref->{size};
                }
                elsif ( $ref->{status} eq 'b' ) {
                    $backup_size += $ref->{size};
                    $backup_file_count++;
                }
                elsif (
                    (      $maxbackupsize != 0
                        && $backup_size >= ( $maxbackupsize * 1024 * 1024 )
                    )
                    || (   $maxvpnsize != 0
                        && $backup_size >= ( $maxvpnsize * 1024 * 1024 )
                        && $vpn_enabled eq 'Y' )
                    )
                {
                    $ref->{status} = 'l';
                    $limit_size += $ref->{size};
                    $skip_count++;
                }
                else {
                    $backup_size += $ref->{size};
                    $backup_file_count++;
                }

                $remote_file_list{"$remote_file"} = $ref;
            }
        }

        # all files are processed... process folders now
        foreach my $folder_todo (@folder_todo_list) {
            &process_tree( "$folder_todo", $share_id );
        }
        $smb->close($fd);
    }
    else {
        PBUtils->logclient( $machine_name, $pid, "err",
            "Error in process_tree routine -> opendir(\"smb://$machine_ip/$folder\"): $!"
        );
    }
}

sub process_modtime {
    my ( $file, $tmpdir ) = @_;

    # Change the modification date of the local file.
    my $ref = $remote_file_list{$file};
    PBUtils->logclient( $machine_name, $pid, "info",
        "Changing modification date of local file '$file' to $ref->{mtime}" )
        if $DEBUG;
    my $rslt = utime( $ref->{mtime}, $ref->{mtime}, "$tmpdir/$file" );
    PBUtils->logclient( $machine_name, $pid, "err",
        "Failed to execute \"utime($ref->{mtime},$ref->{mtime},\"$tmpdir/$file\")\""
    ) unless $rslt != 0;
}

sub sendnotification {
    my $starttime = sprintf( "%02d:%02d",
        ( localtime( time + $messagedelay ) )[2],
        ( localtime( time + $messagedelay ) )[1] );

    # Send a notification mail if the user wants it
    my $query =
          "SELECT u.username"
        . "     , m.email"
        . "     , u.emailadres"
        . "     , m.notification"
        . "  FROM machines m"
        . "     , users u"
        . " WHERE m.machine_name = '$machine_name'"
        . "   AND u.id=m.user_id";
    my ( $user, $email, $emailaddress, $notification ) =
        $dbh->exec_single_select($query);

    PBUtils->logclient( $machine_name, $pid, "info",
        "Start client for user '$user' machine '$machine_name' ip '$machine_ip'"
    );

    if ( $email eq 'Y' && $notification eq 'Y' ) {
        PBMessage->send_notif_mail( $machine_name, $pid, $language,
            $starttime, $emailaddress );
        PBUtils->logclient( $machine_name, $pid, "info",
            "Now waiting for '$messagedelay' seconds before continuing." );
        sleep($messagedelay);
    }
}

sub sendresult {

    my $query =
          "SELECT u.emailadres"
        . "     , u.language"
        . "     , m.info"
        . "     , m.email"
        . "  FROM machines m"
        . "     , users u"
        . " WHERE m.machine_name = '$machine_name'"
        . "   AND u.id=m.user_id";

    my ( $emailaddress, $language, $info, $email ) =
        $dbh->exec_single_select($query);

    if ( $info eq 'Y' && $email eq 'Y' ) {
        PBMessage->send_overview_mail( $machine_name, $pid, $language,
            $emailaddress );
    }

}

sub smbclient {
    my ( $share_id, $share_name, $username, $password ) = @_;
    my ( $fd, $logon );
    my $error = "OK", $local_logon = "";

    # Set status to busy
    $dbh->exec_dml( "UPDATE shares SET backed_up = ? WHERE id = ?",
        $STATUS{"copying"}, $share_id );

    # Try to connect to the remote machine
    if ((   $smb = new Filesys::SmbClient(
                username  => "${username}",
                password  => "$password",
                workgroup => "$workgroup",
                debug     => 1
            )
        )
        )
    {
        if ( ( $fd = $smb->opendir("smb://$machine_ip/$share_name") ) ) {
            $smb->close($fd);
            $logon = "Domain Logon";
        }
        else {
            $error =
                "Error connecting to machine '$machine_ip - $machine_name' with Domain Logon: "
                . $!;
        }
    }
    else {
        $error =
            "Error connecting to machine '$machine_ip - $machine_name': "
            . $!;
    }

# If the previous attempt did not work, try again with username "machine\\user"
    if ( $error ne "OK" ) {
        $error       = "OK";
        $local_logon = "${machine_name}\\";
        if ((   $smb = new Filesys::SmbClient(
                    username  => "${local_logon}${username}",
                    password  => "$password",
                    workgroup => "$workgroup",
                    debug     => 1
                )
            )
            )
        {
            if ( ( $fd = $smb->opendir("smb://$machine_ip/$share_name") ) ) {
                $smb->close($fd);
                $logon = "Non Domain Logon";
            }
            else {
                $error =
                    "Error connecting to machine '$machine_ip - $machine_name' with Non Domain Logon: "
                    . $!;
            }
        }
        else {
            $error =
                "Error connecting to machine '$machine_ip - $machine_name': "
                . $!;
        }
    }

    # Log the error
    if ( $error ne "OK" ) {
        PBUtils->logclient( $machine_name, $pid, "err", $error );
    } else {
       PBUtils->logclient( $machine_name, $pid, "info", $logon );
    }

    return ($error);
}

sub perform_backup {
    my ($machine_id, $share_id,      $share_name,
        $username,   $password,      $subdir_ref,
        $user,       $maxbackupsize, $maxvpnsize
    ) = @_;
    my ($start_backup, $stop_backup,  $backup_time, $status,
        $subdir,       $log_location, $remote_file
    );
    my @folders = @$subdir_ref;

    %remote_file_list  = ();    # Empty the remote file list.
    $backup_size       = 0;
    $skip_size         = 0;
    $limit_size        = 0;
    $backup_file_count = 0;
    $skip_count        = 0;

    PBUtils->logclient( $machine_name, $pid, "info",
        "Performing backup for share $share_name ($share_id)" );

# See if the machine is still connected to the network, if not stop the backup attempt!
    &reset_alarm();

    $backup_location = "${pooldir}/${machine_name}/${share_id}";
    my $query =
          "SELECT backup_id"
        . "  FROM backuplog"
        . " WHERE status='S'"
        . "   AND share_id='$share_id'"
        . "   AND backup_date = (SELECT MAX(backup_date) FROM backuplog WHERE share_id='$share_id' AND status='S')";
    ($old_backup_id) = $dbh->exec_single_select($query);
    if ( -d "$backup_location/current.backup" ) {

# Something went wrong the last time...so we are just going to continue were we left off.
        $current_backup_exists = 1;
        PBUtils->logclient( $machine_name, $pid, "info",
            "Incomplete backup set found...going to continue were we left off."
        );
    }
    elsif (
        !( $old_backup_id ne "" && -d "$backup_location/$old_backup_id" ) )
    {
        $old_backup_id = "";
    }

    unlink("${backup_location}/current.backup.dbm")
        if ( -e "${backup_location}/current.backup.dbm" );

    # Remove lock if it should exist after improper cleanup
    unlink("${backup_location}/__db.current.backup.dbm")
        if ( -e "${backup_location}/__db.current.backup.dbm" );

    tie %remote_file_list, "MLDBM", "${backup_location}/current.backup.dbm",
        O_RDWR | O_CREAT, 0644
        || PBUtils->logclient( $machine_name, $pid, "die",
        "Cannot tie dbm file '${backup_location}/current.backup.dbm': $!" );

    PBUtils->logclient( $machine_name, $pid, "info",
        "Creating remote file list of current share." );
    foreach $subdir (@folders) {
        &process_tree( "${share_name}${subdir}", $share_id );

        ( my $backup_size_conv ) = PBUtils->convert($backup_size);
        ( my $skip_size_conv )   = PBUtils->convert($skip_size);
        PBUtils->logclient( $machine_name, $pid, "info",
            "Backup size for share '$subdir' $backup_size_conv" );
        PBUtils->logclient( $machine_name, $pid, "info",
            "Skip size for share '$subdir' $skip_size_conv" );
        if ( $limit_size > 0 ) {
            my ($limit_size_conv) = PBUtils->convert($limit_size);
            if ( $vpn_enabled eq 'Y' ) {
                PBUtils->logclient( $machine_name, $pid, "info",
                    "Maximum vpn backup size of $maxvpnsize Mb reached. Skipped $limit_size_conv for share '$subdir'"
                );
            }
            else {
                PBUtils->logclient( $machine_name, $pid, "info",
                    "Maximum backup size of $maxbackupsize Mb reached. Skipped $limit_size_conv for share '$subdir'"
                );
            }
        }
    }

    # Figure out if we already tried backed up today
    $query =
          "SELECT backup_id"
        . "  FROM backuplog"
        . " WHERE machine_id = '$machine_id'"
        . "   AND share_id='$share_id'"
        . "   AND backup_date=CURRENT_DATE"
        . "   AND STATUS <> 'S'";
    ($backup_id) = $dbh->exec_single_select($query);

# See if the machine is still connected to the network, if not stop the backup attempt!
    &reset_alarm();

    # Here the "real" backing up begins :)
    if ( $backup_size > 0 ) {
        $start_backup = time;
        my $query;
        my $i = 0;

        PBUtils->logclient( $machine_name, $pid, "info",
            "Backup backup started at $start_backup" );

     # Do the actual backup of the data from the client PC to the backupserver
        if ( !$backup_id )
        {    # We did not try today...create a new entry in the database
            my $query =
                  "INSERT INTO backuplog"
                . " ( machine_id"
                . " , share_id"
                . " , backup_location"
                . " , backup_size"
                . " , skip_size"
                . " , limit_size"
                . " , skip_count"
                . " , vpn"
                . " , transfer_size"
                . " , status)"
                . " VALUES (?, ?, ?, ?, ?, ?, ?, ?, '0', 'I')";
            $dbh->exec_dml(
                $query,           $machine_id,  $share_id,
                $backup_location, $backup_size, $skip_size,
                $limit_size,      $skip_count,  $vpn_enabled
            );
            $query =
                  "SELECT backup_id"
                . "  FROM backuplog"
                . " WHERE machine_id = '$machine_id'"
                . "   AND share_id='$share_id'"
                . "   AND backup_date=CURRENT_DATE"
                . "   AND STATUS <> 'S'";
            ($backup_id) = $dbh->exec_single_select($query);
        }
        else
        { # We already tried today...update the existing entry in the database
            PBUtils->logclient( $machine_name, $pid, "info",
                "Share '$share_id' already backed up today...removing obsoleted folder '$backup_location/$backup_id' now"
            );

#			my $rslt = rmtree ("$backup_location/$backup_id");
#			PBUtils->logclient($machine_name, $pid, "err", "Unable to execute 'rmtree(\"$backup_location/$backup_id\")'") if ($rslt==0 && $!!=0);
            my $query =
                  "UPDATE backuplog"
                . "   SET backup_location = ?"
                . "     , backup_size = ?"
                . "     , skip_size = ?"
                . "     , limit_size = ?"
                . "     , skip_count = ?"
                . "     , vpn = ?"
                . "     , status = 'I'"
                . " WHERE backup_id = ?";
            $dbh->exec_dml( $query, $backup_location, $backup_size, $skip_size,
                $limit_size, $skip_count, $vpn_enabled, $backup_id );
        }

        my $file_count     = 0;
        my $fail_count     = 0;
        my $backup_count   = 0;
        my $pct            = 0;
        my $transfer_size  = 0;
        my $backed_up_size = 0;
        my $lastping       = time;
        my $pct_threshold  = int( $backup_file_count / 100 );
        my $pct_file_count = $pct_threshold;
        foreach $remote_file ( keys %remote_file_list ) {
            my $ref = $remote_file_list{$remote_file};
            next if ( $ref->{status} ne '' && $ref->{status} ne 'b' );

# After each $pinginterval seconds we are going to ping the machine to see if it is still connected to the network, if not we stop the backup attempt!
            if ( time - $lastping > $pinginterval ) {
                PBUtils->logclient( $machine_name, $pid, "info",
                    "Performing Alive ping..." )
                    if $DEBUG;
                &reset_alarm();
                $lastping = time;
            }
            $file_count++;
            $pct_file_count--;
            if ( $pct_file_count <= 0 ) {
                $pct = 100 / $backup_file_count * $file_count;
                my $query =
                      "UPDATE shares"
                    . "   SET progress = CEIL(?)"
                    . " WHERE id = ?";
                $dbh->exec_dml( $query, $pct, $share_id );
                $query =
                      "UPDATE backuplog"
                    . "   SET backup_count = ?"
                    . "     , fail_count = ?"
                    . "     , transfer_size = transfer_size + ?"
                    . " WHERE backup_id = ?";
                $dbh->exec_dml(
                    $query,         $backup_count, $fail_count,
                    $transfer_size, $backup_id
                );
                $pct_file_count = $pct_threshold;
                $transfer_size  = 0;
            }
            PBUtils->logclient( $machine_name, $pid, "info",
                "Now transferring file '$remote_file'" )
                if $DEBUG;
            my $localfile =
                $backup_location . "/current.backup/" . $remote_file;
            if ( $localfile =~ /^(.*)\/[^\/]*$/ ) {
                if ( !-d "$1" ) {
                    my $local_dir = $1;
                    $local_dir =~ s/\`/\\\`/g;
                    my $rslt = system("$mkdir -p \"$local_dir\"");
                    PBUtils->logclient( $machine_name, $pid, "err",
                        "Failed to execute '$mkdir -p \"$local_dir\"'" )
                        unless $rslt == 0;
                    PBUtils->logclient( $machine_name, $pid, "info",
                        "Creating local folder $local_dir" )
                        if $DEBUG;
                }
            }
            if ( $ref->{status} eq '' ) {
                if (   ($current_backup_exists)
                    && ( -f "$backup_location/current.backup/$remote_file" ) )
                {
                    unlink("$backup_location/current.backup/$remote_file");
                }
                my $copyerror;
                open( LOCALFILE, ">$localfile" )
                    || PBUtils->logclient( $machine_name, $pid, "err",
                    "Unable to open '$localfile' for writing" );

                if ( my $fd = $smb->open("smb://$machine_ip/$remote_file") ) {
                    while ( defined( my $block = $smb->read($fd) ) ) {
                        if ( $block eq "-1" && $! != 0 ) {
                            $copyerror = "Error: $!";
                            PBUtils->logclient( $machine_name, $pid, "err",
                                "ERROR DETECTED DURING COPY!!: $copyerror" );
                        }
                        print LOCALFILE $block;
                    }
                    $smb->close($fd);
                }
                else {
                    $copyerror = "Error: $!";
                }
                close(LOCALFILE);

                if ( not $copyerror ) {

                    # Set the correct modification date on the copied file
                    &process_modtime( $remote_file,
                        "$backup_location/current.backup" );
                    $ref->{status} = 'b';
                    $transfer_size  += $ref->{size};
                    $backed_up_size += $ref->{size};
                    $backup_count++;
                }
                else {
                    PBUtils->logclient( $machine_name, $pid, "err",
                        "Unable to backup remote file $remote_file: $copyerror"
                    );
                    $ref->{status} = 'f';
                    $fail_count++;
                }
                $remote_file_list{$remote_file} = $ref;
            }
            else {
                link "$backup_location/$old_backup_id/$remote_file",
                    "$backup_location/current.backup/$remote_file"
                    unless (
                    -f "$backup_location/current.backup/$remote_file" );
            }
        }
        my $error_pct =
            sprintf( "%.2f", 100 / $backup_file_count * $fail_count );
        PBUtils->logclient( $machine_name, $pid, "info",
                  "Successfully backed up "
                . ( 100 - $error_pct )
                . "% of the files" );

        PBUtils->logclient( $machine_name, $pid, "info",
            "Storing backup data in log dbm file ${backup_location}/${backup_id}.dbm"
        );

        # Store the results in the logfile
        # Make sure there is no old log db present
        if ( -e "${backup_location}/${backup_id}.dbm" ) {
            unlink("${backup_location}/${backup_id}.dbm");
        }

        rename(
            "${backup_location}/current.backup.dbm",
            "${backup_location}/${backup_id}.dbm"
        );

        $stop_backup = time;
        PBUtils->logclient( $machine_name, $pid, "info",
            "Backup stopped at $stop_backup" );
        $backup_time = $stop_backup - $start_backup;
        ( my $backup_time_conv ) = PBUtils->convert_sec($backup_time);

        if ( ( 100 - $error_pct ) >= $backupthreshold )
        {    # Set status to S(uccessfull)
            PBUtils->logclient( $machine_name, $pid, "info",
                "Effective backup time: $backup_time_conv" );
            my $query =
                  "UPDATE shares"
                . "   SET backed_up = ?"
                . " WHERE id = ?"
                . "   AND share_name = ?";
            $dbh->exec_dml( $query, $backed_up_size, $share_id, $share_name );
            $query =
                  "UPDATE backuplog"
                . "   SET status = 'S'"
                . "     , backup_time = ?"
                . "     , skip_count = ?"
                . "     , backup_count = ?"
                . "     , fail_count = ?"
                . "	, transfer_size = transfer_size + ?"
                . " WHERE backup_id = ?";
            $dbh->exec_dml(
                $query,      $backup_time,   $skip_count, $backup_count,
                $fail_count, $transfer_size, $backup_id
            );
            my $rslt =
                system(
                "mv $backup_location/current.backup $backup_location/$backup_id"
                );
            PBUtils->logclient( $machine_name, $pid, "err",
                "Failed to execute \"mv $backup_location/current.backup $backup_location/$backup_id\""
            ) unless $rslt == 0;

            PBUtils->logclient( $machine_name, $pid, "info",
                "Backup successful" );
        }
        else {    # Set status to F(ailed)
            my $query =
                  "UPDATE shares"
                . "   SET errcount=errcount+1"
                . "     , backed_up = ?"
                . " WHERE id = ?";
            $dbh->exec_dml( $query, $STATUS{"skipped"}, $share_id );
            $query =
                  "UPDATE backuplog"
                . "   SET status = 'F'"
                . "     , skip_count = ?"
                . "     , backup_count = ?"
                . "     , fail_count = ?"
                . "	, transfer_size = transfer_size + ?"
                . " WHERE backup_id = ?";
            $dbh->exec_dml(
                $query,      $skip_count,    $backup_count,
                $fail_count, $transfer_size, $backup_id
            );
            PBUtils->logclient( $machine_name, $pid, "err", "Backup failed" );
        }
    }
    else {
        my $backed_up;
        $backed_up = $STATUS{"nodiff"};

        $dbh->exec_dml( "UPDATE shares SET backed_up = ? WHERE id = ?",
            $backed_up, $share_id );

        # Set status to E(mpty)
        if ( !$backup_id )
        {    # We did not try today...create a new entry in the database
            my $query =
                  "INSERT INTO backuplog"
                . " ( machine_id"
                . " , share_id"
                . " , backup_location"
                . " , backup_size"
                . " , skip_size"
                . " , limit_size"
                . " , status"
                . " , transfer_size)"
                . " VALUES (?, ?, ?, '0', ?, ?, 'E', '0')";
            $dbh->exec_dml( $query, $machine_id, $share_id, $backup_location,
                $skip_size, $limit_size );
        }
        else
        { # We already tried today...update the existing entry in the database
            my $query =
                  "UPDATE backuplog"
                . "   SET backup_location = ?"
                . "     , backup_size = '0'"
                . "     , backup_count = '0'"
                . "     , fail_count = '0'"
                . "     , skip_size = ?"
                . "     , limit_size = ?"
                . "     , skip_count = ?"
                . "     , status = 'E'"
                . " WHERE backup_id = ?";
            $dbh->exec_dml( $query, $backup_location, $skip_size, $limit_size,
                $skip_count, $backup_id );
        }

        PBUtils->logclient( $machine_name, $pid, "info",
            "Backup successful. No changes found." );
    }
}

sub smbclient_close {
    my ( $share_id, $start_conn ) = @_;
    my ( $stop_conn, $conn_time );

    $stop_conn = time;
    $conn_time = $stop_conn - $start_conn;
    ( my $conn_time_conv ) = PBUtils->convert_sec($conn_time);

    PBUtils->logclient( $machine_name, $pid, "info",
        "SmbClient connection successfully closed at $stop_conn" );
    PBUtils->logclient( $machine_name, $pid, "info",
        "Effective connection time: $conn_time_conv" );

    my $query =
          "UPDATE backuplog"
        . "   SET conn_time = ?"
        . " WHERE share_id = ?"
        . "   AND backup_date = CURRENT_DATE";
    $dbh->exec_dml( $query, $conn_time, $share_id );
}

sub backup_failed {
    my ( $share_id, $text ) = @_;

    my $query =
          "UPDATE shares"
        . "   SET errcount=errcount+1"
        . "     , backed_up = ?"
        . " WHERE id = ?";
    $dbh->exec_dml( $query, $STATUS{"skipped"}, $share_id );

    PBUtils->logclient( $machine_name, $pid, "err", $text );
}

sub skipshare {
    my ($share_id) = @_;

    # If after x times of retry, the share is still not available, disable it
    my $query =
        "SELECT errcount" . "  FROM shares" . " WHERE id = '$share_id'";
    my ($errcount) = $dbh->exec_single_select($query);
    if ( $errcount > $maxretry ) {
        $query =
              "UPDATE shares"
            . "   SET backed_up = ?"
            . "     , errcount = '0'"
            . " WHERE id = ?";
        $dbh->exec_dml( $query, $STATUS{"skippedpermanent"}, $share_id );
    }
}

sub setmachinestatus {
    my $query =
          "SELECT COUNT(*)"
        . "  FROM shares"
        . " WHERE backed_up = '"
        . $STATUS{"skipped"} . "'"
        . "   AND machine_id  = '$machine_id'";
    my ($skipcount) = $dbh->exec_single_select($query);
    $query =
          "SELECT COUNT(*)"
        . "  FROM shares"
        . " WHERE backed_up = '"
        . $STATUS{"skippedpermanent"} . "'"
        . "   AND machine_id  = '$machine_id'";
    my ($failcount) = $dbh->exec_single_select($query);
    $query =
          "SELECT COUNT(*)"
        . "  FROM shares"
        . " WHERE (backed_up > 0 OR backed_up = '"
        . $STATUS{"nodiff"} . "')"
        . "   AND machine_id  = '$machine_id'";
    my ($successcount) = $dbh->exec_single_select($query);
    $query =
          "SELECT COUNT(*)"
        . "  FROM shares"
        . " WHERE machine_id  = '$machine_id'";
    my ($totalcount) = $dbh->exec_single_select($query);

    PBUtils->logclient( $machine_name, $pid, "info",
        "On a total of $totalcount shares there are $skipcount shares skipped, $failcount shares failed and $successcount shares successful"
    );
    if ( $skipcount > 0 || $failcount > 0 )
    {    # Not all shares are properly backed up.
        $dbh->exec_dml( "UPDATE machines SET status = ? WHERE id = ?",
            $STATUS{"skipped"}, $machine_id );
        return 0;
    }
    else {    # All shares of the machine have been backed up successfully
        $dbh->exec_dml( "UPDATE machines SET status = ? WHERE id = ?",
            $STATUS{"successful"}, $machine_id );
        return 1;
    }
}

sub detectvpn {
    my ($machine_ip) = @_;
    my @range     = split /\:/, $vpnrange;
    my $lower     = inet_aton( $range[0] );
    my $upper     = inet_aton( $range[1] );
    my $packed_ip = inet_aton($machine_ip);

    if ( $packed_ip lt $lower || $packed_ip gt $upper ) { return 'N'; }
    else {
        PBUtils->logclient( $machine_name, $pid, "info",
            "Machine $machine_name is connected via vpn." );
        return 'Y';
    }
}

sub reset_alarm {

    # Ping machine to check if it is still alive.
    my $p = Net::Ping->new("icmp");

    # Reset alarm and continue work if it is still alive and kicking
    if ( $p->ping($machine_ip) ) {
        alarm($clienttimeout);
    }
    else {

        # In 1 second we are going to bail out...machine is gone
        alarm(1);
    }
    $p->close();
}

sub alarm_client {

    # Machine is gone...
    my $query =
          "UPDATE shares"
        . "   SET backed_up = '"
        . $STATUS{"skipped"} . "'"
        . "     , errcount=errcount+1"
        . " WHERE machine_id = '$machine_id'"
        . "   AND backed_up<0";
    $dbh->exec_dml($query);
    $query =
        "SELECT id" . "  FROM shares" . " WHERE machine_id = '$machine_id'";
    my $result = $dbh->exec_select($query);
    foreach my $share_id (@$result) {
        &skipshare( @$share_id[0] );
    }
    $dbh->disconnect;
    PBUtils->logclient( $machine_name, $pid, "err",
        "Stopped personalbackupc for machine '$machine_name' due to timeout"
    );
    exit(1);
}

sub quit_client {

    # Client failed to backup...try again later
    my $query =
          "UPDATE shares"
        . "   SET backed_up = '"
        . $STATUS{"skipped"} . "'"
        . " WHERE machine_id = '$machine_id'"
        . "   AND backed_up<0";
    $dbh->exec_dml($query);
    $query =
          "UPDATE machines"
        . "   SET status = '"
        . $STATUS{"skipped"} . "'"
        . " WHERE machine_name = '$machine_name'";
    $dbh->exec_dml($query);
    $dbh->disconnect;
    PBUtils->logclient( $machine_name, $pid, "err",
        "Caught sigterm for personalbackupc for machine '$machine_name'" );
    exit(1);
}

$dbh->disconnect;

# Client normally finished
PBUtils->logclient( $machine_name, $pid, "info",
    "Stopped personalbackupc for machine '$machine_name'" );
exit(0);