diff options
Diffstat (limited to 'torusbac.pl')
| -rw-r--r-- | torusbac.pl | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/torusbac.pl b/torusbac.pl new file mode 100644 index 0000000..ac5f34a --- /dev/null +++ b/torusbac.pl @@ -0,0 +1,78 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use POSIX qw(strftime); +use File::Basename; + +my $DATA_FILE = '/home/torus/torus.dat'; +my $COPYPARTY_URL = ''; +my $COPYPARTY_PASS = ''; +my $BACKUP_DIR = '/tmp/torus_backups'; + +$COPYPARTY_PASS = $ENV{COPYPARTY_PASS} if $ENV{COPYPARTY_PASS} && !$COPYPARTY_PASS; + +unless (-f $DATA_FILE) { + die "Data file not found: $DATA_FILE\n"; +} + +mkdir $BACKUP_DIR unless -d $BACKUP_DIR; + +my $timestamp = strftime('%Y.%m.%d.%H.%M', localtime); +my $backup_file = "$BACKUP_DIR/torus.dat.$timestamp"; +my $meta_file = "$BACKUP_DIR/torus.csv.$timestamp"; + +print "Creating backup: $backup_file\n"; +open my $data_in, '<', $DATA_FILE or die "Failed to open data file: $!\n"; +open my $backup_out, '>', $backup_file or die "Failed to create backup: $!\n"; +while (my $chunk = read($data_in, my $buf, 4096)) { + print $backup_out $buf; +} +close $data_in; +close $backup_out; + +print "Creating metadata backup: $meta_file\n"; +open $data_in, '<', $DATA_FILE or die "Failed to open data file: $!\n"; +open my $meta_proc, '|-', "/opt/torus/meta > '$meta_file' 2>/dev/null" or die "Failed to open meta: $!\n"; +while (read($data_in, my $tile, 4096)) { + print $meta_proc $tile; +} +close $data_in; +close $meta_proc; + +print "Compressing files...\n"; +system('gzip', '-9', '-f', $backup_file) == 0 or die "Failed to gzip backup\n"; +system('gzip', '-9', '-f', $meta_file) == 0 or die "Failed to gzip meta\n"; + +$backup_file .= '.gz'; +$meta_file .= '.gz'; + +my $url = $COPYPARTY_URL; +$url .= '/' unless $url =~ m{/$}; +if ($COPYPARTY_PASS) { + $url .= "?pw=$COPYPARTY_PASS"; +} + +for my $file ($backup_file, $meta_file) { + my $basename = basename($file); + print "Uploading $basename to copyparty...\n"; + + my $cmd = "curl -X POST '$url' " . + "-F 'f=\@$file' "; + + if ($COPYPARTY_PASS) { + $cmd .= "-H 'PW: $COPYPARTY_PASS' "; + } + + $cmd .= "--silent --show-error --fail --max-time 600"; + + my $result = system($cmd); + if ($result == 0) { + print "Successfully uploaded $basename\n"; + unlink $file or warn "Failed to delete $file: $!\n"; + } else { + my $exit_code = $result >> 8; + warn "Failed to upload $basename (exit code: $exit_code)\n"; + } +} + +print "Backup completed\n"; |