App-SimpleBackuper

 view release on metacpan or  search on metacpan

bin/simple-backuper  view on Meta::CPAN

		App::SimpleBackuper::_print_table([
				['rights', 'owner', 'group', 'size', 'mtime', 'backups'],
				map {[
					(
						S_ISDIR($_->{mode}) ? 'd' :
						S_ISLNK($_->{mode}) ? 'l' :
						S_ISREG($_->{mode}) ? '-' :
						'?'
					)
					.((S_IRUSR & $_->{mode}) ? 'r' : '-')
					.((S_IWUSR & $_->{mode}) ? 'w' : '-')
					.((S_IXUSR & $_->{mode}) ? 'x' : '-')
					.((S_IRGRP & $_->{mode}) ? 'r' : '-')
					.((S_IWGRP & $_->{mode}) ? 'w' : '-')
					.((S_IXGRP & $_->{mode}) ? 'x' : '-')
					.((S_IROTH & $_->{mode}) ? 'r' : '-')
					.((S_IWOTH & $_->{mode}) ? 'w' : '-')
					.((S_IXOTH & $_->{mode}) ? 'x' : '-')
					,
					$_->{user},
					$_->{group},
					$_->{size},
					$_->{mtime},
					join(', ', @{ $_->{backups} }),
				]} @{ $result->{versions} }
			],
			1,
		);
	} else {
		print "This files has no backuped versions";
		print " (but it's subfiles has)" if @{ $result->{subfiles} };
		print "\n";
	}
}
elsif($command eq 'restore') {
	
	($state{db}->{backups} and @{ $state{db}->{backups} })
		or usage("Database file is not exists or empty. May be your backups database is located in backup storage. You can restore in here with 'restore-db' command");
	
	$options{$_} or usage("Required option --$_ doesn't specified") foreach qw(path destination storage backup-name);
	
	my $result = App::SimpleBackuper::Info(\%options, \%state);
	
	if(! grep {$options{'backup-name'} eq $_} map {@{ $_->{backups} }} @{ $result->{versions} }) {
		usage(qq|Backup named '$options{"backup-name"}' of path '$options{path}' was not found|);
	}
	
	App::SimpleBackuper::Restore(\%options, \%state);
}
elsif($command eq 'restore-db') {
	
	! $state{db} or ! @{ $state{db}->{backups} }
		or usage(qq|Database already exists and contains |.@{ $state{db}->{backups} }.qq| backups. |
			.qq|If you want to rewrite database file with restored one, please delete current database file $options{db}|);
	
	$options{'priv-key'} or usage("Required option --priv-key doesn't specified");
	
	$options{storage} or usage("Required option --storage doesn't specified");
	
	open(my $h, '<', $options{'priv-key'}) or usage(qq|Can't read private key file '$options{"priv-key"}': $!|);
	$state{rsa} = Crypt::OpenSSL::RSA->new_private_key( join('', <$h>) );
	close($h);
	
	App::SimpleBackuper::RestoreDB(\%options, \%state);
}
# TODO: статистика: кол-во бекапов, кол-во бекапов без единого удалённого файла, % файлов в каждом бекапе
elsif($command eq 'stats') {
	App::SimpleBackuper::_print_table([
		['name', 'max files cnt', 'current files cnt', '%'],
		map {[
			$_->{name},
			($_->{is_done} ? $_->{max_files_cnt} : '? (unfinished)'),
			$_->{files_cnt},
			($_->{is_done} ? int($_->{files_cnt} / $_->{max_files_cnt} * 100).'%' : ''),
		]} map {$state{db}->{backups}->unpack($_)} @{ $state{db}->{backups} }
	]);
}
elsif($command) {
	usage("Unknown command $command");
}


__DATA__
Usage: simple-backuper <COMMAND> [OPTIONS]

COMMANDS:
    backup        - creates a new backup.              Required options: --backup-name. Possible: --cfg.
    info          - prints info of files in backup.    Possible options: --db, --path, --cfg
    restore       - restores files from backup.        Required options: --path, --backup-name, --storage, --destination.
                                                       Possible options: --db, --write.
    restore-db    - fetch from storage & decrypt database. Required options: --storage --priv-key
    storage-check - check for existents all data on storage with local database.                    Possible option: --cfg
    storage-fix   - fix local database for loosen data and remove unknown extra files from storage. Possible option: --cfg
    stats         - statistics about files count in backups.                                        Possible options: --cfg, --db

OPTIONS:
    --cfg %path%            - path to config file (see below). (default is ~/.simple-backuper/config)
    --db %path%             - path to db file. (by default using config value if possible, or ~/.simple-backuper/db otherwise)
                              This file need for any operations. It copied to backup storage dir each creating backup time.
    --backup-name %name%    - name of creating, listing or restoring backup.
    --path %path%           - %path% of files in backup. (default: /)
    --priv-key %path%        - %path% to private key file for decryption.
    --storage %path%        - %path% to storage dir. Remote SFTP path must begins with '%host%:'.
    --destination %path%    - destination path to restore files.
    --write                 - Without this option restoring use dry run.

For EXAMPLES see README.md

CONFIG file must be a json-file like this:
{
    "db":                   "~/.simple-backuper/db",
    // This database file changes every new backup. ~/.simple-backuper/db - is a default value.
    
    "compression_level":    9,
    // LZMA algorythm supports levels 1 to 9
    
    "public_key":           "~/.simple-backuper/key.pub",
    // This key using with "backup" command.
    // For restore-db command you need to use private key of this public key.
    
    // Creating new pair of keys:



( run in 0.899 second using v1.01-cache-2.11-cpan-39bf76dae61 )