Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm newbie in php, and i just wrote a small script to run mysqldump all DB into local folder, which is working fin, being written daily with date("Y-m-d-H-i-s").
Q.
I wanted to transfer that backup file, after it's done, to remote ftp server, where my issue is how can I read the daily date increment variable?
I know, it's a stupid question, sorry, i'm newbie, as I said, and no idea how write that variable. My script is below:
Note that you are overwiting $command all the time before executing it.
This BTW is an excellent example where scripting directly in bash is a better solution than doing this from any other scripting language. Altough your solution might eventually work, debugging and code maintenance is more difficult.
Thank you for your prompt jlinkels,
so according your suggestion, I'd re-write the script as below:
Quote:
<?php
$username = "root";
$password = "my_pw";
$hostname = "localhost";
$database = "my-refered-db";
$username =escapeshellcmd($username);
$password =escapeshellcmd($password);
$hostname =escapeshellcmd($hostname);
$database =escapeshellcmd($database);
$ftpusername = "username";
$ftppassword = "ftpw";
$ftphostname = "ftp-remote-server";
$backupdate=date("Y-m-d-H-i-s");
$backupFile='/home/backups/'.$backupdate . $database.'.sql';
$command = "mysqlcheck -Aao --auto-repair -u$username -p$password -h$hostname";
$command = "mysqldump -R -u$username -p$password -h$hostname $database > $backupFile";
$command = "ncftp"
$command = "open -u ftpusername -p ftppassword ftphostname"
$command = "put /home/backups/*" --> Here, would load all the existing backups to the ftp, right?
$command = "rm -rf /home/backups/*" --> Here to clean the rest of the existing file before go out, right?
system($command, $result);
echo $result;
?>
So, you think now it would be fine? What about if I want to delete old backups, for example, from the last 2 weeks, as we're doing daily backup, so no need to keep the old, and can go deleting the old over 15 days, for example, how should be in this case?
First, the first command 'ncftp' is never executed. Secondly, every time you execute the system call, a new shell is being openened. So the first call you open an ncftp connection, and close it. The next time you issue the password, it would open another shell and try to execute password as shell command.
If you insist on using PHP you could use ncftpput to do it all at once. Anyway, every time you compose a command with $command= you have to execute it by calling system. And then again, after you made your call, that command is finished. You can't open an FTP in one call and use it in the next call.
And I got it properly, all loaded, and fine... don't know where's the syntax error in your script!!
But, to get the script working, I just put the flags together in one line, and got it working properly... now it's working having them together... Even, it's not wrong, no? But not so nice, but work
I'm sorry to "break" the posts trying to find the solution, but...
I think that instead of "ncftp" I would use "lftp" (that is Lftp) which has options to provide the usr&pass and the commands in one line (which I'm using) and it tries to transfer the files until finished (reconnecting, resuming) opposing the usual ftp which can break and lose connection and then stops even if not complete file was transfered.
My "lftptransfer_script.sh"
Code:
#!/bin/sh
HOST='ftp.domain.com'
USER='ftpuser'
PASSWD='idontknow'
backupdate=$(date "+%F %T")
FILE="/home/backups/${backupdate}${database}.sql" <--- your file to transfer
lftp -c "open $HOST && user $USER $PASSWD && cd FOLDER_NAME_FOR_STORING/backups/ && put -c $FILE" <<END_SCRIPT
# FOLDER_NAME_FOR_STORING/backups/ change to directory name you are using
# all in one command that connects to HOST=ftp.domain.com with Username/password
# and changes directory to whatever you need
# then transfers the file
bye
exit
END_SCRIPT
exit 0
this script transfers my "backup" file TARred and Gzipped even if 20GB filesize and takes about 6 to 10 hours with avg. speed of 350kB/s (I think).
Hi jlinkels,
Sure, I run it as bash -x backupcron...
lithos, Thank you for the break, your suggestion is interesting also, as i was just looking around for the lftp, it seem to be more reasonable, as my db are large, one of them have even over 1gb.. so, i'd use this...
But, in both cases, i still doig something wrong, having the same file location error, as I give:
Quote:
FILE="/home/backups/${backupdate}${database}.sql"
And I still getting when i run the:
Quote:
bash -x backupcron
backupcron: línea 13: $FILE: redireccionamiento ambiguo
put: /home/backups/2012-07-14: No existe el fichero o el directorio
+ bye
backupcron: línea 15: bye: no se encontró la orden
+ exit
Something in this arguments are wrong, what I'm doing wrong in this synthesis?
The syntax of these two lines could be wrong:
Code:
backupdate=$(date +"%F %T") <--- see the order of + and "
backupFile="/home/backup/$backupdate-$database.sql" <-- this is how I would write the filename - without { }
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.