Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # The database name is had from the filename, so ensure the file is named
- # backup_DATABASENAME.sh or this will crash. Set the other relevant vars here.
- # Definitely need to change these:
- USER='USER_NAME'
- PASSWORD='PASSWORD'
- # Might need to change these:
- HOST=localhost
- FOLDER='backups/'
- #!/bin/sh
- ########################### Do not edit below this line #######################
- TARGET=`expr match "$0" '\(.*\)bash/backup_.*'`
- TARGET=$TARGET$FOLDER
- DB=`expr match "$0" '.*backup_\(.*\).sh'`
- # Simpler to just use seconds everywhere
- NOW=`date +"%s"`
- mysqldump -h $HOST -u $USER -p$PASSWORD $DB > $TARGET$DB'_'$NOW.sql
- bzip2 -f $TARGET$DB'_'$NOW.sql
- echo Backed up database $DB on $HOST to $TARGET$DB'_'$NOW.sql.bz2
- days_diff()
- # Echos difference in days between exisiting files and arg1 an epoch timestamp.
- {
- ref_time=$1
- FILE_2=`expr match "$2" '.*_\(.*\)\.sql.*'`
- INTER=$(($ref_time-$FILE_2))
- echo $(($INTER/86400))
- }
- divisible_by_six() # The frequency with which we'll delete files.
- {
- REMAINDER=$(( $1 % 6 ))
- if [ $REMAINDER -eq 0 ]
- then
- echo "1"
- else
- echo "0"
- fi
- }
- process_files() # Removes every sixth file that's more than a week old.
- {
- COUNTER=0
- find $1 -maxdepth 1 -type f | while read -r dir
- do
- VAL=$(basename ${dir})
- DIFF=`days_diff $2 $VAL`
- if [ "$DIFF" -gt 6 ]
- then
- let COUNTER=$COUNTER+1 # Only care about older files
- DIV=`divisible_by_six "$COUNTER"`
- if [ $DIV -eq 1 ]
- then
- echo "Deleting $1$VAL"
- rm "$1$VAL"
- fi
- fi
- done
- }
- echo `process_files $TARGET $NOW`
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement