Hello,
I share with you what I’ve setup for the backups of all siteworx accounts.
I have a NAS account with SAGO to store all my backups.
So the idea is :
- to store in this space (distant server) all siteworx backup files.
- to create a cronjob and use the iworx backup command
- to have only the last backup files. Not all previous backuped files.
The problem is that you can’t acces with SSH to this NAS space.
So you can’t create a script to delete all previous backup files.
The solution I found is to use SFTP to run some commands as “rm”.
So we first connect to this distant server with SFTP to delete all previous backup files, then use the iworx backup command with the scp option to transfer all the new backup files to the distant server.
One time again there is a pbm.
SFTP can use a file to run commands in batch mode but you can’t give it a clear password.
You have to use public/private key to login to the distant server.
So, we will have to :
1- Create dsa key
2- Move the public key to the distant server
3- Create a file with all necessary SFTP command to be used in batch mode
4- Create the transfer.ini file for the iworx backup command
5- Create the final script that call the SFTP and iworx backup commands
6- Create the cronjob
1- Create the DSA KEY
Login on root on SSH on your system.
mkdir /root/key
chmod 700 /root/key
ssh-keygen -t dsa -b 2048 -f /root/key/host
You can replace host with your actual hostname (of your box).
When it asked you for the challenge phrase, let it empty and press enter twice
2- Copy the public key on the distant server
On your local server, do :
chmod 600 /root/key/*
cd /root/key
scp -P 22 host.pub user@hostname.tld:~
You may replace “host” with the hostname of your box. It has to be the same than 1-
Now we have to create a directory on the distant server.
Login using your favourite FTP client.
Make sure you set it such that you can view hidden files.
- Create a directory .ssh
- Move the host.pub into the newly created directory
- Rename “host.pub” to “authorized_keys”
- set it to mode 600.
If your have multiple systems, you would need to merge all your public keys into 1 file before uploading to your distant box. You can do so on another machine. You can use the following command:
cat hostname.pub >> merged.pub
then rename merged.pub to “authorized_keys”
Now your system is ready to connect with SFTP/SCP to the distant server without asking you for a passphrase or a password.
Test it by doing
sftp -o IdentityFile=/root/key/host user@hostname.domain.tld
You should be connected without entered a password or passphrase
3- Create a file with all necessary SFTP command to be used in batch mode
On your local server, do
touch /root/sftpcmd
vi /root/sftpcmd
enter these two lines
rm ~/yourbackupdir/* (or domain* or domain.tld)
exit
4- Create the transfer.ini file for the iworx backup command
This file is used by the iworx backup command to store you login information to the distant server
On your local server, do :
touch /root/transfer.ini
chown root:iworx /root/transfer.ini
chmod 640 /root/transfer.ini
vi /root/transfer.ini
enter these lines in your transfer.ini file
[siteworx.backup_transfer_data]
username=“user”
password=“your password”
hostname=“hostname.domaine.tld”
remotefile=“~/yourbackupdir/”
Exit and save
5- Create the final script that call the SFTP and iworx backup commands
On your local server, do
touch /root/iworxbackup
chmod +x /root/iworxbackup
vi /root/iworxbackup
enter these lines in your iworxbackup script
#!/bin/bash
sftp -b /root/sftpcmd -o IdentityFile=/root/key/host user@hostname.domain.tld #to delete all previous backup files
/home/interworx/bin/backup.pex --domains=all --scp --transfer-ini=/root/transfer.ini #to store new backup files
exit and save.
The backup script is ready. It will delete all previous backup file, backups all siteworx account and send the new backup files to the distant server
you can test it for one or two domains before creating the cronjob
Edit the iworxbackup script and change the iworx backup command by
/home/interworx/bin/backup.pex --domains=domain1.tld,domain2.tld --scp --transfer-ini=/root/transfer.ini
Also, think to change the sftpcmd file if need. (to delete these two domains : rm ~/yourbackupdir/* (or domain* or domain.tld) )
Everything should work fine.
6- Create the cronjob
Now if everything was going fine you may create a cronjob that will call this script every day for example. do
crontab -e
and add this line
30 5 * * * /root/iworxbackup >> /dev/null 2>&1
This example will run the backup job every day at 5:30 AM
<<<<<<<<< END >>>>>>>
Hope this may help some of you.
Of course if you have access to your distant box with SSH, it will be more easy to create a script that connect to the distant server with SSH and execute a rm command before running the iworx backup command
Pascal