I'm working on ultimate backup script for very important server. My script already:

  • creates a backup
  • verifies copied file(-s)
  • verifies gz archive
  • compares MD5 on local and remote after sending file to remote server via scp
  • removes monthly backups older than 365, weekly backups older than 31 days, daily backups older than 7 days, hourly backups older than 24 hours
  • checks for available disk space on local and remote machine
  • sends email report if some errors ocurred
  • sends email report for weekly backups

In addition to all this, I miss one important thing - clock verification on local and remote because it affects the naming and removal of copies.

How can I make sure if date and time are set correctly in bash in a simple and reliable way?

If possible - it would be nice if that method could work on Windows server with cygwin.

I'm not asking for complete solution, just advice for taking correct and not complex approach.


1 Answer 1


You can use command in Linux:

date +%s

and in Windows (PowerShell)

Get-Date -UFormat %s

to get epoch time. Those numbers can be easy compared.

P.S. For Windows you may need to strip the number to integer like this:

$t3 = (Get-Date -UFormat %s).split(',')[0]
echo $t3

N.B. Here comma , is my decimal number delimiter. Depend of your language you may need to change it.

This command will give you Epoch time from external source (to compare with your machines)

curl -s "http://worldtimeapi.org/api/timezone/Europe/Rome" |jq '.unixtime'

Feel free to change your timezone

  • This is good to compare between two machines, but I need something that checks some reference ntp server.
    – Kamil
    Jan 13 at 16:22
  • 1
    @Kamil In such case implement to ALL machines ntp clients Jan 13 at 16:24
  • I have ntp clients on all machines, but I need to be sure that it is working before I start deleting backups older than x days.
    – Kamil
    Jan 13 at 17:13
  • 1
    @Kamil, will update the answer to give you time from World source Jan 13 at 17:22
  • 1
    Nice, pretty portable (available on cygwin, debian and I don't know what else) but it looks like I would have to to install curl on all servers. I think it is worth.
    – Kamil
    Jan 13 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.