Skip to content

Instantly share code, notes, and snippets.

@briezler
Last active May 16, 2016 23:17
Show Gist options
  • Save briezler/e9ee84725e873a20663e to your computer and use it in GitHub Desktop.
Save briezler/e9ee84725e873a20663e to your computer and use it in GitHub Desktop.
Webdirectory Backup including database
#/bin/bash
# SET Variables
NOW="$(date +"%d-%m-%Y_%H-%M-%S")"
ROOT_PATH="/home/www/p177910/html"
DIRECTORY_TO_SAVE="$ROOT_PATH/typo3"
BACKUP_TO_DIRECTORY="$ROOT_PATH/backup"
LOGFILE="$ROOT_PATH/backup/log/yourlogfile.log"
USER=who
DB_USER=""
DB_PASS=""
DB_NAME=""
DB_HOST=""
EXCLUDE_FILE="$ROOT_PATH/backup/bu_exclude.txt"
DELETE_OLDER_THAN_DAYS="7"
SQL_BACKUP_NAME="$DB_NAME.$NOW.sql"
#check if Backupdirectory exists
if [ ! -d "$DIRECTORY_TO_SAVE" ]; then
echo "Directory doesn't exist!"
echo "Tried to backup non existent directory;$NOW" >> $LOGFILE
exit 2
else
mysqldump -u $DB_USER -p$DB_PASS -h $DB_HOST $DB_NAME > "$DIRECTORY_TO_SAVE/$SQL_BACKUP_NAME" | >> $LOGFILE
if [ "$?" -eq 0 ]
then
echo "$USER;MySQL Dump $SQL_BACKUP_NAME successfully created;$NOW" >> $LOGFILE
# zip Directory
tar -C "$DIRECTORY_TO_SAVE/" -zvchf "$BACKUP_TO_DIRECTORY/$NOW.tar.gz" -X $EXCLUDE_FILE "$DIRECTORY_TO_SAVE" "$BACKUP_TO_DIRECTORY/$SQL_BACKUP_NAME"
echo "$USER; Directory $BACKUP_TO_DIRECTORY/$NOW.tar successfully saved;$NOW" >> $LOGFILE
rm $DIRECTORY_TO_SAVE/$SQL_BACKUP_NAME
#go to backup dir and remove all files older than 10 days
cd $BACKUP_TO_DIRECTORY
find *.tar.gz -mtime +$DELETE_OLDER_THAN_DAYS -delete
exit 0
else
# mysqldump couldn't be saved/created
echo "$USER;MySQL Dump $SQL_BACKUP_NAME couldnt be created. see database.err for additional information;$NOW" >> LOGFILE
exit 2
fi
fi
exit 0
@pixelink
Copy link

Backup Web Directory

Usage

Copy that script into a .sh file. Let's say CronBackup.sh and chmod +0755 to make it executable.
Then set the variables, and be aware that every directory wich is defined has proper rights, that they could be accessed by the executing cronjob user.

Thats it. You could test the script by running ./CronBackup.sh.

Variables

NOW is used to give the .gz and database dump a filename wich is timestamped
DIRECTORY_TO_SAVE is the directory you like to backup
BACKUP_DIRECTORY is the path where u like to save your backup
LOGFILE where the logs should be put.
USER leave as it is

Database credentials

As you set the database credentials, keep in mind to keep the shell script out of your webroot, so no one can access the file an fetch that data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment