我正在尝试构建一个非常灵活且实用的服务器备份 Bash 脚本。我几乎没有使用或构建 Bash 脚本的经验。
我知道世界上有成千上万个这样的生物,但我想要一个更加定制化、能够尽可能具备这些功能的生物。
所以我的主要问题是,阅读我想要的功能列表...你能告诉我这些功能是否不可能实现吗?
感谢任何帮助,我正在尝试学习这一点,因此会进行大量的尝试和错误。我从事 Web 开发已有 15 年,但还没有使用 Bash 脚本的经验……目前还没有!感谢任何帮助!
将适用于 Ubuntu。
备份功能:
- Backup defined Folder of files (folder/filepath set to a variable $FOLDER_NAME_PATH)
- compress files/folder into a .tar archive
- Backup MySQL Database
- compress MYSQL backup file into a .tar archive
- Write all backups to a Log file with the Date and Filepaths of backed up files (
1/12/2015 - MySQL Database $DATABASE_NAME backed up.
1/12/2015 - File/Folder $FOLDER_NAME_PATH backed up to 1-12-2015-files.rar.gz
)
- Rotate backup files, deleting files older than 8 days
- Upload the Files and Database Backup archives to a folder OR remote server based on a setting (
// You can upload to any of these including more then 1 based on setting mentioned below
- Copy Backup files to another Folder on same server
- Upload Backup files to remote server using SCP
- Upload Backup files to remote server using SFTP
- Upload Backup files to remote server using RSYNC
)
- Email Notification Date & File path's of all backed up files sent to a list of Email Addresses(check for mail OR sendmail)
- Email list of Email addresses on Error
- Make all the above features optional on/off with switch variables! (
- ON/OFF Backup File/Folder
- ON/OFF Backup MySQL Database
- ON/OFF Rotating files. Deleting older backup files can be turned on or off
- ON/OFF Write all backup jobs to a Log File
- ON/OFF Copy backed up files to another Local Folder on same server
- ON/OFF Upload backup files to SCP Server
- ON/OFF Upload backup files to SFTP Server
- ON/OFF Upload backup files to RSYNC Server
- ON/OFF Email Notifications for successful backup
- ON/OFF Email Notifications for Errors
)
可能的特征 - 不确定是否可能??
- If Rsync backup is enabled, check to make sure it is installed.
If it is not then Install it and then run it?
- If Email notifications are ON. Then check that MAIL is working
(it's not on my server!) Then if it is NOT working then check for
SENDMAIL (SENDMAIL does work on my server!). Attempt to find one or the other that might be working? Possible?
- If SFTP backup is ON. Check that it is installed.
- If SCP is ON. Check that it is installed.
- When it checks for an installed program and it;s not found or working,
is it possible to automatically install the program and then re-run that code?
答案1
一切皆有可能。但是 - 仅使用 bash 是不行的 - 仅使用 bash 发送电子邮件,不使用 sendmail 并不是一件容易的事 ;) 发送文件也是一样。一切都需要外部程序 - bash 只能控制它们。
您必须记住,在不同的发行版中可以安装不同的工具 - 例如 mail 命令有时可以向邮件添加附件,有时则不能。安装软件包也是一样 - centos/redhat 上的安装与 debian/ubuntu 上的安装不同。如果您仅为特定版本的 ubuntu 创建此工具 - 这个问题并不重要。
在 bash 中处理不同的发行版、系统变化是可能的,但并非易事。而且 bash 很慢,所以做复杂的事情会很慢。有时最好创建一个脚本,检查环境并依赖它安装所需的软件包,并为主程序设置正确的变量/函数。