Posts Tagged ‘Servers’

Back up a cPanel server via SSHFS

November 13th, 2009 No comments

So you have a cPanel server and want to set up automated, off-site backups, just in case anything should happen? This can be done easily with a FUSE (filesystem in userspace) module called SSHFS, and requires a minimum of setup. SSHFS allows one to mount any remote directory accessible via SFTP to a local filesystem. Cool, huh?

All you really need to accomplish this is access to a server via SSH with sufficient disk space to hold your backups, and the FUSE module available on your server. (Many VPS providers are willing to enable this for you if it is not already enabled)

Notice: The following howto assumes that you are a competent linux/unix administrator. I take no responsibility for any damage or data loss which may be caused by following these instructions.

Step one: Set up a passwordless SSH key on the cPanel server.

First, we need to generate an SSH key:
Be sure you leave the passphrase option blank, or the automatic part won’t work so well :). The default options will work fine for this.

Now, you will need to get the public key from the cPanel server that you will be importing:
cat ~/.ssh/
The above assumes you did not choose a DSA key, or opt to store it in a location other than default. Copy the text that the above command outputs.

Now, move over to your remote/backup server, and run the following:
echo "text you copied from the cPanel server" >> ~/.ssh/authorized_keys
chmod 640 ~/.ssh/authorized_keys

Next, go back to the cPanel server and test to ensure that the public key login works:
ssh user@remote_server
If the public key is installed correctly, you will be logged into the remote server without any password prompts.

Step two: install SSHFS

For purposes of this howto, I’m assuming you are using CentOS, in which case you can install sshfs with the following command:
yum install sshfs
Say yes to any dependencies it may prompt you to install.

Step three: Configure the fstab on your cPanel server to mount a backup directory

Assuming you want your backups to go to /backup on the local cPanel server (replacing /path/to/storage with the appropriate path on the remote server):
mkdir /backup
echo "sshfs#user@remote_server:/path/to/storage /backup fuse noauto,compression=yes,nonempty" >> /etc/fstab

Test this mount with the command “mount /backup” If you receive no errors, the mount should be working correctly.

Step four: Configure the cPanel backup

Visit the address https://_your_cpanel_server_:2087/scripts/backupset
in your web browser, and configure the settings to your liking, making sure to select the following:
Backup Status: Enabled
Remount/Unmount backup drive: Enabled
Bail out if backup drive mount fails: Enabled
Backup destination: /backup

Step five: Profit

Now, every night at 1AM (server time) your server will automatically back up to the remote server via SSH.

Categories: Uncategorized Tags: , ,

Quick Summary of User Home Sizes

April 28th, 2009 No comments

I had a niche need to make an easy summary of user directory sizes for an FTP server where the users’ homes could be in one of a few locations. I whipped up this script which should make it easy even for an ultra-novice to get this summary:

#!/usr/bin/env python

import subprocess

def run_this(commandline):
    process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    out,err = process.communicate()
    return out

print "Now calculating home directory size for all non-system users.\nThis may take a few minutes, please wait."

file = open('/etc/passwd','r')
userlist = list()
for line in file:
    user = line.split(':')
    if int(user[2]) >= 1000 and "nobody" not in user[0]:
        usage = run_this("du -hs " + user[5] + " | cut -f 1")
        userlist.append([user[4].split(',',1)[0], usage.strip()])

print "\nStorage Consumed by Users"
print "------------------------------------------------\n"

for userinfo in userlist:
    print userinfo[0] + "\t\t\t" + userinfo[1]

print "\n"

Categories: Coding, Servers Tags: , , ,

Quickly find disabled emails in Virtualmin

April 8th, 2009 2 comments

Anybody who’s adminned a Virtualmin server has probably had update hell.  One that I personally have had to wrangle with is where Virtualmin will randomly disable email accounts, resulting in frustrated users calling you when noone can email them. I’ve written a quick Python script to dump out a list of all disabled email accounts to reduce the inevitable frustration caused by this issue, allowing one to quickly find all of the disabled emails and re-enable them, hopefully before anyone complains. 🙂

#/usr/bin/env python

file = open('/etc/postfix/virtual','r')
activemails = list()
for line in file:
        if '@' in line:

file = open ('/etc/passwd','r')
for line in file:
        if '@' in line and '/bin/false' not in line:
                email = line.split(':',1)[0]
                if email not in activemails:
                        print email

Categories: Coding, Servers Tags: , , ,