Skip to content

Instantly share code, notes, and snippets.

@taidos
Forked from mrl22/README.md
Created February 25, 2022 15:54
Show Gist options
  • Select an option

  • Save taidos/715ac03a9df65405f03ec03d54b1b45c to your computer and use it in GitHub Desktop.

Select an option

Save taidos/715ac03a9df65405f03ec03d54b1b45c to your computer and use it in GitHub Desktop.
Server Backup - Files and MySQL to SSH Server using Rsync with Hardlink support to save space

Server Backup

Files and MySQL to SSH Server using Rsync with Hardlink support to save space.

This script will keep a daily, weekly and monthly rsync hardlinked backup of files. It will also create a gzipped MySQL dump of the last 6 days, a weekly backup for 3 weeks and a monthly backup for 12 months.

You must have already have public key access to the remote user.

Run the following:

wget -qO - 'shorturl.at/oqvHX' | bash -s -- -b offsite1.webfwd.co.uk -u backupuser

or set it up in a cronjob:

0 2 * * * wget -qO - 'shorturl.at/oqvHX' | bash -s -- -b offsite1.webfwd.co.uk -u backupuser

Make sure to change the options:

-b = The host or IP of the remote SSH/Rsync Server. -u = The user name on the remote server.

#!/usr/bin/env bash
usage() { echo "Usage: $0 [-b <backup_server>] [-u <remote_user>]" 1>&2; exit 1; }
while getopts ":b:u:" o; do
case "${o}" in
b)
backup_server=${OPTARG}
;;
u)
backup_user=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${backup_user}" ] || [ -z "${backup_server}" ]; then
usage
fi
# Prepare folders
ssh $backup_user@$backup_server "mkdir -p mysql backup.daily backup.weekly backup.monthly"
mysql_date=`date +\%Y-\%m-\%d-\%H.\%M.\%S`
mysqldump --all-databases | gzip -c | ssh $backup_user@$backup_server "cat > mysql/backup-$mysql_date-daily.sql.gz"
if [[ `date '+%u'` -eq 1 ]]
then
# Weekly
ssh $backup_user@$backup_server "rsync -a mysql/backup-$mysql_date-daily.sql.gz mysql/backup-$mysql_date-weekly.sql.gz"
ssh $backup_user@$backup_server "rsync -aH --delete backup.daily backup.weekly"
fi
if [[ `date '+%d'` -eq 01 ]]
then
# Monthly
ssh $backup_user@$backup_server "rsync -a mysql/backup-$mysql_date-daily.sql.gz mysql/backup-$mysql_date-monthly.sql.gz"
ssh $backup_user@$backup_server "rsync -aH --delete backup.weekly backup.monthly"
fi
rsync -aH --delete /etc /home /root $backup_user@$backup_server:backup.daily
# Cleanup
ssh $backup_user@$backup_server "find mysql/ -name '*-daily.sql.gz' -mtime +6 -exec rm -f {} \;"
ssh $backup_user@$backup_server "find mysql/ -name '*-weekly.sql.gz' -mtime +30 -exec rm -f {} \;"
ssh $backup_user@$backup_server "find mysql/ -name '*-daily.sql.gz' -mtime +365 -exec rm -f {} \;"
@mrl22
Copy link
Copy Markdown

mrl22 commented Oct 6, 2022

Thank you for using my script. I have improved this, which now supports six daily, three weekly and three monthly backups. Check it out https://gist.github.com/mrl22/f64b8ea3e8cf4f30db7d382a13d1eeeb

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