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"`...
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 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)