{"id":661,"date":"2012-11-08T15:30:09","date_gmt":"2012-11-08T15:30:09","guid":{"rendered":"http:\/\/blogs.silicontechnix.com\/?p=661"},"modified":"2012-11-08T15:32:00","modified_gmt":"2012-11-08T15:32:00","slug":"ftp-automation-on-linux","status":"publish","type":"post","link":"https:\/\/blogs.silicontechnix.com\/?p=661","title":{"rendered":"FTP automation on Linux"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Ever wanted FTP backups and automation for keeping backups of important file on off-peak time. I always love automation, so machines can do things automatically and help humans \ud83d\ude42<!--more--><\/p>\n<p>Ok lets start, create a shell script<\/p>\n<pre class=\"brush:bash\">vim autoftp<\/pre>\n<p>Now paste this script and adjust the parameters of ftp host, email address, backup directory, folder to backup.<\/p>\n<pre class=\"brush:bash\">ftphost= IP_Address\r\nEMAIL=me@my.com\r\nDIRTOBACKUP=\/var\/www\/html\r\nBACKUPDIR=\/home\/backup\r\nLOGFILE=$BACKUPDIR\/autoftp.log\r\n\r\nhostname=`hostname`\r\nfile=$hostname-backup-`date +%Y-%m-%d-%H-%M`.tgz\r\ntime=`date +%Y-%m-%d-%H:%M:%S`\r\ncd $BACKUPDIR\r\necho \"###################################################\"&gt; $LOGFILE\r\necho \"# Start Date&amp;Time $time\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\"&gt;&gt; $LOGFILE\r\necho \"###################################################\"&gt;&gt; $LOGFILE\r\necho \"#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Backup Process Started\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\"&gt;&gt; $LOGFILE\r\necho \"###################################################\"&gt;&gt; $LOGFILE\r\necho [$time] $file created &gt;&gt; $LOGFILE\r\ntar cpzf $file $DIRTOBACKUP\r\ntime=`date +%Y-%m-%d-%H:%M:%S`\r\necho [$time] backup compressed now sending to ftp server &gt;&gt; $LOGFILE\r\n\r\nftp -v &lt;&lt; EOF\r\nopen $ftphost\r\nbinary\r\nput $file\r\nquit\r\nEOF\r\n#if your mail client is not setup, you can skip, email backup reports, by commenting it with hash sign #\r\nmail -s \"Backup Report\" $EMAIL &lt; $LOGFILE<\/pre>\n<p>Save the file in any place you like, in this example I am saving it to my home directory, i.e. \/home\/bshafiq\/autoftp<br \/>\nGive execute permission to newly created shell script by doing this:<\/p>\n<pre class=\"brush:bash\">chmod +X autoftp<\/pre>\n<p>For providing FTP authentication details, &#8220;.netrc&#8221; text file with ip userid password details in user root folder<br \/>\nlike this:-<\/p>\n<pre class=\"brush:bash\">vim ~\/.netrc<\/pre>\n<p>and paste details like this:-<br \/>\nmachine <em>IP_Address<\/em> login <em>Your_UserID<\/em> password <em>Your_Super_Strong_Password<\/em><\/p>\n<p>Final step add to crontab<\/p>\n<pre class=\"brush:bash\">crontab -e -u root<\/pre>\n<p>(Change root with anyother user of your choice, but the user must have access to the folders we want to backup and where we put the backup)<\/p>\n<pre class=\"brush:bash\">0 1 * * * \/home\/bshafiq\/autoftp &gt; \/dev\/null<\/pre>\n<p>(It will auto backup on 1am daily, adjust the time as your want)<\/p>\n<p>Check the crontab is ok?<\/p>\n<pre class=\"brush:bash\">crontab -l -u root<\/pre>\n<p>Make sure crontab service is running<\/p>\n<pre class=\"brush:bash\">service crontab status<\/pre>\n<p>That&#8217;s it ! Enjoy auto ftp backups \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Ever wanted FTP backups and automation for keeping backups of important file on off-peak time. I always love automation, so machines can do things automatically and help humans \ud83d\ude42<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"no","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95],"tags":[295,223,296,253,294,19,297],"class_list":["post-661","post","type-post","status-publish","format-standard","hentry","category-system-administration","tag-automation","tag-centos","tag-crontab","tag-fedora","tag-ftp","tag-linux","tag-vim"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p12j6H-aF","_links":{"self":[{"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/posts\/661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=661"}],"version-history":[{"count":5,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/posts\/661\/revisions"}],"predecessor-version":[{"id":665,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=\/wp\/v2\/posts\/661\/revisions\/665"}],"wp:attachment":[{"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.silicontechnix.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}