Skip to content

Instantly share code, notes, and snippets.

@roelentless
Created October 24, 2014 22:24
Show Gist options
  • Save roelentless/534f926e70873e605f97 to your computer and use it in GitHub Desktop.
Save roelentless/534f926e70873e605f97 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Weekly full + daily incrementals of full servers + dbs.
# Cron
# 0 4 * * 6 root bash /scripts/backup full >> /var/log/backup
# 0 4 * * 0,1,2,3,4,5 root bash /scripts/backup >> /var/log/backup
# Configure what folders to backup.
backup_files="/home /etc /root /boot /opt /scripts"
dest="/backups"
latest="$dest/latest"
state="$dest/backup.state"
day=$(date +%Y%m%d)
weekday=$(date +%w)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"
s3_location="bucket/folder"
if [ "$1" == "full" ]; then
echo "Remove incremental file to trigger full backup"
rm -f $state
fi
echo "Removing any previous backup files"
rm -rf $latest
mkdir -p $latest
# Print start status message.
echo "Backing up $backup_files to $latest/$archive_file"
date
echo
# Backup the files using tar.
GZIP=-6 tar --listed-incremental=$state -czf $latest/$archive_file $backup_files
# Split large archives
cd $latest
split -b 50m -a 4 -d $archive_file $hostname-$day.tgz. # To combine: cat parts.* > filename
rm -f $archive_file
# Backup mysql
mysql_password="your password"
mysql -ubackupuser -h localhost -p"$mysql_password" -N -e "show databases where \`Database\` not in ('information_schema', 'performance_schema')" | while read dbname; do mysqldump -ubackupuser -p"$mysql_password" --complete-insert "$dbname" > "$latest/$dbname".sql; done
bzip2 *.sql
# Long listing of files in $dest to check file sizes.
ls -lh $latest
# Upload to S3 (needs expiry lifecycle)
aws s3 sync $latest s3://$s3_location/$day/
# Print end status message.
echo
echo "Backup finished"
date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment