为cpanel的备份文件设置密码
Posted
技术标签:
【中文标题】为cpanel的备份文件设置密码【英文标题】:Set password on backup files of cpanel 【发布时间】:2016-03-11 09:56:56 【问题描述】:我想为Cpanel
备份系统的.tar.gz
文件备份设置密码。
cpanel 使用perl
脚本在/scripts/cpbackup
文件路径中进行备份。
所以,我编辑这个文件并在这里找到这个函数:
sub backupaccts
my $target = shift;
my $all_targets = shift;
Cpanel::FileUtils::TouchFile::touchfile($target);
#if another cpbackup starts just go bye bye
if ( !( $CONF'BACKUPFILES' eq "no" ) )
print "[cpbackup] Running dir & file backup with target : $target\n";
if ( !-e "$target/files" )
mkdir( "$target/files", 0700 );
if ( !-e "$target/dirs" )
mkdir( "$target/dirs", 0700 );
chmod( 0700, "$target/files", "$target/dirs" );
my ( $syncstatus, $syncmessage );
foreach my $file (@FILES)
next if ( !-e $file );
my $rawfile = $file;
$rawfile =~ s/\//_/g;
if ( $CONF'BACKUPINC' eq "yes" )
( $syncstatus, $syncmessage ) = Cpanel::SimpleSync::syncfile( $file, "$target/files/$rawfile", 0, 1 );
else
( $syncstatus, $syncmessage ) = Cpanel::SimpleSync::syncfile( $file, "$target/files/$rawfile", 0, 1 );
if ( $syncstatus != 0 )
if ( cpusystem( $gzip_bin, "-f", "$target/files/$rawfile" ) != 0 )
print STDERR "[cpbackup] Failed to compress file $target/files/$rawfile\n";
if ( $syncstatus == 0 ) print STDERR "[cpbackup] Failed to backup $file ($syncmessage)\n";
foreach my $dir (@DIRS)
next if ( !-e $dir );
my $rawdir = $dir;
$rawdir =~ s/\//_/g;
my @EXCLUDES = (
$dir eq '/var/cpanel'
? ( "--exclude=lastrun/*", "--exclude=bwusagecache/*", "--exclude=serviceauth/*", "--exclude=dnsrequests_db/*", "--exclude=configs.cache/*" )
: ("--exclude=*/proc/*")
);
# Added exclude for /proc for chroot bind setups to prevent error messages
if ( $CONF'BACKUPINC' eq "yes" )
if ( cpusystem( $rsync_bin, $rsyncopts, @EXCLUDES, '--delete', "$dir/", "$target/dirs/$rawdir" ) != 0 )
print STDERR "[cpbackup] Failed to perform incremental backup of $dir/ to $target/dirs/$rawdir\n";
else
if ( cpusystem( $tar_bin, '--use-compress-program=/usr/local/cpanel/bin/gzip-wrapper', '--create', '--preserve-permissions', '--file', "$target/dirs/$rawdir.tar.gz", @EXCLUDES, $dir ) == 0 )
chmod( 0600, "$target/dirs/$rawdir.tar.gz" );
else
print STDERR "[cpbackup] Failed to perform full backup of $dir/ to $target/dirs/$rawdir.tar.gz\n";
print "[cpbackup] Running account backup with target : $target\n";
#BACKUPLOGS no
#mysqlBACKUP accounts
#BACKUPBWDATA yes
$ENV'pkgacct-logs' = ( $CONF'BACKUPLOGS' eq "yes" ? 'yes' : 'no' );
$ENV'pkgacct-mysql' = ( ( $CONF'MYSQLBACKUP' eq "dir" || $CONF'MYSQLBACKUP' eq "no" ) ? 'no' : 'yes' );
$ENV'pkgacct-psql' = ( $CONF'PSQLBACKUP' eq "no" ? 'no' : 'yes' );
$ENV'pkgacct-bwdata' = ( $CONF'BACKUPBWDATA' eq "no" ? 'no' : 'yes' );
my %user_error_map = ();
if ( $CONF'BACKUPACCTS' ne 'no' )
# we need to be sure that all accounts are converted to the new backup system
# user config need to have the LEGACY_BACKUP entry
# this can be missing when importing an account prior 11.36
# or if an error happen during the upgrade process
Install::LegacyBackups->new()->perform();
Cpanel::PwCache::init_passwdless_pwcache();
my @cpusers = Cpanel::Config::Users::getcpusers();
foreach my $user ( sort @cpusers )
my $user_conf = Cpanel::Config::LoadCpUserFile::load($user);
$user_conf->'LEGACY_BACKUP' = 0 if !exists $user_conf->'LEGACY_BACKUP';
next if !$user_conf->'LEGACY_BACKUP';
if ( $CONF'BACKUPINC' eq 'yes' )
my $last_update_time = time();
utime( $last_update_time, $last_update_time, "$target/$user" );
if ( cpusystem( $pkgacct, ( $CONF'COMPRESSACCTS' eq 'no' ? '--nocompress' : () ), ( $CONF'BACKUPINC' eq "yes" ? '--incremental' : () ), $user, $target, 'backup' ) != 0 )
print STDERR "[cpbackup] Failed to back up account $user\n";
$user_error_map$user'account' =
'error_message' => ( $LAST_CPUSYSTEM_ERROR || 'Failed to backup account' ) # FIXME: This is a hack to avoid refactoring cpusystem
;
else
if ( $CONF'BACKUPTYPE' eq 'ftp' )
my $target_file = ( $CONF'COMPRESSACCTS' eq 'no' ? "$user.tar" : "$user.tar.gz" );
foreach my $remote_target (@$all_targets) # If we are going to update multiple targets we send the file multiple times
# previously we would build the backup once for every target. Now we just upload it once
# instead as we know all the targets before we get here now
my %ftp_error_info = ftpsend( $remote_target, $target . '/' . $target_file, $target_file );
$user_error_map$user'transport'$remote_target = \%ftp_error_info if keys %ftp_error_info;
unlink( $target . '/' . $target_file );
return \%user_error_map;
如您所见,这一行正在为归档备份创建 .tar.gz
文件:
cpusystem( $tar_bin, '--use-compress-program=/usr/local/cpanel/bin/gzip-wrapper', '--create', '--preserve-permissions', '--file', "$target/dirs/$rawdir.tar.gz", @EXCLUDES, $dir )
我知道如何使用zip
,gpg
,crypt
,...但我不知道如何在这个函数中使用它并附加它。
您对这项工作有何想法?而且我不知道 perl 编程,所以如果可以的话,请为我提供一个示例代码。
【问题讨论】:
您可以在这个 gzip 包装器周围添加自己的包装器,将生成的 .tar.gz 压缩到 .tar.gz.encrypted 中,然后擦除/擦除 .tar.gz @flaviodesousa 你能提供一个编辑上面代码的代码吗? 【参考方案1】:我知道如何使用 zip、gpg、crypt,...但我不知道如何在这个函数中使用它并附加它。
我认为你不应该修改 Cpanel 软件,而且 tar 不支持密码。所以你应该使用gpg
或crypt
来加密.tar.gz 文件创建后,删除过程中的原始文件
【讨论】:
以上是关于为cpanel的备份文件设置密码的主要内容,如果未能解决你的问题,请参考以下文章
我们需要一个脚本来使用 PHP 中的第三方 Cpanel 详细信息创建所有文件、数据库和电子邮件的备份