I have a .tar file that is transferred to our server every night by one of our client management systems. The file is a compressed .sql file which I need to uncompress and dump into our database.
The directory of the .tar file is /root/backups/premvet_sync/2012.07.17.10.09.19
. As you can see the directory is a timestamp of when the file was uploaded to our server.
I am trying to create a cron job that CD's into the directory and then uncompresses it. The command I would usually use is something like this:
cd /root/backups/premvet_sync/2012.07.17.10.09.19 && tar -xvf backupfile.tar
Obviously that command would only run once (when the timestamp matches the cron job path). Is there anything I can do to CD into the newest directory?
The server is running Ubuntu 10.04 LTS if that makes any difference.
Update
For anyone who is interested, here is the final (working) bash script.
#!/bin/sh
# Untar backup database and dump into local mysql server.
echo "Finding latest backup directory"
dir=$(ls -td1 /root/backups/backup/* | head -1)
echo "Opening latest backup directory"
cd $dir
echo "Uncompress backup
tar -xvf backup.tar
echo "Dig down into correct directory"
cd $dir/backup/databases/MySQL
echo "mysqldump the compressed database file"
gunzip < backup.sql.gz | mysql -u USERNAME -pPASSWORD restore_db_name
echo "Done."
$dir/backup/databases/MySQL
exists before running the tar command. If it does then the sending side didn't work ...