Created
November 13, 2015 21:31
-
-
Save Philosoft/4da46ffd103a53e970d5 to your computer and use it in GitHub Desktop.
simplest possible script for backing up your DB with sh + cron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# usage: | |
# crontab -e | |
# 20 1 * * * /bin/bash /home/user/bin/backuper.sh | |
## configuration { | |
BACKUP_DIR="ABSOLUTE_PATH_TO_BACKUP_DIR" | |
DBNAME="UR_DB_NAME" | |
DBUSER="UR_DB_USER" | |
DBPASSWD="UR_DB_PASSWORD" | |
DATE_TEMPLATE="$(date '+%Y.%m.%d')" | |
DAYS_TO_STORE=30 | |
## } | |
fname="${BACKUP_DIR}/${DBNAME}_${DATE_TEMPLATE}.sql" | |
# ensure existence of $BACKUP_DIR | |
mkdir -p "$BACKUP_DIR" | |
mysqldump --user="$DBUSER" --password="$DBPASSWD" "$DBNAME" > "$fname" | |
gzip --best "$fname" | |
# delete old backups | |
find "$BACKUP_DIR" -type f -name "*.gz" -ctime "$DAYS_TO_STORE" -delete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment