Automatic Backups - Shell Script + Cron

Good solution.
But it does not fit if you cannot install extra software on your site.

I use two bash scripts for my CRM and phpBB forum:
backup_crm_db.sh - run automaticaly by cron every night.
backup_crm_full.sh - run automaticaly once a week and run manual every time before upgarde SuiteCRM or change something in configuration.

[spoiler=backup_crm_db.sh]

#!/bin/sh

########################
# Backup only database #
########################

days=14						# Days to keep backup

temp_path="tmp"					# Temp folder. Will create automaticaly if not exist
tempsql="$temp_path/crmbackup.sql"		# Name file with backup of database
dest="backup"					# Destination backup archives

dbhost="xxx"					# Host of database
dbname="xxx"					# Database name
dbuser="xxx"					# User name
dbpassword="xxx"				# Password

yandexdisk_email='xxx'				# Mail yandex.disk
yandexdisk_pass='xxx'				# Password yandex.disk
yandexdisk_dir='backup/backup_crm_db.tgz.gpg'	# Path to save yandex.disk

user_id="xxx"					# User id for asymmetric encrypt. Dont forget to import key.

# Create archive filename
day=$(date +%F-%H-%M)
hostname=$(hostname -s)
archive_file="$hostname-crm-db-$day.tgz"

# Create temp folder
mkdir -p "$temp_path"

# Dump MySQL
mysqldump $dbname --host=$dbhost --user=$dbuser --password=$dbpassword --default-character-set=utf8 > "$tempsql"

# Compress backup files using tar.
tar czPf "$dest/$archive_file" "$tempsql"

# Clear temp files
rm "$tempsql"

# Delete old backups
find "$dest" -mtime +$days -regex ".*db.*" -type f -exec rm -f {} \;

# Encrypt backup
gpg -e -r "$user_id" "$dest/$archive_file"

# Send backup to Yandex Disk
curl -v --user $yandexdisk_email:$yandexdisk_pass -T $dest/$archive_file.gpg https://webdav.yandex.ru/$yandexdisk_dir

# Delete encrypted file
rm -f $dest/*.gpg

[/spoiler]

[spoiler=backup_crm_full.sh]

#!/bin/sh

#############################
# Backup files and database #
#############################

days=61						# Days to keep backup

temp_path="tmp"					# Temp folder. Will create automaticaly if not exist
tempsql="$temp_path/crmbackup.sql"		# Name file with backup of database
dest="backup"					# Destination backup archives
backup_files="docs"				# Files to backup

dbhost="xxx"					# Host of database
dbname="xxx"					# Database name
dbuser="xxx"					# User name
dbpassword="xxx"				# Password

yandexdisk_email='xxx'				# Mail yandex.disk
yandexdisk_pass='xxx'				# Password yandex.disk
yandexdisk_dir='backup/backup_crm_db.tgz.gpg'	# Path to save yandex.disk

user_id="xxx"					# User id for asymmetric encrypt. Dont forget to import key.

# Create archive filename
day=$(date +%F-%H-%M)
hostname=$(hostname -s)
archive_file="$hostname-crm-full-$day.tgz"

# Create temp folder
mkdir -p "$temp_path"

# Dump MySQL
mysqldump $dbname --host=$dbhost --user=$dbuser --password=$dbpassword --default-character-set=utf8 > "$tempsql"

# Compress backup files using tar.
tar czPf $dest/$archive_file "$backup_files" "$tempsql"

# Clear temp files
rm "$tempsql"

# Delete old backups
find $dest -mtime +$days -regex ".*full.*" -type f -exec rm -f {} \;

# Encrypt backup
gpg -e -r "$user_id" $dest/$archive_file

# Send backup to Yandex Disk
curl -v --user $yandexdisk_email:$yandexdisk_pass -T $dest/$archive_file.gpg https://webdav.yandex.ru/$yandexdisk_dir

# Delete encrypted file
rm -f $dest/*.gpg

[/spoiler]

3 Likes