public
Created

example data dump with retry on failure

  • Download Gist
backup.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/bin/bash
 
while true ; do
 
# there is no way to distinguish node's (1) exit code on SIGINT (and SIGTERM)
# from a (1) exit code due to normal exit call in userland.
# if node preserved SIGINT/SIGTERM, then the shell would detect node's
# "abnormal exit" status, and the script would terminate immediately after
# node returns
# as it stands, it will be very difficult to terminate this script
# using Ctrl-C (SIGINT).
 
if node dump-data.js > backup.json ; then
break
fi
echo -e "\nbackup failed. retrying..."
done
echo "backup complete. $(date)"
 
echo "transferring to permanent storage..."
scp "backup.json" "somehost.example.com:~/backup-$(date +%Y%m%d).json"
echo "transfer complete. $(date)"
dump-data.js
JavaScript
1 2 3 4 5 6 7
process.stderr.write("simulating backup action (sleep for 20 seconds). "+
"Press Ctrl-C.. see what happens.");
 
setTimeout(function() {
process.stderr.write('ok - success!\n');
process.exit(0);
}, 20000);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.