I have a bash script for backing up some databases to the location: /media/backup-disk/mysql-backup It runs from cron every 30min and a script contains:
#!/bin/sh -e
/usr/bin/mysqldump --defaults-file=/user/.myf --add-drop-database mydatabasename > /media/backup-disk/mysql-backup/mydatabasename_backup_$(date +%Y-%m-%d-%H.%M.%S).sql
What I would like to do is to run another bash script which will run once a day and the script would find all the .sql files under the backup path which are older then X days, then compress them in a one .tar file with creation date and move them to another disk like /huge-backup-disk/archive/mysql-backups The OS is Ubuntu 24.04 LTS
I have a bash script for backing up some databases to the location: /media/backup-disk/mysql-backup It runs from cron every 30min and a script contains:
#!/bin/sh -e
/usr/bin/mysqldump --defaults-file=/user/.my.cnf --add-drop-database mydatabasename > /media/backup-disk/mysql-backup/mydatabasename_backup_$(date +%Y-%m-%d-%H.%M.%S).sql
What I would like to do is to run another bash script which will run once a day and the script would find all the .sql files under the backup path which are older then X days, then compress them in a one .tar file with creation date and move them to another disk like /huge-backup-disk/archive/mysql-backups The OS is Ubuntu 24.04 LTS
Share Improve this question edited Mar 19 at 11:56 Lajos Arpad 77.4k40 gold badges117 silver badges222 bronze badges asked Mar 19 at 10:47 Marko AmbMarko Amb 112 bronze badges1 Answer
Reset to default 0Let's see the script first and then let's understand it:
find bar/*.sql -type f -mtime +13 -exec mv {} bar/lorem \;
Explanation:
find
will find your files- I'm searching for sql files inside the bar folder. You can change the path as you wish
- The
type
of the things I'm looking for is files, that is, f - With
-mtime
I specify how many days prior to the command that is executed -exec
is a command to be run- the command is a
mv
of{}
(the match) to a destination, which was bar/lorem in my case
Here's a revised version of the script:
mkdir bar/archives &&
find bar/*.sql -type f -mtime +13 -exec mv {} bar/archives \; &&
tar -C bar cf archives.tar archives &&
yes | rm -R bar/archives &&
mv bar/archives.tar bar/lorem
The idea is that I create a new directory, I named it archives
, but you can use another name, perhaps with a timestamp instead of it. I'm moving the files to this archives folder first, create the tar archive in the bar
directory, remove the temporary directory and move the tar archive to the target.
Example (illustration)