Command to validate between current day and 7 day old in bash scriptlinux - linux

I want to validate, the date given as input is 7 day old or more from current day in bash script linux

This command provides the date 7 days previous to now, in seconds (since 1/1/1970):
minus7=$(date -d "7 days ago" "+%s")
This command provides the date in seconds of the user's input date:
tstDate=$(date -d "$yourInputDate" "+%s")
then
let delta=$tstDate-$minus7

Related

Calculating the time until the start of next hour in Bash

Hi I have a bash script called by a systemd service that needs to run until the start of the next hour. Currently I have been using:
currentTime=$(date +"%s")
nextHour=$(date -d "$(date -d 'next hour' '+%H:00:00')" '+%s')
duration=$(((nextHour-currentTime)*1000))
Which works except for trying to calculate the difference between 11pm and midnight were as far as I can tell it gets the current days midnight from 23 hours previous.
Oct 13 23:00:05 host bash[2019]: 1665698405
Oct 13 23:00:05 host bash[2019]: 1665615600
Oct 13 23:00:05 host bash[2019]: -82805000
I figure I could put a conditional check with a different calculation if needed or perhaps look at a systemd timer for triggering the service but as the service needs to always activate on boot/reboot as well as running hour to hour this setup seemed more appropriate.
Would appreciate any advice on why this is happening and advice on most streamlined steps to avoid it.
This isn't really a question about bash, but seems to be more about date. Given that date has multiple different implementations, it seems wiser to choose a different tool to do the calculation. I suspect perl is more standardized and (almost) as available as date, so you might try to get the difference with:
perl -MTime::Seconds -MTime::Piece -E '
my $t = localtime; my $m = ($t + ONE_HOUR)->truncate(to => "hour"); say $m - $t'

Convert Zulu timestamp to seconds since epoch and compare it with current time in bash script Mac

I have below timestamp, which I need to change to seconds since epoch in a bash script, Mac OS and compare with current system time.
2021-09-21T06:27:15Z
Current System Time is in IST format. i.e Tue Sep 21 12:20:42 IST 2021
Please suggest a better way to achieve it.
This should give you what you want:
#!/bin/bash
date="2021-09-21T06:27:15Z"
epoch=$(date -d "${date}" +%s)

Manipulate the system time for a given command

I want to run command with specific date in history.
Let say today is 12.Nov.2019 and I need to run one command with date of 10th. Is there any possibility of setting date on fly ?
date -d '1 day ago' /path/to/command
doesn't work.
Use can you faketime to run a command with given time:
faketime - manipulate the system time for a given command
Because you didn't tell us what command and time you want to run,
I suppose that you want to run /path/to/command on Oct 10th, 2019.
Run /path/to/command in 2 days ago:
faketime -f -2d /path/to/command
Run /path/to/command on Oct 10th, 2019:
faketime -f '2019-11-10' /path/to/command

Crontab not running on sunday night

So I made this so it would send a command at Sunday night at 11:59pm, 1 minute before Monday, but for some reason it doesn't work. I even changed the * * 7 to 0.
59 23 * * 7 screen -S skyblock -p 0 -X stuff "mangdelp default essentials.fly ${printf \\r)"
Your syntax is incorrect.
It should be like this:
59 23 * * 0 screen -S skyblock -p 0 -X stuff \"mangdelp default essentials.fly ${printf \\r)\" >/dev/null 2>&1
minute(s) hour(s) day(s) month(s) weekday(s) command(s)
The fields are
separated by spaces or tabs. The first five are integer patterns and
the sixth is the command to be executed. The following table briefly
describes each of the fields.
Field Value Description
minute 0-59 The exact minute that the command sequence executes
hour 0-23 The hour of the day that the command
sequence executes
day 1-31 The day of the month that the command
sequence executes
month 1-12 The month of the year that the command
sequence executes
weekday 0-6 The day of the week that the command
sequence executes. Sunday=0, Monday = 1, Tuesday = 2, and so forth.
command Special The complete sequence of commands to be executed. The
command string must conform to Bourne shell syntax. Commands,
executables (such as scripts), or combinations are acceptable.

Linux bash backup script - 12, 9, 6, 3, 1 months

I am writing a bash backup script, and it's working excellent so far. The problem is that it clutters up my harddrive in no time.
The backup runs weekly on sundays.
I would like to:
Save the most recent 4 backups
Save the backup which is 3 months old
Save the backup which is 6 months old
Save the backup which is 12
months old
Now how do i achieve this?
I think i can work out how to "check if file exists", but i'm having trouble getting my head around how to delete the correct backups.
The backup 3 months old, will be 3 months and 1 week old by next week - and thus be deleted..
Is there any geniously simple way to work around this that i may have overlooked..?
Thanks in advance,
If you give your Backup files a nice naming scheme like: 10.29.15-BACKUP.zip you could always do it real easily. Easiest where you can just have 2 separate folders one for Daily Backups and one for Archives.
So in your bash script:
#BACKUP PROCESS HAPPENS HERE, PLACES BACKUP NAMED 10.29.15-BACKUP.zip in /home/User/DailyBackups FOLDER, WHICH WE WILL CALL $CurrentBackup
#Get Date from 3 months ago
ChkDate=`date --date="-3 months" +%m.%d.%y`
#See if this file exists
ls $ChkDate-BACKUP.zip /home/User/BackupArchive/
#If it does exist then copy current backup to BackupArchive Folder and Remove any backups older than 367 days from the BackupArchive Folder
if [[ $? == 0 ]]; then
cp /home/User/DailyBackups/$CurrentBackup /home/User/BackupArchive/$CurrentBackup
find /home/User/BackupArchive/*-BACKUP.zip -mtime +367 -exec rm {} \
fi
#Remove all but the most recent 4 Backups
for i in `ls -t /home/User/DailyBackups/*-BACKUP.zip | tail -n +5`; do
rm "$i"
done
I used 367 to account for a 366 day leap year and just in case your one year backup was a bit off, like 366 days and 1 minute.
I had a similar task to delete files up to n date what i had to do was:
1. generate an interval date from todays date (like 3 months ago)
[this post has a good writeup about getting specific dates
http://stackoverflow.com/questions/11144408/convert-string-to-date-in-bash]
2. loop over all the files in the location and get their time\date stamp with
date -r <filename> +%Y
date -r <filename> +%m
date -r <filename> +%d
3. Compare file date to interval date from todays date and keep if it matches or delete if not.
Hope this helps you to get the concept going
Suppose you named the backup according to the date:
% date +%Y-%m-%d
2015-10-29
Then you can compute the date one year ago like this:
% date +%Y-%m-%d -d "-1 year"
2014-10-29
and the date 5 weeks ago like this:
% date +%Y-%m-%d -d "-5 weeks"
2015-09-24
So you can setup cronjobs with run every 3 months and every Sunday,
and delete backups that occurred one year ago and 5 weeks ago like this:
# Every 3 months, run the backup script
1 2 * */3 * /path/to/backup/script.sh > /path/to/backupdir/$(date +%Y-%m-%d-Q)
# Every 3 months, delete the backup made on that date the previous year
1 2 * */3 * /bin/rm /path/to/backupdir/$(date +%Y-%m-%d-Q -d "-1 year")
# Every Sunday, if backup does not exist, run backup script
1 3 * * 7 if [ ! -f /path/to/backupdir/$(date +%Y-%m-%d-Q) ]; then /path/to/backup/script.sh > /path/to/backupdir/$(date +%Y-%m-%d) fi
# Every Sunday, delete backup 5 weeks old
1 3 * * 7 /bin/rm /path/to/backupdir/$(date +%Y-%m-%d -d "-5 weeks")
Note that
We want to be careful not to run the backup script twice on the same day, for
example, when a quarterly backup happens on a Sunday. If the quarterly backup
cronjob is set to run (at 2:00AM), before the weekly backups (at 3:00AM), then
we can prevent the weekly backup from running by testing if the backup filename
already exists. This is the purpose of using
[ ! -f /path/to/backupdir/$(date +%Y-%m-%d-Q) ]
When we delete backups which are 5 weeks old, we do not want to delete quarterly backups.
We can prevent that from happening by naming the quarterly backups a little differently than the weekly backups, such as with a Q:
% date +%Y-%m-%d-Q
2015-10-29-Q
so that the command to remove weekly backups,
/bin/rm /path/to/backupdir/$(date +%Y-%m-%d -d "-5 weeks")
will not remove quarterly backups.

Resources