admin管理员组文章数量:1333406
I have a backup script which does the job but takes a long time to complete because it tars Users dir against a list of servers in DEV_Servers.txt one by one.
Is there a way the tar command could run for the list of servers all at the same time instead of going through the list one by one?
#!/bin/ksh
print "==========================================="
print "Taking backup of Users dir"
print "==========================================="
serverList="DEV_Servers.txt"
while read -r server;
do
print
print "==========================================="
print "Taking Backup of Users:" $server
ssh -o StrictHostKeyChecking=no -n $server "cd /opt/test/ ; tar -zcvf 'Users$(date '+%Y%m%d').tar.gz' Users"
done < "$serverList"
I have a backup script which does the job but takes a long time to complete because it tars Users dir against a list of servers in DEV_Servers.txt one by one.
Is there a way the tar command could run for the list of servers all at the same time instead of going through the list one by one?
#!/bin/ksh
print "==========================================="
print "Taking backup of Users dir"
print "==========================================="
serverList="DEV_Servers.txt"
while read -r server;
do
print
print "==========================================="
print "Taking Backup of Users:" $server
ssh -o StrictHostKeyChecking=no -n $server "cd /opt/test/ ; tar -zcvf 'Users$(date '+%Y%m%d').tar.gz' Users"
done < "$serverList"
Share
Improve this question
edited Nov 23, 2024 at 16:20
Incursio_02
1101 silver badge7 bronze badges
asked Nov 20, 2024 at 20:09
Engineer83Engineer83
1297 bronze badges
4
|
2 Answers
Reset to default 1One way to run the tar commands in parallel is by putting each SSH call in the background. You can do this by appending an & at the end of each SSH command. Here's how you can modify your script:
#!/bin/ksh
print "==========================================="
print "Taking backup of Users dir"
print "==========================================="
serverList="DEV_Servers.txt"
while read -r server;
do
print
print "==========================================="
print "Taking Backup of Users:" $server
# Run tar command in the background
ssh -o StrictHostKeyChecking=no -n $server "cd /opt/test/ ; tar -zcvf 'Users$(date '+%Y%m%d').tar.gz' Users" &
done < "$serverList"
# Wait for all background jobs to finish
wait
- The
&
at the end of thessh
command tells the shell to run the command in the background. - The
wait
command ensures that the script waits for all background jobs to finish before it exits.
Untested:
#!/bin/ksh
print "==========================================="
print "Taking backup of Users dir"
print "==========================================="
serverList="DEV_Servers.txt"
do_one() {
server="$1"
print
print "==========================================="
print "Taking Backup of Users:" $server
ssh -o StrictHostKeyChecking=no -n "$server" "cd /opt/test/ ; tar -zcvf 'Users$(date '+%Y%m%d').tar.gz' Users"
}
export -f do_one
parallel -j0 do_one < "$serverList"
It requires that ssh server
does not ask for a passphrase. If you have set up ssh-agent
this should be fine.
本文标签: Shell script to backup dir from listStack Overflow
版权声明:本文标题:Shell script to backup dir from list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742331969a2454874.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ssh
command in the background with&
so they all run in parallel. Or use GNU Parallel. – Barmar Commented Nov 20, 2024 at 20:39/bin/ksh
? Why is your question tagged withbash
? – Cyrus Commented Nov 20, 2024 at 20:39$(date '+%Y%m%d')
is going to return the same result on each pass through the loop (in a worse case scenario you could generate a 2nd/different result when passing midnight); consider runningdate '+%Y%m%d'
once earlier in the script, store in a variable and then reference the variable in thessh
call; if usingbash
orksh
you can make use of theprintf / -v
capability, eg:printf -v dt "%(%Y%m%d)T"
- the variabledt
will now contain the current date (eg,20241120
) – markp-fuso Commented Nov 20, 2024 at 20:58