简体   繁体   English

如何编写 bash 脚本来获取备份

[英]how can write bash script to get backup

I want to write a bash script to backup my directories every day我想写一个 bash 脚本来每天备份我的目录

this is my script这是我的脚本

$ sudo tar -cvpzf /home/backup.tar.gz /home/data

how can I automate this我怎样才能自动化这个

As @Fravadona suggested, your best bet would be to use a cron job.正如@Fravadona 建议的那样,您最好的选择是使用 cron 作业。 For such a short and self-explanatory command, I wouldn't even use a script per se.对于这样一个简短且不言自明的命令,我什至不会使用脚本本身。

You can add a new job just typing in your terminal sudo crontab -e for edit (since you want to run a command with sudo, this opens root 's crontab).您可以添加一个新作业,只需在终端中输入sudo crontab -e进行编辑(因为您想使用 sudo 运行命令,这会打开root的 crontab)。 To better understand how the frequency is configured, you can use for example crontab.guru .为了更好地了解频率的配置方式,您可以使用例如crontab.guru

Then, you can add the new entry using your preferred text editor.然后,您可以使用首选的文本编辑器添加新条目。 As suggested in the comments, you could add a timestamp to the tar file name to keep track of it.正如评论中所建议的,您可以在 tar 文件名中添加时间戳以跟踪它。

For example, a backup that runs every day at 08:00:例如,每天 08:00 运行的备份:

0 8 * * * /usr/bin/sudo /usr/bin/tar -cvpzf /home/backup_`date +%s`.tar.gz /home/data

Note : The date +%s command returns a unix timestamp.注意date +%s命令返回 unix 时间戳。

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

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