How to get the script directory, this method is good. update 8 Sep 2018.
1 2 |
use FindBin; $SDIR = $FindBin::Bin; # script directory |
Get a snapshot by the output of rsync. This way is enough for my practical use.
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 |
# # config.pl # # 2016-03-02 # $TARGET_BASE = '/home/user/Desktop/snapshot'; $LOG_DIR = $TARGET_BASE . '/log'; $RSYNC_LOG = $LOG_DIR . '/rsync.log'; $TARGET_OF_SNAPSHOT = $TARGET_BASE . '/share'; $SNAPSHOT_DIR = $TARGET_BASE . '/snapshot'; # backup from $TARGET_OF_SNAPSHOT to $BACKUP_DIR $BACKUP_DIR = $TARGET_BASE . '/backup'; # $SELF_DIR = '/home/user/Desktop/snapshot/bin'; # push(@INC, $SELF_DIR); $EXCLUDE_FROM = $SELF_DIR . '/exclude-from.txt'; # $RSYNC = "/usr/bin/rsync"; $FIND = "/usr/bin/find"; $MKDIR = "/bin/mkdir"; # $GMT = -9; # JST $MAX_SIZE = '256M'; $KEEP_NUMBER_OF_SNAPSHOT = 30; $DELETE_IF_DAY_AFTER_N_DAYS = 400; # end of config.pl |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# # exclude-from.txt # - .DS_Store - .git - .gitignore # temporary - tmp/ - *.tmp # ms office temporary file - ~$*.* # + * |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/usr/bin/perl # # backup.pl # # test script # # 2014-04-14 # use File::Basename 'basename', 'dirname'; use FindBin; # get script directory $SDIR = $FindBin::Bin; # print $SDIR."\n"; require "$SDIR/config.pl"; $cmd = sprintf("%s -azv '%s' '%s' >>'%s'", $RSYNC, $TARGET_OF_SNAPSHOT, $BACKUP_DIR, $RSYNC_LOG); print("$cmd\n"); exit(system($cmd)); |
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 |
#!/usr/bin/perl # # get_snapshot.pl # # 2017-04-14 # # use File::Copy; use File::Basename 'basename', 'dirname'; use FindBin; # get script directory $SDIR = $FindBin::Bin; # print $SDIR."\n"; require "$SDIR/config.pl"; $return_code = 0; if (!open(IN, "< $RSYNC_LOG")) { printf(STDERR "$0: Cannot open log file: $RSYNC_LOG.\n"); exit(1); } @logs = <IN>; close(IN); # print @logs; local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time + $GMT * 60 * 60); $year += 1900; $mon += 1; # /path/@GMT-2017.04.12-00.53.59 $dst_dir = sprintf("%s/\@GMT-%04d.%02d.%02d-%02d.%02d.%02d", $SNAPSHOT_DIR, $year, $mon, $mday, $hour, $min, $sec); $now_int = sprintf("%04d%02d%02d", $year, $mon, $mday) + 0; $copy_count = 0; %srcs; $is_list = 0; $src_dir = dirname($TARGET_OF_SNAPSHOT); foreach $fpath (@logs) { chomp $fpath; # printf("fpath: %s\n", $fpath); if ($is_list && !$fpath) { $is_list = 0; next; } if (!$is_list && $fpath eq 'sending incremental file list') { $is_list = 1; next; } if (!$is_list) { next; } if ($fpath =~ m/^deleting /) { next; } $src = sprintf("%s/%s", $src_dir, $fpath); if (! -f $src) { printf("%s: skip: file not found: '%s'\n", $0, $src); next; } if ($srcs{$src} == 1) { printf("%s: skip: already did snapshot: '%s'\n", $0, $src); next; } # do snapshot ------------------------------ $dst = sprintf("%s/%s", $dst_dir, $fpath); # printf("%s: snapshot: %s => %s\n", $0, $src, $dst); $dir = dirname $dst; # printf("mkdir %s\n", $dir); $cmd = sprintf("%s -p '%s'", $MKDIR, $dir); if (system($cmd)) { $return_code++; printf("%s: failed to make directory: '%s'\n", $0, $dir); next; } # copy the one file to snapshot directory. $cmd = sprintf("%s -av --max-size=%s --exclude-from='%s' '%s' '%s' >/dev/null", $RSYNC, $MAX_SIZE, $EXCLUDE_FROM, $src, $dst); if (system($cmd)) { $return_code++; printf(STDERR "%s: failed to snapshot: '%s' => '%s'\n", $0, $src, $dst); } else { $srcs{$src} = 1; $copy_count++; printf("%s: did snapshot: '%s' => '%s'\n", $0, $src, $dst); } } if (!unlink($RSYNC_LOG)) { printf(STDERR "%s: failed to delete log: '%s'\n", $0, $RSYNC_LOG); } exit($return_code); |
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 |
#!/usr/bin/perl # # purge_snapshot.pl # # 2014-04-14 # use File::Basename 'basename', 'dirname'; use File::Path 'rmtree'; # use File::Copy; use FindBin; # get script directory $SDIR = $FindBin::Bin; # print $SDIR."\n"; require "$SDIR/config.pl"; $return_code = 0; # # Get the snapshot directory list. # if (!opendir(DIRHANDLE, $SNAPSHOT_DIR)) { printf(STDERR "%s: Cannot open snapshot directory: %s.\n", $0, $SNAPSHOT_DIR); exit 1; } @dirs; foreach(readdir(DIRHANDLE)){ # print "$_\n"; next if /^\.{1,2}$/; # skip '.' and '..' $dir = sprintf("%s/%s", $SNAPSHOT_DIR, $_); if (! $dir =~ /^\@GMT\-/) { next; } # @GMT- if (! -d $dir) { next; } push(@dirs, $dir); # print "$dir\n"; } closedir(DIRHANDLE); @dirs = sort @dirs; # print join(',',@dirs),"\n"; # # Keep constant the number of snapshots. # $cut = $#dirs - $KEEP_NUMBER_OF_SNAPSHOT + 1; if ($cut < 1) { printf("%s: No need to purge.\n", $0); exit($return_code); } # Merge from new snapshots to old, # put snapshots together to $dst. for ($i = $cut; $i > 0; $i--) { $src = sprintf("%s", $dirs[$i]); $dst = sprintf("%s", $dirs[$i - 1]); printf("%s: %s => %s\n", $0, $src, $dst); $cmd = sprintf("%s -av --max-size=%s --exclude-from='%s' '%s/' '%s/'", $RSYNC, $MAX_SIZE, $EXCLUDE_FROM, $src, $dst); printf("%s: execute: %s\n", $0, $cmd); if (system($cmd) > 0) { printf(STDERR "%s: failed to execute: %s\n", $0, $cmd); $return_code++; next; } rmtree($src); } if ($return_code) { exit($return_code); } # Move from the merged snapshot to new. # from $dst to $dirs[$cut] # don't use rename and move $save = sprintf("%s", $dirs[$cut]); printf("%s: %s => %s\n", $0, $dst, $save); $cmd = sprintf("%s -av --max-size=%s --exclude-from='%s' '%s/' '%s/'", $RSYNC, $MAX_SIZE, $EXCLUDE_FROM, $dst, $save); printf("%s: execute: %s\n", $0, $cmd); if (system($cmd) > 0) { $return_code++; printf("%s: failed to execute: %s\n", $0, $cmd); } else { rmtree($dst); } if ($SNAPSHOT_DIR =~ /snapshot$/ && $DELETE_IF_DAY_AFTER_N_DAYS > 14) { printf("%s: delete the snapshot files that is %d days old from the changed.\n", $0, $DELETE_IF_DAY_AFTER_N_DAYS); $files = `$FIND '$SNAPSHOT_DIR' -mtime +$DELETE_IF_DAY_AFTER_N_DAYS -delete -print`; if ($files) { printf("%s: deleted old snapshot files: %s\n", $0, $DELETE_IF_DAY_AFTER_N_DAYS, join(", ", $files)); } # delete empty snapshot directories $files = `$FIND '$SNAPSHOT_DIR' -type d -empty -delete -print`; if ($files) { printf("%s: delete empty snapshot directories: %s\n", $0, join(", ", $files)); } } exit($return_code); |
lsyncd でサーバー間ファイル同期してみるメモ / synchronize files between servers with lsyncd