简体   繁体   English

Bash脚本备份服务器上载文件夹

[英]Bash script to backup server upload folders

I have a CentOS server running WHM/cPanel sites, and a lot of these sites run WordPress. 我有一个运行WHM / cPanel站点的CentOS服务器,很多这些站点运行WordPress。 I would like to do an automated backup that zips and moves any account containing an 'uploads' folder into a backup directory. 我想进行自动备份,将包含“uploads”文件夹的任何帐户拉链并移动到备份目录中。 I know there are other solutions, but most want you to backup the entire WordPress site, I only need to backup the uploads however. 我知道还有其他解决方案,但大多数都希望您备份整个WordPress网站,但我只需要备份上传。

I'm not very good with .sh scripts and have spent a lot of time already trying to figure this out, but I can't seem to find any examples similar enough for me to be successful. 我对.sh脚本不是很了解并且已经花了很多时间试图解决这个问题,但我似乎找不到任何类似的例子来让我成功。 The main problem I have is naming the zip after the account. 我遇到的主要问题是在帐户后命名zip。

Location of most upload folders (user1 being the account that changes): 大多数上传文件夹的位置(user1是更改的帐户):

/home/user1/public_html/wp-content/uploads /home/user2/public_html/wp-content/uploads /home/user3/public_html/wp-content/uploads

Script example: 脚本示例:

find /home/ -type d -name uploads -exec sh -c 'zip -r /backup/uploads/alluploads.zip `dirname $0`/`basename $0`' {} \\;

The problem with this approach is that it all writes to one single zip file. 这种方法的问题在于它都写入一个zip文件。 How can I alter this to save each users uploads to there own user1-uploads.zip file? 如何更改此设置以将每个用户上传到自己的user1-uploads.z​​ip文件?

I've played around with exec and sed but I can't seem to figure it out. 我玩过exec和sed,但我似乎无法弄明白。 This is the best I got - just trying to get it to echo the name - but it's not right. 这是我得到的最好的 - 只是想让它回应这个名字 - 但这不对。 Sorry, I'm terrible with regex: 对不起,我对正则表达式很糟糕:

find /home/ -type d -name uploads -exec sh -c 'string=`dirname $0` echo $string | sed `/\\/home\\/\\(.*\\)\\/new\\/wp-content\\/uploads/`'{} \\;

Would appreciate any help or directions on how to fix this. 非常感谢有关如何解决此问题的任何帮助或指示。 Thanks! 谢谢!

You can use Bash's globbing with * to expand all the different user directories. 你可以使用Bash的globbing with *来扩展所有不同的用户目录。

for uploads in /home/*/public_html/wp-content/uploads; do
    IFS=/ read -r _ _ user _ <<<"$uploads"
    zip -r /backup/uploads/${user}.zip "$uploads"
done

A couple of solutions come to mind, you could loop through the user directories: 想到几个解决方案,您可以遍历用户目录:

cd /home
for udir in *; do
   find /home/$udir -type d -name uploads -exec sh -c 'zip -r /backup/uploads/'"$udir"'-uploads.zip `dirname $0`/`basename $0`' {} \;
done

or use cut to get the second element of the path: 或使用cut来获取路径的第二个元素:

find /home/ -type d -name uploads -exec sh -c 'zip -r /backup/uploads/`echo "$0" | cut -d/ -f 2`-uploads.zip `dirname $0`/`basename $0`' {} \;

Although both of these would run into issues if the user has more than 1 directory anywhere under their home that is named uploads 虽然如果用户在其主页下面的任何位置有多个名为uploads目录,这两个都会遇到问题

You might use tr to simply replace the path separator with another character so you end up with a separate zip for each uploads directory: 您可以使用tr简单地用另一个字符替换路径分隔符,这样您最终会为每个uploads目录添加一个单独的zip:

find /home/ -type d -name uploads -exec sh -c 'zip -r /backup/uploads/`echo $0 | tr "/" "-"`.zip `dirname $0`/`basename $0`' {} \; 

so you would end up wil files named /backup/uploads/home-user-wp-content-uploads.zip and /backup/uploads/home-user-wip-uploads.zip instead of one zip overwriting the other 所以你最终将wil文件名为/backup/uploads/home-user-wp-content-uploads.zip/backup/uploads/home-user-wip-uploads.zip而不是一个zip覆盖另一个

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

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