Score:0

How can I verify if time is set correctly on server in my bash script?

it flag

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.

stark avatar
mu flag
See https://serverfault.com/questions/1077601/how-to-check-whether-the-system-time-is-synchronised-to-ntp-server-without-knowi
Kamil avatar
it flag
@stark thats pretty complicated, I need some kind of bash one-two liner :)
Score:1
in flag

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

Kamil avatar
it flag
This is good to compare between two machines, but I need something that checks some reference ntp server.
Romeo Ninov avatar
in flag
@Kamil In such case implement to ALL machines ntp clients
Kamil avatar
it flag
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.
Romeo Ninov avatar
in flag
@Kamil, will update the answer to give you time from World source
Kamil avatar
it flag
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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.