Solved

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

Posted on 2011-03-03
18
621 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:frostino
  • 10
  • 4
  • 2
  • +1
18 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 35030918
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.


0
 

Author Comment

by:frostino
ID: 35031518
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.  
0
 
LVL 9

Expert Comment

by:AriMc
ID: 35031920
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.

0
 

Author Comment

by:frostino
ID: 35031935
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.
0
 
LVL 9

Expert Comment

by:AriMc
ID: 35032001
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.".

0
 

Author Comment

by:frostino
ID: 35032026
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.
0
 
LVL 9

Expert Comment

by:AriMc
ID: 35032136
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.





0
 

Author Comment

by:frostino
ID: 35032236
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.
0
 

Author Comment

by:frostino
ID: 35032249
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:mccracky
ID: 35037116
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).
0
 

Author Comment

by:frostino
ID: 35037185
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.
0
 
LVL 12

Expert Comment

by:mccracky
ID: 35037695
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
###
0
 

Author Comment

by:frostino
ID: 35062690
mccracky, yes I did create the subdirectories.  in this case, only one subdirectory for one domain.
0
 
LVL 2

Expert Comment

by:maxalarie
ID: 35084602
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

0
 
LVL 2

Expert Comment

by:maxalarie
ID: 35084672
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

0
 

Author Comment

by:frostino
ID: 35084747
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.
0
 

Accepted Solution

by:
frostino earned 0 total points
ID: 35101079
I found a PHP script that someone gave me on the cPanel forum.  
http://forums.cpanel.net/f42/automatic-remote-ftp-backup-script-196402.html#post805751
0
 

Author Closing Comment

by:frostino
ID: 35135915
I went to another source (the cPanel forum) and found exactly what I needed.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now