Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2016 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/b384e6461d5f0bfb043e813a1eba78c8 to your computer and use it in GitHub Desktop.
Save anonymous/b384e6461d5f0bfb043e813a1eba78c8 to your computer and use it in GitHub Desktop.
A backup script for daily file and database backups for a single website
#!/bin/bash
# Site name
site_name="sitename.com"
# Database credentials
user="db_user"
password="super_secret_pass"
host="localhost"
db_name="db_name"
# Files source
files_src_dir="/path/to/$site_name/"
# Backup path
backup_path="/path/to/backups/"
# You shouldn't need to edit anything below this line
# Set the date
date=$(date +"%d-%b-%Y")
# Set today's backup path
today_backup_path=$backup_path$site_name-$date
# Create today's backup directory
mkdir $today_backup_path
# Set default file permissions
umask 177
# Dump database into SQL file
mysqldump --user=$user --password=$password --host=$host $db_name > $today_backup_path/$db_name-$date.sql
# Copy the srouce directory files to today's backup path
cp -a $files_src_dir. $today_backup_path
# TAR the archive
tar -zcf $today_backup_path.tar.gz $today_backup_path
# Now remove the directory that we used to create the archive
rm -rf $today_backup_path
# Delete files older than 15 days
find $backup_path/* -mtime +15 -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment