Link to home
Start Free TrialLog in
Avatar of frostino
frostinoFlag for United States of America

asked on

How do I create a backup script in a Linux environment?

I'm not very experienced with Linux, scripts, or cron jobs.  I'm looking to set up a few backups, one is good for now, that create a backup file and move it to a folder on the server.  Ideally, I'd like to set it up to FTP the file to another server but I'll settle for just a folder on the current server for now.  I have it working so far but I'm using this script that I found online.  It's not putting the file where I want it even though I created the directory "web_backup".  

I guess my first question is how do I get it in the correct folder.

Next, is there a simpler script I may use to get the same thing done?  I'm using this in cPanel where the Cron Jobs interface allows me to just select the frequency and I enter the location of the script to execute.  I can clarify any of this, if needed.

###
# cPanel Backup via Cron v1.2.1
###

###
#  Instructions
###
# Make sure CP_SCP_BACKUP_ROOT directory exists. It is "web_backup" by default.
# Within this directory you'll need a directory for each cPanel account you're
# backing up.
#
# I.e. if you're backing up "example.com" and "foobar.com" and you want them
# to be backed up in the default location of "web_backup", you'll need to
# execute the following commands at the recieving end:
# mkdir ~/web_backup
# mkdir ~/web_backup/example.com
# mkdir ~/web_backup/foobar.com
###


###
# Config begins
### 

#cPanel info -- host that is being backed up
CP_USERNAME=uname
CP_PASSWORD=pword
CP_DOMAIN=domain.com
CP_SKIN=x3 # plain "x" is probably the most common one

#SCP/FTP info -- host to receive the backup file
CP_SCP_USER=uname
CP_SCP_PASS=pword
CP_SCP_HOST=domain.com
CP_SCP_PORT=22

# see cPanel backup page for alternatives of transfer mode
CP_SCP_MODE=cp

# email address for sending notification after backup is completed
CP_EMAIL=email@gmail.com

# target directory on remote host; do not include domain names in this one
CP_SCP_BACKUP_ROOT=web_backup%2F  # url encoded, e.g. "/" equals "%2F"

###
# Config ends
### 



# creates the target path (directory and filename)
# if you make changes, remember to url encode slashes, i.e. "/" equals "%2F"
# if --year option is used, script creates a yearly filename, otherwise a monthly one
if [ "$1" = '--year' ]; then
	CP_SCP_RDIR=$CP_SCP_BACKUP_ROOT$CP_DOMAIN%2F$CP_DOMAIN-year-`date +%Y`.tar.gz
else
	CP_SCP_RDIR=$CP_SCP_BACKUP_ROOT$CP_DOMAIN%2F$CP_DOMAIN-month-`date +%m`.tar.gz
fi

# initiate backup (the following command should be on one line)
curl --silent --insecure --user $CP_USERNAME:$CP_PASSWORD -d "dest=$CP_SCP_MODE&email=$CP_EMAIL&server=$CP_SCP_HOST&user=$CP_SCP_USER&pass=$CP_SCP_PASS&port=$CP_SCP_PORT&rdir=$CP_SCP_RDIR" https://$CP_DOMAIN:2083/frontend/$CP_SKIN/backup/dofullbackup.html > /dev/null

# gets curl return code
CURL_EXIT=$?

# curl encountered an error
if [ $CURL_EXIT -gt 0 ]; then
	echo curl returned exit status $CURL_EXIT - see curl manual pages for more details
	exit 69
fi

# everything ok
exit 0

Open in new window

Avatar of AriMc
AriMc
Flag of Finland image

I'm using backup-manager. If your distribution has that available it should be quite easy to setup for basic tasks as the setup walks you through the necessary information.

If your distribution doesn't have it, you can download it from here:

   http://www.backup-manager.org/

As your needs get more complicated, you can edit the configuration file /etc/backup-manager.conf to do things like uploading the backups to remote hosts, backing up your databases etc.


Avatar of frostino

ASKER

AriMc,

I'm not quite sure how to use this.  I know it says it's easy but like I said, I have very little experience with this.  I looked at the INSTALL file and I'm not exactly sure what I'm expected to do to start.  The URL to the User Guide is no longer valid.  I'm also not sure what you mean about distribution.  Sorry for the lack of knowledge on this.  
Distribution is like your "brand" of Linux. They have names like Ubuntu, Fedora, Debian, SuSE, Mandriva etc. Do you recognize any of these as "your Linux"? We could start from that and see if Backup Manager is readily available there.

I'm using shared hosting in a linux environment.  I access most of my stuff through cPanel 11.  Beyond that, I can't tell you which is being used.
Ok, looking quickly at the cPanel website I'm inclined to say that your question is not  really related to Linux but to the cPanel software/service you're using. As they say on their website: "We recommend contacting your web host for assistance.".

I've already talked to him and he said I need to use the Cron Jobs feature and write my own script.  I'm looking for the script to use.  It's not really specific to cPanel.  I just don't know the syntax at all.
I guess this "Cron Jobs feature" is some layer in cPanel that is used to control what in normal Linux is know as crontab. Again it's a question specific to cPanel and its user interface, not Linux itself.





I looked around for a Zone that better fits this question and see none.  Previous questions pertaining to this are in OS - Linux - Linux Admin - CPanel but that zone no longer exists.  The answers to previous related questions are 100% clear to me.
To correct my previous statement, the previous related questions are NOT 100% clear to me.  Anyway, I'd like to let the question stand in case someone else would like to answer.  I do not believe the script is any different for cPanel than any other linux environment.  It's just a matter of knowing the syntax, which I don't.
The "Cron Jobs" in Linux is like a "Scheduled Task".  Cron is a "daemon" (in Windows, a "service") that runs to kick off other jobs at specified times.  

Here is a page that has a simple backup script and explains it pretty well.  (The "tar" program could be roughly thought of as something like pkzip or other file packaging program.)
https://help.ubuntu.com/8.04/serverguide/C/backup-shellscripts.html

The script you have above is one that requires some configuration of your domains/directories that you want to back up along with usernames and passwords in the upper config section.  (**Make sure you DON'T post a copy of the script with your modified values!**).  Did you actually put your values in the script?  (e.g. rather than "domain.com" your actual domain).
Mccracky:
Yes, I did plug in my values.  I edited them here so my values wouldn't be visible to the public.  I've got the script I posted to the point where my shared hosting server actually logs into my second server but doesn't transfer the backup file.  I'll look at your link and see if any of those scripts help me out more than this one.
Did you also create the subdirectories in the web_backup folder?

###
#  Instructions
###
# Make sure CP_SCP_BACKUP_ROOT directory exists. It is "web_backup" by default.
# Within this directory you'll need a directory for each cPanel account you're
# backing up.
#
# I.e. if you're backing up "example.com" and "foobar.com" and you want them
# to be backed up in the default location of "web_backup", you'll need to
# execute the following commands at the recieving end:
# mkdir ~/web_backup
# mkdir ~/web_backup/example.com
# mkdir ~/web_backup/foobar.com
###
mccracky, yes I did create the subdirectories.  in this case, only one subdirectory for one domain.
Frostino,

cpanekl does not give you much power..  Usually you cannot install anything..

This script will  backup the /home/user/public_html directory  as a  tar.gz archive   and place the archive in the backup folder.

Just schedule  the script with cpanels cron section after that.








#!/bin/bash
DATE=$(date +%Y-%m-%d)

tar zcvf /home/user/Backup/BackupWeb-$DATE.tar.gz  /home/user/public_html

Open in new window

You can edit the script to SCP the archive to another server.

 The command would be : scp  /home/user/Backup/BackupWeb-$DATE.tar.gz  xxx.xxx.xxx:/path

This will ask for a password..  check this procedure to get rid of the password prompt: http://linuxproblem.org/art_9.html

The script I'm using isn't installing anything, unless you're referencing someone else's response.  The thing about the script I am using and would like to use, if possible, is that it uses cPanel's full backup feature which also includes every folder on the account and the DBs.  It's successfully logging into the second server via FTP but its just not creating the file in the second server and not finding the folder the script is pointing to.


Net::FTP>>> Net::FTP(2.77)
Net::FTP>>>   Exporter(5.58)
Net::FTP>>>   Net::Cmd(2.29)
Net::FTP>>>   IO::Socket::INET(1.29)
Net::FTP>>>     IO::Socket(1.29)
Net::FTP>>>       IO::Handle(1.25)
Net::FTP=GLOB(0xa08a9e4)<<< 220 NASFTPD Turbo station 2.x 1.3.2e Server (ProFTPD) [::ffff:192.168.1.55]
Net::FTP=GLOB(0xa08a9e4)>>> USER myusername
Net::FTP=GLOB(0xa08a9e4)<<< 331 Password required for myusername
Net::FTP=GLOB(0xa08a9e4)>>> PASS ....
Net::FTP=GLOB(0xa08a9e4)<<< 230 User myusername logged in
Net::FTP=GLOB(0xa08a9e4)>>> TYPE I
Net::FTP=GLOB(0xa08a9e4)<<< 200 Type set to I
Net::FTP=GLOB(0xa08a9e4)>>> CWD alurium/2849CP_USERNAME-month-03.tar.gz
Net::FTP=GLOB(0xa08a9e4)<<< 550 alurium/2849CP_USERNAME-month-03.tar.gz: No such file or directory
Net::FTP=GLOB(0xa08a9e4)>>> ALLO 769027933
Net::FTP=GLOB(0xa08a9e4)<<< 202 No storage allocation necessary
Net::FTP=GLOB(0xa08a9e4)>>> PORT 208,68,156,18,137,34
Net::FTP=GLOB(0xa08a9e4)<<< 200 PORT command successful
Net::FTP=GLOB(0xa08a9e4)>>> STOR backup-3.9.2011_00-00-03_outofbre.tar.gz
Net::FTP=GLOB(0xa08a9e4)<<< 550 backup-3.9.2011_00-00-03_outofbre.tar.gz: Forbidden filename
Net::FTP=GLOB(0xa08a9e4)>>> QUIT
Net::FTP=GLOB(0xa08a9e4)<<< 221 Goodbye.
ASKER CERTIFIED SOLUTION
Avatar of frostino
frostino
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I went to another source (the cPanel forum) and found exactly what I needed.