Shell script to read filename and pick most recent date by timestamp

I currently backup a MySQL docker container using a shell script, it dumps the mysql file into a relative directory, such as..

set-e
TODAY=`date +"%d%b%Y"`
mysqldump -uroot -p"$MYSQL_ROOT_PASSWORD" -h $MYSQL_HOST --all-databases | gzip > /data/mysqldb/$TODAY/all-databases-$TODAY.sql.gz

I need to create a restore script that will read the $TODAY and determine which is most recent. How do I do this? :S

1

1 Answer

This is the approach i used. I have a working backup script with it.

CD into the backup DIR.

cd backup_dir && ls -t | head -n 2

-t -> sort by modification time, newest first

In a script:

LAST_2_BACKUPS=$(ls -t | head -n 2)
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like