# logfile="/var/log/backupalldatabases.log" backup_dir="/backupdir/" username="root" password="xxxxxxxxxxxxxx" if [ ! -d $backup_dir ]; then mkdir -p $backup_dir fi #Find and delete backups with more than 7 days for i in $(find $backup_dir -iname "*.bz2" -mtime +7); do rm -f $i; done #Create a new log rm -rf $logfile touch $logfile #Get date and time timeslot=`date +%Y%m%d_%H%M` cd $backup_dir # Get all databases that user(root) can read for BBDD in $( mysql -u $username --password=$password -h localhost -Bse 'show databases' ); do # Dumping DB to temp file mysqldump --user=$username --password=$password $BBDD > $BBDD.sql # compressing $BBDD.sql gzip -9 $BBDD-$timeslot.gz $BBDD.sql echo "Backup : $BBDD ($BBDD-$timeslot.tar.bz2)" >> $logfile # Borramos el fichero temporal rm $BBDD.sql # ncftpput options # -m Attempt to mkdir the dstdir before copying. # -DD Delete local file after successfully uploading it. # -y Try using "SITE UTIME" to preserve timestamps on remote host. # -R Recursive mode; copy whole directory trees. ncftpput -u $username -p $password -m -DD -y -R $remotehost $remotedir $files # or maybe, rsync with keys #rsync options # -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X) # --delete delete extraneous files from dest dirs rsync -az --delete /backup/ backupuser@remotehost:/backup/ done #------------------------------------------------- # # Send log by email # cat $logfile|mail -s "Proceso Backup $HOSTNAME = MySQL" email@address.com