Skip to content

Instantly share code, notes, and snippets.

@samanzamani
Created August 6, 2020 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samanzamani/7ab48c4aed1c00e83d967e328504837b to your computer and use it in GitHub Desktop.
Save samanzamani/7ab48c4aed1c00e83d967e328504837b to your computer and use it in GitHub Desktop.
auto generate back up from all available databases and sites
#!/bin/bash
#----------------------------------------
# OPTIONS
#----------------------------------------
USER='' # MySQL User
PASSWORD='' # MySQL Password
DAYS_TO_KEEP=60 # 0 to keep forever
GZIP=1 # 1 = Compress
BACKUP_PATH='/var/backup-sites/databases'
#----------------------------------------
# Create the backup folder
if [ ! -d $BACKUP_PATH ]; then
mkdir -p $BACKUP_PATH
fi
# Get list of database names
databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "|" | grep -v Database`
for db in $databases; do
if [ $db == 'information_schema' ] || [ $db == 'performance_schema' ] || [ $db == 'mysql' ] || [ $db == 'sys' ]; then
echo "Skipping database: $db"
continue
fi
date=$(date -I)
if [ "$GZIP" -eq 0 ] ; then
echo "Backing up database: $db without compression"
mysqldump -u $USER -p$PASSWORD --databases $db > $BACKUP_PATH/$date-$db.sql
else
echo "Backing up database: $db with compression"
mysqldump -u $USER -p$PASSWORD --databases $db | gzip -c > $BACKUP_PATH/$date-$db.gz
fi
done
# Delete old backups
if [ "$DAYS_TO_KEEP" -gt 0 ] ; then
echo "Deleting backups older than $DAYS_TO_KEEP days"
find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \;
fi
#!/bin/bash
#----------------------------------------
# OPTIONS
#----------------------------------------
DAYS_TO_KEEP=1 # 0 to keep forever
WWW_PATH='/var/www'
BACKUP_PATH='/var/backup-sites/files'
#----------------------------------------
# Create the backup folder
if [ ! -d $BACKUP_PATH ]; then
mkdir -p $BACKUP_PATH
fi
# change into the web root directory
cd "$WWW_PATH"
if [ "$(pwd)" != "$WWW_PATH" ] ; then
echo "Failed to change directory to root of web path"
exit
fi
for website in * ; do
if [[ -d $website && ! -L "$website" ]]; then
echo "Found website folder: $website"
date=$(date -I)
tar -cvpzf $BACKUP_PATH/$date-$website.tar.gz $website
fi
done
# Delete old backups
if [ "$DAYS_TO_KEEP" -gt 0 ] ; then
echo "Deleting backups older than $DAYS_TO_KEEP days"
find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment