简体   繁体   English

备份bash脚本和tar排除日志和目录

[英]Backup bash script and tar exclude logs and directory

Following script takes the backup of all websites which are in directory /var/www/websites and create .tar.gz for each individual website at /var/backup and works fine 以下脚本对目录/ var / www / websites中的所有网站进行备份,并在/ var / backup中为每个单独的网站创建.tar.gz,并且工作正常

WEBROOT="/var/www/websites"
BAKPATH="/var/backup"

for vhost in "$WEBROOT"/*; do
  test -d "$vhost" || continue
  base=$(basename "$vhost")
  ( cd "$WEBROOT" && tar -cpzf "$BAKPATH/$(date +%F)_$base.tar.gz" "$base" )
done

But, I need to exclude logs folder and one website from backup, like we normally exclude with tar as 但是,我需要从备份中排除日志文件夹和一个网站,就像我们通常用tar排除一样

--exclude=$BAKPATH/*/logs or --exclude=$BAKPATH/website2.com --exclude = $ BAKPATH / * / logs或--exclude = $ BAKPATH / website2.com

I am not sure where to put that in? 我不确定该放在哪里? I tried following but it does not work 我尝试了以下操作,但不起作用

WEBROOT="/var/www/websites"
BAKPATH="/var/backup"

for vhost in "$WEBROOT"/*; do
  test -d "$vhost" || continue
  base=$(basename "$vhost")
  ( cd "$WEBROOT" && tar -cpzf "$BAKPATH/$(date +%F)_$base.tar.gz --exclude=$BAKPATH/*/logs --exclude=$BAKPATH/website2.com" "$base")
done

Where should I put 我应该放在哪里

--exclude=$BAKPATH/*/logs --exclude=$BAKPATH/website2.com --exclude = $ BAKPATH / * / logs --exclude = $ BAKPATH / website2.com

so it take affect? 这样影响吗?

$BAKPATH is the place where the target of the backup process (the archive, the *.tgz file) is supposed to be created. $BAKPATH是应该在其中创建备份过程目标 (档案,*。tgz文件)的地方。 If you want to exclude something from the source , you have to specify the paths in the source : 如果你想排除从源头上的东西,你必须指定的路径:

tar -cpz --exclude "$base/logs" \
    -f "$BAKPATH/$(date +%F)_$base.tar.gz" "$base"

This will exclude all files which match the glob pattern $base/logs (and everything below). 这将排除所有与glob模式$base/logs匹配的文件(以及下面的所有内容)。

According to your question, I assume that you want to exclude eg a file named /var/www/websites/cardboardwebshop/logs/access.log . 根据您的问题,我假设您要排除例如名为/var/www/websites/cardboardwebshop/logs/access.log的文件。 If this is not correct, please comment, maybe we can figure out what exactly you need to write to exclude what you want to exclude. 如果这是不正确的,请发表评论,也许我们可以弄清楚您到底要写什么才能排除您要排除的内容。

After getting some feedback from different sources, I was able to achieve this by adding following lines with if condition 从不同来源获得一些反馈后,我可以通过添加以下行以及if条件来实现此目的

##ADD THIS
  if [[ $vhost = *"website1.com"* || $vhost = *"website2.co.uk"* ]]; then
  continue
  fi
##

So, finally the script looks like this 所以,最终脚本看起来像这样

WEBROOT="/var/www/websites"
BAKPATH="/var/backup"

for vhost in "$WEBROOT"/*; do
  test -d "$vhost" || continue
  if [[ $vhost = *"website1.com"* || $vhost = *"website2.co.uk"* ]]; then
  continue
  fi
  base=$(basename "$vhost")
  ( cd "$WEBROOT" && tar -cpzf "$BAKPATH/$(date +%F)_$base.tar.gz" "$base" )
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM