Skip to content

Instantly share code, notes, and snippets.

@cullylarson
Created October 1, 2018 17:20
Show Gist options
  • Save cullylarson/934b2ce8233b9a837cf6ed83fee95d16 to your computer and use it in GitHub Desktop.
Save cullylarson/934b2ce8233b9a837cf6ed83fee95d16 to your computer and use it in GitHub Desktop.
Database and file backup scripts
### ABOUND BACKUPS
DB_BK_CMD = /home/user/backups/site_name/do-db-backup.sh
FILE_BK_CMD = /home/user/backups/site_name/do-files-backup.sh
# backup / Every day @ 3 am
0 3 * * * $DB_BK_CMD do-daily database_name
0 3 * * * $FILE_BK_CMD do-daily /home/user/public_html
# rotate weekly / Every 7 days @ 3:30am
30 3 */7 * * $DB_BK_CMD do-weekly database_name
30 3 */7 * * $FILE_BK_CMD do-weekly /home/user/public_html
# rotate monthly / On the 1st of the month @ 3:40am
40 3 1 * * $DB_BK_CMD do-monthly database_name
40 3 1 * * $FILE_BK_CMD do-monthly /home/user/public_html
#!/bin/bash
if [[ -z "$1" ]]; then exit 1; fi
if [[ -z "$2" ]]; then exit 1; fi
TASK=$1
# Backup folders
BK_BASE=/home/user/backups/site_name/database
BK_MANUAL=$BK_BASE/manual
BK_DAILY=$BK_BASE/daily
BK_WEEKLY=$BK_BASE/weekly
BK_MONTHLY=$BK_BASE/monthly
# How many backups to keep
KEEP_DAILY=7
KEEP_WEEKLY=4
DBH=localhost
DBU=username
DBP='password'
DBN=$2
# manual backup
if [[ $TASK == "do-now" ]]; then
mysqldump -h $DBH -u $DBU -p$DBP $DBN | gzip -> $BK_MANUAL/$DBN-`date +'%Y%m%d%H%M%S'`.sql.gz
exit 0
fi
# Backup database
if [[ $TASK == "do-daily" ]]; then
mysqldump -h $DBH -u $DBU -p$DBP $DBN | gzip -> $BK_DAILY/$DBN-`date +'%Y%m%d%H%M%S'`.sql.gz
exit 0
fi
# Copy the newest daily backup to weekly, and delete all but latest KEEP_DAILY daily backups
if [[ $TASK == "do-weekly" ]]; then
cp `ls -1td $BK_DAILY/$DBN* | head -n 1` $BK_WEEKLY
ls -1trd $BK_DAILY/$DBN* | head -n -$KEEP_DAILY | xargs -d '\n' rm -f
exit 0
fi
# Copy the newest daily backup to monthly, and delete all but latest KEEP_WEEKLY weekly backups
if [[ $TASK == "do-monthly" ]]; then
cp `ls -1td $BK_DAILY/$DBN* | head -n 1` $BK_MONTHLY
ls -1trd $BK_WEEKLY/$DBN* | head -n -$KEEP_WEEKLY | xargs -d '\n' rm -f
exit 0
fi
#!/bin/bash
if [[ -z "$1" ]]; then exit 1; fi
if [[ -z "$2" ]]; then exit 1; fi
TASK=$1
FOLDER="$2"
FOLDER_P="$2/../"
NAME=`basename $FOLDER`
# Backup folders
BK_BASE=/home/user/backups/site_name/files
BK_MANUAL=$BK_BASE/manual
BK_DAILY=$BK_BASE/daily
BK_WEEKLY=$BK_BASE/weekly
BK_MONTHLY=$BK_BASE/monthly
# How many backups to keep
KEEP_DAILY=7
KEEP_WEEKLY=4
STAMP=`date +'%Y%m%d%H%M%S'`
# Manual backup
if [[ $TASK == "do-now" ]]; then
cd $FOLDER_P && tar cf - $NAME | gzip -> $BK_MANUAL/$NAME-$STAMP.tar.gz
exit 0
fi
# Daily backup
if [[ $TASK == "do-daily" ]]; then
cd $FOLDER_P && tar cf - $NAME | gzip -> $BK_DAILY/$NAME-$STAMP.tar.gz
exit 0
fi
# Copy the newest daily backup to weekly, and delete all but latest KEEP_DAILY daily backups
if [[ $TASK == "do-weekly" ]]; then
cp `ls -1td $BK_DAILY/$NAME* | head -n 1` $BK_WEEKLY
ls -1trd $BK_DAILY/$NAME* | head -n -$KEEP_DAILY | xargs -d '\n' rm -f
exit 0
fi
# Copy the newest daily backup to monthly, and delete all but latest KEEP_WEEKLY weekly backups
if [[ $TASK == "do-monthly" ]]; then
cp `ls -1td $BK_DAILY/$NAME* | head -n 1` $BK_MONTHLY
ls -1trd $BK_WEEKLY/$NAME* | head -n -$KEEP_WEEKLY | xargs -d '\n' rm -f
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment