Skip to content

Instantly share code, notes, and snippets.

@brunob
Created July 22, 2015 17:05
Show Gist options
  • Save brunob/2861a663967ba5796d89 to your computer and use it in GitHub Desktop.
Save brunob/2861a663967ba5796d89 to your computer and use it in GitHub Desktop.
Simple backup
#!/bin/bash
date=$(date +%Y%m%d)
old=$(date +%Y%m%d -d '7 days ago')
###
# Pamrametres du script
###
# Chemin du repertoire de la sauvegarde
backup="/var/state/"$date
# Creation du dossier pour Backup, s'il existe, ignorer
mkdir $backup 2> /dev/null;
# Prefixe des sauvegardes
prefixe="tydebs"
# Variables de connexion SQL
user_sql="root"
host_sql="localhost"
pass_sql="pass"
host_ftp="ftp.host.net"
user_ftp="ftplogin"
###
# Backup SQL
###
# Options passées à MysqlDUMP
options="--add-drop-table --complete-insert"
# On construit la liste des bases de données
bases="$(mysql -u $user_sql -h $host_sql -p$pass_sql -Bse 'show databases')"
# On lance le dump des bases
for db in $bases
do
mysqldump -u$user_sql -h$host_sql -p$pass_sql $options $db | gzip > $backup"/"$prefixe"-"$db"-"$date".sql.gz"
echo "["$(date +%F\ %X)"] Dump compressé dans "$backup"/"$prefixe"-"$db"-"$date".sql.gz"
done
###
# Transfer data
###
# synchroniser la copie de /var/www et /etc/apache2/sites-available avec rsync
# on lance rsync avant lftp sinon on obtient une erreur avec rsync (pas encore trouvé de solution)
rsync -e ssh -avz --delete --exclude "*/tmp/*" --exclude "*/local/*" /var/www $user_ftp@$host_ftp:state/
rsync -e ssh -avz --delete /etc/apache2/sites-available $user_ftp@$host_ftp:state/
# envoyer les fichiers de backup sql par ftp, puis supprimer la plus vieille archive sur le ftp et en local
cd $backup
lftp -u ${user_ftp},dumb sftp://${host_ftp} -e "set net:timeout 10; set net:reconnect-interval-base 5; set net:reconnect-interval-multiplier 1; set cmd:fail-exit true; mirror -R --ignore-time ${backup} state/${date} ; local rm -rf ${backup}; rm -rf state/${old} ; exit";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment