Linux Shell 脚本:备份和恢复文件,同时维护目录位置

发布于 2024-11-08 18:07:35 字数 470 浏览 0 评论 0原文

我正在寻找编写一个 Linux shell 脚本,以便对我的项目文件夹中的所有源代码执行备份和恢复(.c、.cpp、.java 等)

我的脚本删除了所有这些源代码文件,我希望能够在需要时轻松恢复这些文件。

这是我现在正在做的事情:

#copy the entire directory to a backup folder
cp -r $CLEANUP_PATH $BACKUP_PATH

#delete the project and copy from backup to restore source code
rm -rf $CLEANUP_PATH
cp -r $BACKUP_PATH $CLEANUP_PATH

该项目大约有 600MB,因此我执行这些操作需要很长时间。

如何从备份中仅恢复源代码(而不是整个目录)并确保每个文件都恢复到每个文件所属的特定目录?

I am looking to write a Linux shell script in order to perform a backup and restore of all of the source code in my project folder (.c, .cpp, .java, etc)

My script deletes all of these source code files and I want to be able to easily restore the files as well if need be.

Here is what I am doing now:

#copy the entire directory to a backup folder
cp -r $CLEANUP_PATH $BACKUP_PATH

#delete the project and copy from backup to restore source code
rm -rf $CLEANUP_PATH
cp -r $BACKUP_PATH $CLEANUP_PATH

The project is around 600MB, so it takes a long time when I perform these actions.

How would I be able to restore only the source code (rather than the entire directory) from a backup and ensure that each file is restored to the specific directories that each file belongs in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-11-15 18:07:35

考虑使用 tar:它会记住有关文件的所有信息,包括路径、权限和日期。如果源代码位于几个目录中,则可以按名称从 tar 文件中提取这些目录。

Consider using tar: it remembers everything about the files, including paths, permissions, and dates. If you source code is inside a few directories, you can extract those directiories by name from the tar file.

奈何桥上唱咆哮 2024-11-15 18:07:35

考虑使用版本控制。它会记住有关文件的所有信息,包括路径、权限、日期和以前的修订版本。这不仅会阻止灾难恢复,还会使您成为更好的开发人员(出于多种原因)。

git init .
git add `find . -type f | egrep '\.(cpp|c|java)

当然,假设您在目录中有不想提交/保存的内容。如果您想保存所有内容(请不要保存二进制文件/库/其他生成或可生成的文件!),那么您可以 git add 。

完成一些工作后,签入更改

git status
git commit -a -m "Descriptive comment about changes"

您还可以克隆git 存储库,以防您删除该目录。系统外克隆,以防您删除计算机。

git clone ssh://hostname/path/to/repository
` git commit -m "initial version"

当然,假设您在目录中有不想提交/保存的内容。如果您想保存所有内容(请不要保存二进制文件/库/其他生成或可生成的文件!),那么您可以 git add 。

完成一些工作后,签入更改

您还可以克隆git 存储库,以防您删除该目录。系统外克隆,以防您删除计算机。

Consider using version control. It remembers everything about the files, including paths, permissions, dates, and previous revisions. This will not only prevent disaster recovery, but it will make you a better developer (for any number of reasons).

git init .
git add `find . -type f | egrep '\.(cpp|c|java)

The of course assumes you have stuff in the directory you do not want committed/saved. If you want to save everything (no binaries/library/other generated or generatable files please!) then you can git add .

After you do some work, check in the changes

git status
git commit -a -m "Descriptive comment about changes"

You can also make clones of the git repository in case you delete the directory. Offsystem clones in case you delete the machine.

git clone ssh://hostname/path/to/repository
` git commit -m "initial version"

The of course assumes you have stuff in the directory you do not want committed/saved. If you want to save everything (no binaries/library/other generated or generatable files please!) then you can git add .

After you do some work, check in the changes

You can also make clones of the git repository in case you delete the directory. Offsystem clones in case you delete the machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文