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

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

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest