sckzw
8/18/2014 - 4:04 PM

Backup shell script

Backup shell script

#! /bin/csh

set host_name     = route
set user_name     = shuichi
set mac_addr      = 00:00:00:00:00:00
set wait_time     = 2m
set shutdown_flag = 0

echo "Starting backup. `date`"

ping $host_name -n 1 >> /dev/null

if ( $status != 0 ) then
    echo "Wake up $host_name ($mac_addr)."
    ./wol.py $mac_addr

    echo "Waiting for $wait_time."
    sleep $wait_time

    ping $host_name -n 1 >> /dev/null

    if ( $status == 0 ) then
        set shutdown_flag = 1
    else
        echo "Failed to wake up $host_name."
        exit(1)
    endif
endif

set src_list = "/cygdrive/d/backup /cygdrive/d/Users/shuichi/Music /cygdrive/d/Users/shuichi/Videos /cygdrive/d/Users/shuichi/Documents /cygdrive/d/Users/shuichi/Pictures /cygdrive/d/Users/shuichi/Downloads"

set dst_dir  = "/backup/nightly"
set old_dir  = `ssh $user_name@$host_name ls $dst_dir | tail -n 1`
set new_dir  = `date +%Y%m%d_%H%M%S`

echo "New backup directory is $dst_dir/$new_dir."

if ( $old_dir == "" ) then
    echo "Old backup directory is not found."
    set link_arg = ""
else
    echo "Old backup directory is $dst_dir/$old_dir."
    set link_arg = "--link-dest=$dst_dir/$old_dir"
endif

echo "Stating rsync."
rsync -av --chmod=744 $link_arg $src_list ${user_name}@${host_name}:$dst_dir/$new_dir

if ( $status != 0 ) then
    echo "Failed to rsync. Status is $status."
endif

if ( $shutdown_flag == 1 ) then
    echo "Shutdown $host_name."
    ssh ${user_name}@${host_name} shutdown -h now

    echo "Waiting for $wait_time."
    sleep $wait_time
endif

echo "Ending backup. `date`"