Skip to content

Instantly share code, notes, and snippets.

@lucarin91
Last active May 12, 2016 01:30
Show Gist options
  • Save lucarin91/13af8f9b206904a5d1d2 to your computer and use it in GitHub Desktop.
Save lucarin91/13af8f9b206904a5d1d2 to your computer and use it in GitHub Desktop.
Backup script
#!/bin/bash
#****
# CUSTOM VARIABLES
TMP_BACKUP="/var/tmp"
LOG_FILE="/var/log/backup-mega.log"
DESTINATION="/mnt/mega/backup"
ENCRYPT_KEY="key.bin"
FOLDER_TO_BACKUP="/home/luca /etc/nginx /var/www"
#****
# DUMP ALL POSTGRES DATABASES
#tmp_Psql=$TMP_BACKUP"/postgres-backup.sql"
#sudo -u postgres -H pg_dumpall > $tmp_Psql
#FOLDER_TO_BACKUP=$FOLDER_TO_BACKUP" "$tmp_Psql
# DUMP ALL MySQL DATABASES
printf "\n[DOING ] CREATING MY-SQL DUMP @ $TMP_BACKUP/mysql-backup.sql \n"
tmp_MySQL=$TMP_BACKUP"/mysql-backup.sql"
mysqldump --user=root --password=pass --all-databases > $tmp_MySQL
FOLDER_TO_BACKUP=$FOLDER_TO_BACKUP" "$tmp_MySQL
printf "[ DONE ]"
# GENERATES THE ARCHIVE FILENAME
day=$(date +"%d-%m-%Y")
day1=$(date +"%d-%m-%Y--%H:%M:%S")
hostname=$(hostname -s)
archive_file="$hostname-$day1"
# PRINTS THE STATUS MESSAGES
printf "\nBacking up $FOLDER_TO_BACKUP to $DESTINATION/$archive_file"
date
# COMPRESS FILES USING tar
printf "\n[DOING ] Compressing files..\n"
tar cz $FOLDER_TO_BACKUP | openssl enc -aes-256-cbc -salt -out $TMP_BACKUP/$archive_file -pass file:$ENCRYPT_KEY
printf "[ DONE ]"
# UPLOAD USING MEGATOOL https://megatools.megous.com/man/megatools.html#_megatools
printf "\n[DOING ] Uploading files.."
megaput --path /Root/sf-backup $TMP_BACKUP/$archive_file
printf "\n[ DONE ]"
# sudo cp $TMP_BACKUP/$archive_file $DESTINATION
status=$?
if [ $status -ne 0 ]; then
echo "error with $status" >> $LOG_FILE
fi
# DELETE ALL TMP FILES
printf "\n[DOING ] Cleaning Up.."
rm $TMP_SQL_FILE $TMP_BACKUP/$archive_file
printf "\n[ DONE ]"
# Print end status message.
echo
echo "Backup finished"
date
# TODO
# - Mongo db dump
# - define a folder to put the key
# - find a solution for the hardcoded mysql paswd
# - write a script that downloads the last backup, decrypts and uncompresses it on a specific folder
#!/bin/bash
#****
#custom variable
TMP_BACKUP="/var/tmp"
LOG_FILE="/var/log/backup-mega.log"
DESTINATION="/mnt/mega/backup"
ENCRYPT_KEY="/etc/backup-mega/key.bin"
FOLDER_TO_BACKUP="/home/luca /etc/nginx /var/www"
#****
#dump all postgres datatbase
tmp_sql=$TMP_BACKUP"/all.sql"
sudo -u postgres -H pg_dumpall > $tmp_sql
FOLDER_TO_BACKUP=$FOLDER_TO_BACKUP" "$tmp_sql
# Create archive filename.
day=$(date +"%d-%m-%Y")
day1=$(date +"%d-%m-%Y--%H:%M:%S")
hostname=$(hostname -s)
archive_file="$hostname-$day1"
# Print start status message.
echo "Backing up $FOLDER_TO_BACKUP to $DESTINATION/$archive_file"
date
echo
# Backup the files using tar.
tar cz $FOLDER_TO_BACKUP | openssl enc -aes-256-cbc -salt -out $TMP_BACKUP/$archive_file -pass file:$ENCRYPT_KEY
sudo cp $TMP_BACKUP/$archive_file $DESTINATION
status=$?
if [ $status -ne 0 ]; then
echo "error with $status" >> $LOG_FILE
fi
#cancello i file tmp del database
rm $TMP_SQL_FILE $TMP_BACKUP/$archive_file
# Print end status message.
echo
echo "Backup finished"
date

##Backup script for mega

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