Compare file dates before copy

Sometimes when I boot, my system goes into emergency mode.

I then use Clonezilla to restore an image.

Usually the image is older than the current date.

This is part of a backup script that runs as a startup program.

cd /home/andy/bin/
zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/

This then overwrites my zip file with one that is in fact older.

I thought that if I could compare the file dates, before the zip operation, I could prevent the overwriting.

Normally, when we make backups, we put the date of the backup in the name of the file.

So, if you do this using a standard UNIX time, it is very easy to parse than info out of the filename and compare.

Example:

mybackupfile.1533817852.tar.gz

Thanks.

How do I put date into the filename?

Use the utility in your script (depends on the shell or script you are using) and attach it to the filename.

When you are asking technical questions, you will get faster and better answers, and not waste others' time, by providing the details of what you are doing.

operating system? scripting language (bash, PHP, ksh, etc)? and other details.

If we have to reply back and forth many times, just to get basic info, it is a waste of both of our time.

OK. Using bash, Ubuntu Mate 18.04. Ubuntu Mate terminal.

I do not understand "utility in my script."

How to you get the UNIX time in bash?

HINT:

date +%s

Each time I use date +%s, the number increases.

Am I in the ballpark with this ?

file1time=`stat -c %Y /home/andy/bin/Ubuntu_Scripts.zip`

file2time=`stat -c %Y /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Ubuntu_Scripts.zip`

if [ "$file1time" -ot "$file2time" ]
then
 echo "File is older. "
fi     

I would not use stat . I would do it as I said, add the UNIX time to the file name.

If you want to use stat , that's up to you.

Ok. Will work on adding it in a month/day/year format.

If you convert to DMY format, will have have to write more code to instruct a computer to compare dates.

That's why UNIX time is so great.

You can easily compute with it.

On my backups, I use standard date and time format because I don't use the information to have a computer compare the dates / times.

So, I prefer human readable file names.

But if I was comparing the times using a computer, then I use UNIX time.

For example, when you logout of this forum, we set a cookie of your last login time, so when you login again, we know what posts you have not seen, based on time, etc.

This cookie stores the time in UNIX time.

That makes sense.

Use date +%Y-%m-%d format that can be alphabetically compared!

I think this will work.

rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/

rsync is a good way to make backups.

There are 100s of ways to do things on Linux, so always do what you like.

However, when I rsync backups, for example database dump backups, I keep the file names in each file:

-rw-r--r-- 1 root root 409715952 Jun  1 22:30 main_masterdump_2018-06-01-22.sql.gz
-rw-r--r-- 1 root root 409688854 Jun  2 22:30 main_masterdump_2018-06-02-22.sql.gz
-rw-r--r-- 1 root root 409982101 Jun  3 22:30 main_masterdump_2018-06-03-22.sql.gz
-rw-r--r-- 1 root root 409919476 Jun  4 22:30 main_masterdump_2018-06-04-22.sql.gz
-rw-r--r-- 1 root root 409866365 Jun  5 22:30 main_masterdump_2018-06-05-22.sql.gz
-rw-r--r-- 1 root root 409877826 Jun  6 22:30 main_masterdump_2018-06-06-22.sql.gz
-rw-r--r-- 1 root root 409882688 Jun  7 22:30 main_masterdump_2018-06-07-22.sql.gz
-rw-r--r-- 1 root root 410654112 Jun  8 22:30 main_masterdump_2018-06-08-22.sql.gz
-rw-r--r-- 1 root root 411599782 Jun  9 22:30 main_masterdump_2018-06-09-22.sql.gz
-rw-r--r-- 1 root root 412574869 Jun 10 22:30 main_masterdump_2018-06-10-22.sql.gz
-rw-r--r-- 1 root root 414508311 Jun 11 22:30 main_masterdump_2018-06-11-22.sql.gz
-rw-r--r-- 1 root root 410308454 Jun 12 22:30 main_masterdump_2018-06-12-22.sql.gz

You will be a wiser sys admin to include dates in your filenames.

I am a normal user, not a sys admin.

But if my filenames have the date in them, won't I accumulate a lot of files?

I tried this, but it made a ton of files which quickly filled up a directory.

cp --backup=numbered afile /path/to/dest

Yes, because it will not overwrite the old backup.

That is a "good thing" because you don't want to automatically overwrite your backups.

The purpose of having backups is to have a lot of them incase there was a problem.

For example.

Let's say that in two weeks time, you discover a criminal hacked into your system a week ago and did some bad things.

If you only have the latest backup, two weeks from now, you will certainly not have the backup from three weeks ago (from two weeks in the future).

So, if you want to be safe and take care of your users and user data, you must have backups going back ... a long way... depending on how critical your data is.

Then, as a good sys admin, you delete your very old backups (manually is best) when you are confident you don't need them.

That's how it is done.

Otherwise, you will f*ed very much when you discover in a month someone hacked you two weeks ago from today :wink:

Ok. But I will need more help in putting file dates into my file names.

Bash is very featured filled.

Much more so than batch files I used in WIndows.

You need a command substitution, either ` ` (backticks) or $( )
Store it in a variable like this

date=$(date +%Y-%m-%d)

Then insert $date where you need it.

cp Ubuntu_Documents.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Ubuntu_Documents.zip_`date +%d%b%Y_%H_%M`
Ubuntu_Documents.zip_09Aug2018_12_00