Skip to content

Instantly share code, notes, and snippets.

@isDipesh
Created October 19, 2012 11:20
Show Gist options
  • Save isDipesh/3917689 to your computer and use it in GitHub Desktop.
Save isDipesh/3917689 to your computer and use it in GitHub Desktop.
Script to transfer a website from one host to another. Moves files with FTP and MySQL data with dumping and importing.
#!/usr/bin/python
#SSH into the new server, and run this from the home directory. Granted ~/public_html is the Apache DocumentRoot for
#the user
import getpass
import os
print '\nThis script will automate the process of moving a website from one server to another.'
db_name = 'dump'
config = {
'old_ip' : '174.120.168.90',
#db_dump_file_name = 'dump.sql',
'delete_dump_after_import' : True,
}
def ftp_download(config):
print '\nProvide FTP login for old/source server:'
ftp_username = raw_input('FTP Username: ')
ftp_password = getpass.getpass('FTP Password: ');
print '\nDownloading public_html folder..'
rtrn = 0
rtrn = os.system('wget -m ftp://'+config['old_ip']+'/public_html --ftp-user="'+ftp_username+'" --ftp-password="'+ftp_password+'" --no-host-directories --tries=3')
if rtrn != 0:
print '\nFTP download failed with error code', rtrn, ', exiting!'
return
print '\nFile transfer finished!'
return
def db_download(config):
print '\nDatabase details for source server:'
global db_name
db_name = raw_input('Database Name: ')
db_username = raw_input('MySQL Username: ')
db_password = getpass.getpass('MySQL Password: ')
print '\nDownloading db dump, grab a cup of coffee as this may take a while depending upon the db size'
print 'Make sure you have added the current host/ip to remote database acess host list in the server'
os.system('mysqldump -u '+db_username+' --password="'+db_password+'" -h '+config['old_ip']+' '+db_name+' | pv -ptrbe >'+db_name+'.sql')
print '\nDatabase dump download finished!'
return
def db_import(config):
print '\nDatabase details for target server:'
new_db_name = raw_input('Database Name:')
new_db_username = raw_input('MySQL Username:')
new_db_pass = getpass.getpass('MySQL Password:')
rtrn =0
rtrn = os.system('mysql -u '+new_db_username+' --password='+new_db_pass+' '+new_db_name+' < '+db_name+'.sql')
if rtrn != 0:
print '\nDatabase import failed with error code', rtrn, ', exiting!'
return
if config['delete_dump_after_import']:
os.remove(db_name+'.sql')
print '\nDatabase dump deleted'
print '\nDatabase import successful! Have fun!'
return
while True:
print '\n\n\n1. Download files with FTP.'
print '2. Download database dump.'
print '3. Import database dump.'
print '4. Exit!\n'
inp = raw_input('Enter your choice: ')
if inp == '1':
ftp_download(config)
if inp == '2':
db_download(config)
if inp == '3':
db_import(config)
if inp == '4':
print '\nBye Bye!\n'
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment