{"id":219,"date":"2012-06-12T11:10:57","date_gmt":"2012-06-12T03:10:57","guid":{"rendered":"http:\/\/rmohan.com\/?p=219"},"modified":"2012-06-12T11:29:44","modified_gmt":"2012-06-12T03:29:44","slug":"mysql-back-on-unix","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=219","title":{"rendered":"MYSQL Back On Unix"},"content":{"rendered":"<p><strong>MYSQL Back On Unix <\/strong><\/p>\n<p>Shell script is a script where we are writing different types of commands and executing those commands from a single file.  We can execute that command manually, by entering command one by one. But if we use shell script we have to write those commands into a text file for the first time and then we can run those commands as many times as required.<\/p>\n<p>In this article first I will show you, Complete Script. Later on, you will get a description of that script.  I assumed that you know about shell scripting, mysqldump and crontab.<\/p>\n<p>Operating System: Any Linux or UNIX.<\/p>\n<p>Main Script (for backup your mysql database):<\/p>\n<p>This shell script will make the backup process of a database automatic. Just copy and paste this script in a text editor, put database user name, password, and database name. I will use  mysqlump command to backup the database. Later on you will get description of each line.<br \/>\n1. Make a directory name \u201cbackup\u201d and \u201coldbackup\u201d<br \/>\n1<br \/>\n2<\/p>\n<p>mkdir \/backup<br \/>\nmkdir \/oldbackup<br \/>\n2. Now make file name \u201cbackup.sh\u201d and edit with any editor you like<\/p>\n<p>I\u2019m using vi here-<br \/>\n1<\/p>\n<p># vi \/backup\/backup.sh<\/p>\n<p>Now write these lines into backup.sh file:<br \/>\n#!bin\/bash<br \/>\ncd \/backup<br \/>\necho \u201cYou are In Backup Directory\u201d<br \/>\nmv backup* \/oldbackup<br \/>\necho \u201cOld Databases are Moved to oldbackup folder\u201d<br \/>\nNow=$(date +\u201d%d-%m-%Y&#8211;%H:%M:%S\u201d)<br \/>\nFile=backup-$Now.sql<br \/>\nmysqldump \u2013u user-name  \u2013p \u2018password\u2019 database-name > $File<br \/>\necho \u201cYour Database Backup Successfully Completed\u201d<\/p>\n<p>Script Description:<\/p>\n<p>Remember, in line number 8: Put your Database username, Password, database name after mysqldump command.<\/p>\n<p>When we run this script, first it goes to a \/backup directory. Then it will move old database backup files to \/oldbackup folder. Next it generates a file name from system date and time. And finally mysqldump command will generate a database backup file with \u201c.sql\u201d format<br \/>\n3. Set permission to backup.sh file executable<br \/>\n1<\/p>\n<p># chmod +x \/backup\/backup.sh<br \/>\n4. Running the Script<br \/>\n1<\/p>\n<p>#.\/backup.sh<\/p>\n<p>You will get following output after executing the script.<br \/>\nroot@Server1:\/download#.\/backup.sh<br \/>\nYou are in Download Directory<br \/>\nOld Backup Database is Moved to oldbackup folder<br \/>\ndatabase backup successful completed<br \/>\nroot@Server1:\/download#<\/p>\n<p>NB: first time when you run this script you will get a message \u201cno such file\u201d, because you don\u2019t have old backup file. No problem just runs again this script, problem will be solved.<br \/>\n5. Schedule the Backup using cron<\/p>\n<p>Using Cron job you can execute this script in a certain time, and all works will be done automatically. Use crontab command to edit editing cron schedules.<\/p>\n<p>#crontab \u2013e<\/p>\n<p>Just add below line in the editor and save it.<br \/>\n0 13 * * * * \/backup\/backup.sh<\/p>\n<p>In this way every day 1PM your database will back up in your desired folder. Please visit crontab manuals for more details about setting the cron jobs.<\/p>\n<p>This is a very basic script for the beginners. Hope you can use the same idea for more complex backup. We will try to come up with new scripts to automate further. Please ask any question you have. We will try our best to address your questions. Thanks for staying with us.<\/p>\n<p>No related posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MYSQL Back On Unix <\/p>\n<p>Shell script is a script where we are writing different types of commands and executing those commands from a single file. We can execute that command manually, by entering command one by one. But if we use shell script we have to write those commands into a text file for [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/219"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=219"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":221,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/219\/revisions\/221"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}