shellbackupksh

Shell script to backup dir from list


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"

Solution

  • One 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