php rsnapshot config generator, tabs does not work properly

351 Views Asked by At

I am trying to create rsnapshot config files using a script, the scripts creates the file and uploads it to the backup server just fine.

Note: both servers are running centos6

Once i try to run it on the backup server i get following errors:

ERROR: /backup/config/rsnapshot_56.conf on line 2:
ERROR: config_version 1.2 - config_version not recognized!
ERROR: /backup/config/rsnapshot_56.conf on line 12:
ERROR: no_create_root 1 - no_create_root must be set to either 1 or 0
ERROR: /backup/config/rsnapshot_56.conf on line 15:
ERROR: rsync_short_args -az - rsync_short_args "-az " not in correct format
ERROR: /backup/config/rsnapshot_56.conf on line 17:
ERROR: interval daily 10 - "10 " is not a legal value for a retention count
ERROR: /backup/config/rsnapshot_56.conf on line 18:
ERROR: interval weekly 4 - "4 " is not a legal value for a retention count
ERROR: /backup/config/rsnapshot_56.conf on line 20:
ERROR: interval monthly 0 - "0 " is not a legal value for a retention count
ERROR: /backup/config/rsnapshot_56.conf on line 23:
ERROR: loglevel 5 - loglevel must be a value between 1 and 5
ERROR: ---------------------------------------------------------------------
ERROR: Errors were found in /backup/config/rsnapshot_56.conf,
ERROR: rsnapshot can not continue. If you think an entry looks right, make
ERROR: sure you don't have spaces where only tabs should be.

According to the errors it would seen that the config file contains spaces which is not allow in rsnapshot, but the file does not have any?

$argv[1] is equal to an id in the database.
The php looks as following:

$source_file = "temp.conf";
$remote_file = "rsnapshot_".$argv[1].".conf";
$ftp_server = "xxxx";
$ftp_user_name = "xxxx";
$ftp_user_pass = "xxxx";

$config_sql = "SELECT * FROM xxxx WHERE id = ". $argv[1];
$config_result = mysqli_query($con, $config_sql);
$config_row = mysqli_fetch_array($config_result);

# Create/Edit the temp file with the config that is going to be uploaded
$config_file = fopen($source_file, "w+b") or die("Unable to open file!");
$config_content = "";
$config_content .= "config_version\t1.2\n\n";
$config_content .= "# Path to binaries\n";
$config_content .= "cmd_rsync\t/usr/bin/rsync\n";
$config_content .= "cmd_ssh\t/usr/bin/ssh\n";
$config_content .= "cmd_cp\t/bin/cp\n";
$config_content .= "cmd_rm\t/bin/rm\n";
$config_content .= "cmd_du\t/usr/bin/du\n";
$config_content .= "cmd_logger\t/usr/bin/logger\n\n";
$config_content .= "no_create_root\t1\n\n";
$config_content .= "# Rsync args\n";
$config_content .= "rsync_short_args\t-az\n\n";
$config_content .= "# Interval of how many backups to keep\n";
$config_content .= "interval\tdaily\t" . $config_row['daily'] . "\n";
$config_content .= "interval\tweekly\t" . $config_row['weekly'] . "\n";
$config_content .= "interval\tmonthly\t" . $config_row['monthly'] . "\n\n";
$config_content .= "# Level og verbosity (1 = lowest, 5 = highest)\n";
$config_content .= "loglevel\t5\n\n";
$config_content .= "# Rsync args\n";
$config_content .= "rsync_long_args\t--stats --delete --numeric-ids --delete-excluded --port " . $config_row['port'] . "\n\n";
$config_content .= "# Root folder, logfile, lockfile\n";
$config_content .= "snapshot_root\t" . $config_row['filepath'] . "\n";
$config_content .= "logfile\t" . $config_row['filepath'] . "/rsnapshot.log\n";
$config_content .= "lockfile\t" . $config_row['filepath'] . "/rsnapshot.pid\n\n";
$config_content .= "# Folders that gets excluded\n";
$config_content .= "exclude\t.recycle/\n\n";
$config_content .= "# Backup commands pr folder\n";

$config_folders = explode(',',$config_row['folders']);
foreach ($config_folders as $value) {
    $config_content .= "backup\trsync://backup@" . $config_row['ip'] . "/".$value."\t".$value."/\n";
}

fwrite($config_file, $config_content);
fclose($config_file);

# Set up basic connection
$conn_id = ftp_connect($ftp_server);

# login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

# upload the config file
if (ftp_put($conn_id, $remote_file, $source_file, FTP_ASCII)) {
    echo "successfully uploaded $source_file\n";
} 
else {
    echo "There was a problem while uploading $source_file\n";
}

# close the connection
ftp_close($conn_id);
?>

Is it because of the \t? and if thats the case what can be done to come by this?
Any help is appreciated.

1

There are 1 best solutions below

0
On

To fix the issue with incorrect tabs use following comand on the generated files:

sed -i -e 's/\r$//' /path/to/rsnapshot.conf

the sed utility in linux will parses and transforms text.