-
-
Save gautiermichelin/0768e44a6abf5fa4a8e1f8b434f256ae to your computer and use it in GitHub Desktop.
Incremental RSync Backup with 14 Day Retention for Databases and Home Folder
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/sh | |
rsync="/usr/bin/rsync" | |
################################ | |
# VARIABLES | |
################################ | |
# General Variables | |
remote_server="yourserver.com" | |
remote_port="22" | |
source="/home/" | |
destination_folder="/path/to/local/backup" | |
destination=$destination_folder/$remote_server | |
################################ | |
# Execute database backup on server | |
# Requires /home/.dbbackups/ folder on remote | |
################################ | |
ssh root@$remote_server 'bash -s' < for i in $(mysql --batch --skip-column-names -e 'SHOW DATABASES' | egrep -v 'information_schema|mysql|eximstats|leechprotect|cphulkd|modsec|roundcube|logaholicDB_.'); do mysqldump -h localhost --opt $i | gzip -c > /home/.dbbackups/$i.sql.gz; done | |
################################ | |
# BACKUP ALL FILES IN /HOME/ | |
################################ | |
if [ ! -d "$destination/Progress" ]; | |
then | |
mkdir "$destination/Progress" | |
fi | |
# try rsync for x times | |
I=0 | |
MAX_RESTARTS=2 | |
LAST_EXIT_CODE=1 | |
while [ $I -le $MAX_RESTARTS ] | |
do | |
I=$(( $I + 1 )) | |
echo $I. start of rsync | |
rsync -ax --stats --progress --include-from="$destination_folder/filter-list.txt" -e "ssh -p $remote_port" --delete --link-dest="$destination/Latest" "root@$remote_server:$source" "$destination/Progress" | |
LAST_EXIT_CODE=$? | |
if [ $LAST_EXIT_CODE -eq 0 ]; then | |
break | |
fi | |
done | |
# check if successful | |
if [ $LAST_EXIT_CODE -ne 0 ]; then | |
echo rsync failed for $I times. giving up. | |
else | |
echo rsync successful after $I times. | |
# Move Progress to Current Date Folder | |
date=`date "+%Y-%m-%d-%H%M%S"` | |
mv "$destination/Progress" "$destination/$date" | |
# Create New Latest Link to Current Date Folder | |
ln -sfn "$date" "$destination/Latest" | |
# Delete Folders Leaving Last 14 | |
ls | find "$destination/." ! -name '.' -type d -maxdepth 1 | sort -rn | awk 'NR>14' | | |
while read file; | |
do rm -Rf "$file"; | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment