alexander Posted October 23, 2009 Report Posted October 23, 2009 So if you haven't caught on, i like to make random 1 line long scripts that do cool things, and this is one of these times when its no different, introducing the one liner that does my mysql backups :) cd / && tar cf root/01_mysql_nightly.tar var/log/mysql/`ls -t1 /var/log/mysql | head -n2 | sed -n 2p` && for file in `ls -t1 /var/backups/ | head -n2`; do tar rf root/01_mysql_nightly.tar var/backups/${file}; done && gzip -f root/01_mysql_nightly.tar && wput root/01_mysql_nightly.tar.gz ftp://user:[email protected]/01_mysql_nightly.tar.gz so a little more explanation is needed here... Mini MySQL backup howto: i use unix's logrotate utility to do the actual mysql dumps, to do that i have created a folder /var/backupsthen i created a logrotate job /etc/logrotate.d/mysql_backup /var/backups/mybase.sql.gz { rotate 60 daily nocompress nocopytruncate postrotate HOME=/root mysqldump --opt --all-databases --master-data=2 --flush-logs | gzip > /var/backups/mybase.sql.gz endscript } you can then touch /var/backup/mybase.sql.gzand runlogrotate -f /etc/logrotate.d/mysql_backupwhich will do the first rotation :hihi:then i created a cron task to run logrotate at midnight, because i want everything to be all set for the 1am tape write for sureso crontab -eadded a line 0 0 * * * /usr/sbin/logrotate /etc/logrotate.d/mysql_backup && /root/./ftp_backup.sh ok so that will run logrotate and then execute this ftp_backup script. Since we have a san that writes tapes for everything that's on it, if you throw things on the san, they are automagically backed up each night to tape, all i gotta do is ftp the backup up there (yea yea i know, ftp sucks, not my choice either)I decided to have a backup scheme as such, i back up today's and yesterday's dumps, as well as the binlog between those dates, this way if something is corrupt in the newest backup, i can use the previous backup, dump the transactions out of the binlog, edit the ones that cause the issue, replay them, and i have a functioning DB (without having to pull 2 tapes), so i incorporated this need int o my one line script ftp_backup.sh #!/bin/bash cd / && tar cf root/01_mysql_nightly.tar var/log/mysql/`ls -t1 /var/log/mysql | head -n2 | sed -n 2p` && for file in `ls -t1 /var/backups/ | head -n2`; do tar rf root/01_mysql_nightly.tar var/backups/${file}; done && gzip -f root/01_mysql_nightly.tar && wput root/01_mysql_nightly.tar.gz ftp://user:[email protected]/01_mysql_nightly.tar.gz done, change the script permission to 700 and we are done :) Quote
alexander Posted October 23, 2009 Author Report Posted October 23, 2009 forgot to mention that the logrotate will keep 60 days of backups on the local box, and yes i overwrite the data on the backup server and dont append it, but that's really a backup to a backup and its sole purpose is to write'em tapes :) Quote
stereologist Posted November 20, 2009 Report Posted November 20, 2009 Back in the hey days of APL everyone was a one line king. My favorite was writing polynomial regression in 1 line. Quote
alexander Posted December 1, 2009 Author Report Posted December 1, 2009 Ok, so this just might secure me a title of the supreme overlord of one line command lines.... safe version that does not actually modify files:for a in `ifconfig -a | awk '/eth/{print $1}'`; do if [ -f /etc/sysconfig/network-scripts/ifcfg-${a} ]; then cat /etc/sysconfig/network-scripts/ifcfg-${a} | sed "s/([0-9A-F]{2}:rolleyes:{5}[0-9A-F]{2}/`ifconfig ${a} | awk '/HWaddr/{print $5}'`/"; else ethdevice=`ethtool -i ${a} | awk '/bus-info/{split($2,b,":"); print b[2]":"b[3]}'`; echo -e "# `lspci | grep ${ethdevice} | sed "s/${ethdevice} Ethernet controller: //"`nDEVICE=$anBOOTPROTO=dhcpnHWADDR=`ifconfig ${a} | awk '/HWaddr/{print $5}'`nONBOOT=no"; fi; done; basically centos stores hardware addresses for networking devices in /etc/sysconfig/network-scripts/ifcfg-ethX . This script iterates through your devices and will either substitute the current hardware address in the network script, or create a new "default-ish" network script for the device (notice the device hardware identification portion, took me almost 7 minutes to figure that part out...) so, what do you think supreme overlord application material? sanctus 1 Quote
alexander Posted December 1, 2009 Author Report Posted December 1, 2009 Ok, so this is the one line above in a much more easily controlled script #!/bin/bash function show_help { echo -e "This will change mac addresses in /etc/sysconfig/network-scripts/ifcfg-ethX to the corresponding hardware interfacesnnOptions:n-t test run, will display the changesn-f will actually write the changesn-h will display this helpn-j the just do it option, no confirmation, outputs done when done" exit } # safe, but doesnt fix things function safe_run { for a in `ifconfig -a | awk '/eth/{print $1}'`; do echo "Found ${a} device" if [ -f /etc/sysconfig/network-scripts/ifcfg-${a} ]; then echo -e "Found ifcfg script correlating to the ${a} devicenCorrected file:n" cat /etc/sysconfig/network-scripts/ifcfg-${a} | sed "s/([0-9A-F]{2}:rolleyes:{5}[0-9A-F]{2}/`ifconfig ${a} | awk '/HWaddr/{print $5}'`/" echo -e "nn" else ethdevice=`ethtool -i ${a} | awk '/bus-info/{split($2,b,":"); print b[2]":"b[3]}'` echo -e "nWarning: Could not find a ifcfg file correlating to the ${a} devicenPlease make sure your distro uses these files if you want to run the fixnnThe script will create a default config file containing:n" echo -e "# `lspci | grep ${ethdevice} | sed "s/${ethdevice} Ethernet controller: //"`nDEVICE=$anBOOTPROTO=dhcpnHWADDR=`ifconfig ${a} | awk '/HWaddr/{print $5}'`nONBOOT=no" echo -e "nn" fi done exit } # dangerous, but does fix things function fix_run { while : do echo "Are you sure, this could break things, you know! Continue? [n]:"; read yn; case $yn in "y" ) break;; "Y" ) break;; * ) exit;; esac done while : do echo "No i mean like really sure, this really can break things! Continue? [n]:"; read yn; case $yn in "y" ) break;; "Y" ) break;; * ) exit;; esac done for a in `ifconfig -a | awk '/eth/{print $1}'`; do echo "Found ${a} device" if [ -f /etc/sysconfig/network-scripts/ifcfg-${a} ]; then echo "Correcting the ifcfg script" sed -i "s/([0-9A-F]{2}:phones:{5}[0-9A-F]{2}/`ifconfig ${a} | awk '/HWaddr/{print $5}'`/" /etc/sysconfig/network-scripts/ifcfg-$a else echo "nWarning: ifcfg file not found, generating a new ifcfg file" ethdevice=`ethtool -i ${a} | awk '/bus-info/{split($2,b,":"); print b[2]":"b[3]}'` echo -e "# `lspci | grep ${ethdevice} | sed "s/${ethdevice} Ethernet controller: //"`nDEVICE=$anBOOTPROTO=dhcpnHWADDR=`ifconfig ${a} | awk '/HWaddr/{print $5}'`nONBOOT=no" > /etc/sysconfig/network-scripts/ifcfg-$a fi done exit } function just_do_it { for a in `ifconfig -a | awk '/eth/{print $1}'`; do if [ -f /etc/sysconfig/network-scripts/ifcfg-${a} ]; then sed -i "s/([0-9A-F]{2}:){5}[0-9A-F]{2}/`ifconfig ${a} | awk '/HWaddr/{print $5}'`/" /etc/sysconfig/network-scripts/ifcfg-$a else ethdevice=`ethtool -i ${a} | awk '/bus-info/{split($2,b,":"); print b[2]":"b[3]}'` echo -e "# `lspci | grep ${ethdevice} | sed "s/${ethdevice} Ethernet controller: //"`nDEVICE=$anBOOTPROTO=dhcpnHWADDR=`ifconfig ${a} | awk '/HWaddr/{print $5}'`nONBOOT=no" > /etc/sysconfig/network-scripts/ifcfg-$a fi done echo "Done" exit } if [ $# != 1 ]; then show_help fi case $1 in -t) safe_run ;; -f) fix_run ;; -j) just_do_it ;; -h) show_help ;; *) show_help ;; esac Enjoy Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.