Bash multiple if语句在不同的日子运行不同的脚本变体 [英] Bash multiple if statements to run different variants of script on different days
问题描述
我正在尝试编写一个bash脚本来使用rsync运行每日增量备份,但备份会进入星期一,星期二,星期三文件夹。
I'm trying to write a bash script to run a daily incremental backup using rsync but the back up goes into a Monday, Tuesday, Wednesday folder.
因此,我不想在cron中运行6个脚本,而是希望使用ifs的1个脚本确定它是否是星期一运行此命令。但如果它的星期三运行这个,如果它不是周一至周五退出。
So instead of having 6 scripts running in cron I would like 1 script using ifs to determine if it's a monday run this command. but if its Wednesday run this, and if it's not Mon-Fri exit.
我是bash的新手所以我可以看看是否可以做到这一点但是我我不确定如何设置它。
I am new to bash so I can see if being a way to maybe do this but I'm not fully sure as to how to set it up.
以下是我到目前为止的情况。此刻非常粗糙。一旦ifs被排序,我将开始清理剩余的代码。
Below is what I have so far. very rough at the moment. once the ifs are sorted I will start cleaning the rest of the code up.
#!/bin/bash
if [[ $(date +%u) -e 1 ]]
then
rsync -avz --delete --exclude backup --exclude virtual_machines /home /home/backup/files/backupdaily/monday
else
if [[ $(date +%u) -e 2 ]]
then
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/tuesday
else
if [[ $(date +%u) -e 3 ]]
then
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/wednesday
elselse
if [[ $(date +%u) -e 4 ]]
then
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/thursday
elselse
if [[ $(date +%u) -e 5 ]]
then
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/friday
else
if [[ $(date +%u) -e 6 ]]
then
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/saturday
else
exit
fi
稍微疑惑这个脚本在compileonline.com上测试时有效
Slightly puzzled this script works when test on compileonline.com
$bash -f main.sh
sending incremental file list rsync: opendir "/home/beach" failed: Permission denied (13) rsync: opendir "/home/com" failed: Permission denied (13)
rsync: opendir "/home/railo" failed: Permission denied (13)
rsync: opendir "/home/webmaster" failed: Permission denied (13)
rsync: mkdir "/home/backup/files/backupdaily/Thursday" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(576) [receiver=3.0.6]
rsync: connection unexpectedly closed (9 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(600)
但如果我尝试运行它我会得到:
but if i try to run it i get:
[root @ san-prod-01 backup ] #bash ./backup_daily_v3.sh
:命令not foundh:第4行:'/ backup_daily_v3.sh
:第5行:'/backup_daily_v3.sh
:行中意外令牌附近的语法错误5:case $ WeekDay in -
[root@san-prod-01 backup]# bash ./backup_daily_v3.sh : command not foundh: line 4: '/backup_daily_v3.sh : line 5: syntax error near unexpected token in '/backup_daily_v3.sh : line 5: case $WeekDay in –
任何建议?
推荐答案
我建议让cron启动这个脚本。另外,在这种情况下(双关语?)你最好使用案例。
I would suggest having cron start this script. Additionally, in this case (pun?) you will be better off using case.
#!/bin/bash
case $(date +%u) in
1) # Monday
rsync -avz --delete --exclude backup --exclude virtual_machines /home /home/backup/files/backupdaily/monday
;;
2) # Tuesday
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/tuesday
;;
3) # Wednesday
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/wednesday
;;
4) # Thursday
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/thursday
;;
5) # Friday
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/friday
;;
6) # Saterday
rsync -avz --exclude backup --exclude virtual_machines --link-dest=/home/backup/files/backupdaily/monday /home /home/backup/files/backupdaily/saturday
;;
7) # Sunday
exit 0
;;
esac
[更新]更新脚本以使用工作日的小写字母。[/ Update ]因为有些日子运行大致相同的命令,您可以将脚本缩短为:
[Update] updated script to use lower case letters for weekdays.[/Update] Because some days run roughly the same command, you could shorten your script to:
#!/bin/bash
LinkDest=/home/backup/files/backupdaily/monday
WeekDay=$(date +%A |tr [A-Z] [a-z])
case $WeekDay in
monday) rsync -avz --delete --exclude backup --exclude virtual_machines /home /home/backup/files/backupdaily/monday
;;
tuesday|wednesday|thursday|friday|saturday) rsync -avz --exclude backup --exclude virtual_machines --link-dest=$LinkDest /home /home/backup/files/backupdaily/$WeekDay
;;
sunday) exit 0
;;
esac
这篇关于Bash multiple if语句在不同的日子运行不同的脚本变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!