Posts Tagged ‘tar’
Linux scripts, Server administration - Sunday, June 1, 2008 23:30 - 0 Comments
Simple linux FTP backup script
This is a quick and simple linux FTP backup script we use to run on clients servers that have small backups that need to be shipped off to a remote FTP server somewhere. You can basically backup anything from here, Mysql, Apache config files, webserver files etc…
Requirments:
Shell access
ncftp
Vi or Pico/Nano editor
Cron
Step 1:
Log into your server as the user you want to run this script as (usually root). You can then create a file somewhere on your server (I usually put the file in /etc/backup)
# cd /etc
# mkdir backup
# cd backup
# nano backup.sh
Step 2:
Cut and paste the script I have written into the file and then edit the parts required (it is spretty streight forward to understand but I will put comments on each line — the comments will start with a * so do not add these comments into your script). After you have edited and save the file you must chmod the file 755 to make it executable
#!/bin/sh
HOST=’192.168.1.3′
* this can be an ip address or a hostname of the destination FTP server
USER=’ftpbackup’
* the username of the ftp server
PASSWD=’backup0012′
* the password on the ftp server
DATE=`/bin/date +%Y%m%d`
* no edits for DATE
TIME=`/bin/date +%H`
* No Edits for TIME
HOSTNAME=`/bin/hostname`
* No edits for HOSTNAME
mysqldump -c -B -uroot -pfoobar DB1 DB2 mysql > data.sql &&
* This is if you want to backup your mysql database, DB1 and DB2 represent databases within mysql you want yo backup -uroot -pfoobar (change this to your mysql username and password). if you are not backing up mysql then remove the above line.
tar zcvf $DATE.$TIME.$HOSTNAME.tar.gz ./data.sql /etc/ /var/www > /dev/null &&
* you can edit this line as you wish if you are not backing up mysql you can remove ./data.sql, you can add directories such as /etc and /var/www
ncftpput -u $USER -p $PASSWD $HOST /tobebackedup $DATE.$TIME.$HOSTNAME.tar.gz ;:
* nothing to edit here
The finished script:
#!/bin/sh
HOST=’192.168.1.3′
USER=’ftpbackup’
PASSWD=’backup0012′
DATE=`/bin/date +%Y%m%d`
TIME=`/bin/date +%H`
HOSTNAME=`/bin/hostname`
mysqldump -c -B -uroot -pfoobar DB1 DB2 mysql > data.sql &&
tar zcvf $DATE.$TIME.$HOSTNAME.tar.gz ./data.sql > /dev/null &&
ncftpput -u ftpbackup -p backup0012 $HOST /tobebackedup $DATE.$TIME.$HOSTNAME.tar.gz ;:
Running the script on a schedule (Crontab howto):
as the user that you want to run the backup script as type the following commands:
crontab -e
To run weekly on Sunday enter the line:
* * * * 6 /etc/backup/backup.sh
Article written by MyComputerAid.com