Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Database backup script
- #
- #Large tables - we only keep one copy of these
- export mysql_large_tables="track_page track_visit emails_message_body emails_message_body_new"
- export dumpopts=" --single-transaction --max_allowed_packet=32M --compact "
- export mysqldump="mysqldump -Q $dumpopts"
- export backup_root='/srv/backup/temp-databases'
- export ldap_dumpall="/usr/sbin/slapcat"
- export email=#Your email
- export POSTGRES_USER=#Your user
- export MYSQL_USER=#Your user
- export MYSQL_PASS=#Your pass
- export ftp_user=#Your FTP Username
- export ftp_password=#Your FTP Password
- export ftp_host=#YOUR HOST
- export ftp_url="ftp://$ftp_host/"
- export curlftp="curl --ftp-create-dirs -v -u $ftp_user:$ftp_password"
- export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- umask 022
- export DEBIAN_FRONTEND=noninteractive
- if [ ! -e /usr/bin/lzop ]; then
- apt-get --force-yes -y install lzop
- fi
- if [ ! -e /usr/bin/parallel ]; then
- apt-get --force-yes -y install parallel
- fi
- if [ ! -e /usr/bin/ncftpput ]; then
- apt-get --force-yes -y install ncftp
- fi
- function check_port() {
- host=$1;
- port=$2
- timeout 1 bash -c "cat < /dev/null > /dev/tcp/$host/$port"
- if [[ $? -ne 0 ]]; then
- echo "Cant connect to port $host:$port"
- return 1
- fi
- echo "Successfully connected to $port on $host"
- return 0
- }
- export -f check_port
- function backup_mysql() {
- host="$1"
- user="$2"
- password="$3"
- connect_opts=" -u $user --password=$password -h $host"
- # set -o xtrace
- # mysql_running tells us the number of mysql processes running
- # mysql_status tells us the exit number of the mysql_running command
- if [ ! -e /usr/bin/mysqldump ] ; then
- return
- fi
- echo "is mysql running?"
- check_port $host 3306 || return
- # Host must be up - back it up
- echo `date`
- echo "Backup mysql on $host.zeald.com"
- #make sure directory exists
- ncftp -u $ftp_user -p $ftp_password $ftp_host<<EOF
- mkdir $host
- EOF
- for db in `mysql $connect_opts -e "SHOW DATABASES" --skip-column-names`;
- do
- if [[ "$db" == "mysql" ]]; then
- continue
- fi
- if [[ "$db" == "information_schema" ]]; then
- continue
- fi
- if [[ "$db" == "performance_schema" ]]; then
- continue
- fi
- echo "Backing up $db";
- db_replaced="${db/shop_/}"
- firstletter=${db_replaced:0:1}
- #make sure directory exists
- ncftp -u $ftp_user -p $ftp_password $ftp_host<<EOF
- mkdir $host/$firstletter
- EOF
- ignore=""
- for i in $mysql_large_tables; do
- ignore="$ignore --ignore-table=$db.$i"
- done
- date
- echo "$mysqldump $db $ignore | lzop | ncftpput -c $ftp_host $host/$firstletter/db-$host-$db-mysql-$(date +%d).sql.lzo"
- $mysqldump $connect_opts $db $ignore | lzop | ncftpput -c -u $ftp_user -p $ftp_password $ftp_host $host/$firstletter/db-$host-$db-mysql-$(date +%d).sql.lzo
- # Check what happened - did the backup successfully run? If not, email $email
- if [ $? != 0 ]; then
- # Mail the administrator
- echo "Sending email to $email"
- mail_text="While backing up MySQL $db on $host.zeald.com, something went wrong with backup (1)"
- echo "$mail_text" | mail $email -s "Backup failure!"
- echo "Failed backing up mysql db's on $host!"
- #continue
- fi
- #backup the large tables
- for table in $mysql_large_tables; do
- echo "$mysqldump $db $table | lzop | ncftpput -c $ftp_host $host/$firstletter/db-$host-$db-mysql-largetable-$table.sql.lzo"
- $mysqldump $connect_opts $db $table | lzop | ncftpput -c -u $ftp_user -p $ftp_password $ftp_host $host/$firstletter/db-$host-$db-mysql-largetable-$table.sql.lzo
- done
- done
- }
- export -f backup_mysql
- function backup_ldap() {
- host="$1"
- echo "Check if ldap is running on $host";
- check_port $host 636 || return
- echo "Backup ldap on $host"
- #make sure directory exists
- ncftp -u $ftp_user -p $ftp_password $ftp_host<<EOF
- mkdir $host
- EOF
- $ldap_dumpall | lzop | ncftpput -c -u $ftp_user -p $ftp_password $ftp_host $host/db-$host-ldap-$(date +%d).ldif.lzo
- if [ $? != 0 ]; then
- echo "Failed backing up ldap dbs on $host"
- return
- fi
- }
- export -f backup_ldap
- function backup_postgres() {
- host="$1"
- echo "Check if postgresql is running on $host";
- check_port $host 5432 || return
- echo "Backup postgres on $host"
- #make sure directory exists
- ncftp -u $ftp_user -p $ftp_password $ftp_host<<EOF
- mkdir $host
- EOF
- #String to append to the name of the backup files
- #Numbers of days you want to keep copie of your databases
- number_of_days=30
- databases=` su postgres -c "psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'"`
- for i in $databases; do
- if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then
- echo Dumping $i to $backup_dir$i\_$backup_date
- echo " pg_dump -Fc $i | lzop | ncftpput -c -u $ftp_user -p $ftp_password $ftp_host $host/db-$host-postgres-$(date +%d).sql.lzo"
- su postgres -c "pg_dump -Fc $i" | lzop | ncftpput -c -u $ftp_user -p $ftp_password $ftp_host $host/db-$host-postgres-$(date +%d).sql.lzo
- fi
- done
- }
- export -f backup_postgres
- function backup_server {
- server=$1
- echo "Backup process for $server beginning : Check what databases are running and back them up"
- backup_ldap $server
- backup_postgres $server zeald
- backup_mysql $server zeald pq27_zTl
- echo "Backed up $server";
- }
- export -f backup_server
- date
- #Delete any old backups
- servers=`hostname`
- if [ -e /usr/local/bin/zeald_cluster_manager ]; then
- echo 'Found zeald_cluster_manager'
- DO_BACKUP_POD=`/usr/local/bin/zeald_cluster_manager is-resource-minder mysql`;
- if [[ $DO_BACKUP_POD -eq 1 ]]; then
- echo "Is Master : check dbs for this pod"
- zest_db_servers=`perl <<'EOF'
- use lib '/opt/platform/interchange_hosting';
- use lib '../interchange_hosting';
- use Data::Dumper;
- require ZealdCluster;
- require ZealdUtil;
- use List::MoreUtils qw(uniq);
- use List::Util qw/shuffle/;
- my @servers;
- #Backup the cluster nfs resources
- my $api = ZealdCluster->new();
- my $resources = $api->get_cluster_resources($api->{cluster_id}, 'mysql');
- foreach my $resource (@$resources) {
- push @servers, $resource->{details};
- }
- print join " ", shuffle uniq @servers;
- EOF`
- servers="$servers $zest_db_servers"
- fi
- fi
- echo "Servers:"
- echo $servers
- #Get a lock on this backup
- lockfile="/var/run/zeald-remote-backup-$server"
- if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null;
- then
- trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
- for server in $servers; do
- echo "Starting backup for $server"
- sem -u -j4 "backup_server $server"
- # backup_server $server
- done
- sem --wait; echo sem --wait done
- rm -f "$lockfile"
- trap - INT TERM EXIT
- else
- echo "Failed to acquire lockfile: $lockfile."
- echo "Held by $(cat $lockfile)"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement