We were taking mysql backups using a very simple shell script which was using mysqldump as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/bin/sh #echo "Starting the script"; HOST=localhost USER=myDBUser PASSWD='myPassword'; # create directory if it doesn't exist OLDBACKUP=/home/my_backup/sql_files if [ ! -d "$OLDBACKUP" ]; then # create direcotry mkdir /home/my_backup/sql_files fi # set file names by appending date to each file dbName=dbName-`date -I`.sql; # take db backups mysqldump -u $USER -p$PASSWD -n -c myDbName >Â $dbName; # move the file to sql_files directory mv $dbName $OLDBACKUP |
We were keeping old backup files but we knew that we would need to remove the backup files older than two weeks or a month. I was thinking what to do and after some studying I found the following one line, would help us remove files older than x number of days.
1 |
find /home/my_backup/sql_files -type f -mtime +15 | xargs rm |
Where 15 could be changed to any number of days and it will remove all the files older than that number of days recursively, using this command. This command won’t delete any special files or sub directories.