import os
import psycopg2
from functions import get_env, query_to_array

get_env('database_credentials.env')
connection = psycopg2.connect(database=os.getenv('DB_NAME'),
                              user=os.getenv('DB_USER'),
                              password=os.getenv('DB_USER_PASSWORD'),
                              host=os.getenv('DB_HOST'),
                              port=os.getenv('DB_PORT'))

query = '''SELECT * FROM history WHERE  order by id'''

not_accepted_array = []
query_to_array(connection, not_accepted_array, query)
length = len(not_accepted_array)
for i in range(0, length):
    print(not_accepted_array[i])
print(length)
# coding=UTF-8
# !/usr/bin/ python3
import os
import time
import pipes
from functions import get_env
from subprocess import Popen, PIPE

get_env('/Users/yamna/Projets/python_script/database_credentials.env')
db_host = os.getenv('DB_HOST')
db_user = os.getenv('DB_USER')
db_user_password = os.getenv('DB_USER_PASSWORD')
db_name = os.getenv('DB_NAME')
backup_path = os.getenv('BACKUP_PATH')

# Nom du dossier de sauvegarde à la date du jour
datetime = time.strftime('%d%m%Y-%H:%M:%S')
today_back_up_path = backup_path + datetime

# Creation du dossier de sauvegarde
try:
    os.stat(today_back_up_path)
except:
    os.makedirs(today_back_up_path)

print("Sauvegarde de la base de donnée " + db_name + " en cours...")

# Lancement de la commande du process de sauvegarde
back_up_command = "exec /usr/local/bin/pg_dump -h " + db_host + " -U " + db_user + " -Ft " \
                  + db_name + " > " + pipes.quote(today_back_up_path) \
                  + "/" + db_name + ".tar"
Exemple #3
0
# coding=UTF-8
# !/usr/bin/ python3
import os
import psycopg2
from sendgrid import SendGridAPIClient, Content
from sendgrid.helpers.mail import Mail
from functions import get_env, query_to_array
from builtins import Exception

get_env('/Users/yamna/Projets/Bankeschon/Script/database_credentials.env')
get_env('/Users/yamna/Projets/Bankeschon/Script/sendgrid.env')

connection = psycopg2.connect(database=os.getenv('DB_NAME'),
                              user=os.getenv('DB_USER'),
                              password=os.getenv('DB_USER_PASSWORD'),
                              host=os.getenv('DB_HOST'),
                              port=os.getenv('DB_PORT'))

# Récuperation des donnees client

query = '''SELECT surname, name, mail ,adapted_offers from prospects where is_customer = 1 order by id'''

customers_array = []
query_to_array(connection, customers_array, query)

# Parcours du tableau et envoie de mail aux clients correspondants

length = len(customers_array)
for i in range(0, length):
    adapted_offers_array = customers_array[i]['adapted_offers'].split('-', 3)
    customers_email = customers_array[i]['mail']
    signal.signal(signal.SIGINT, trap)
    signal.signal(signal.SIGTERM, trap)

    # import time
    # time.sleep(10)

    # allow bonjour/zeroconf
    helpers.run(
        'iptables -I INPUT -p udp --dport 5353 -d 224.0.0.251 -m comment --comment "searchExamServer" -j ACCEPT'
    )
    helpers.run(
        'iptables -I OUTPUT -p udp --dport 5353 -d 224.0.0.251 -m comment --comment "searchExamServer" -j ACCEPT'
    )

    display = helpers.get_env("DISPLAY")
    xauthority = helpers.get_env("XAUTHORITY")
    env = {
        'DISPLAY': display,
        'XAUTHORITY': xauthority,
        'LANG': helpers.get_env('LANG')
    }

    # create the directory structure and cleanup
    for path in [
            'newroot', 'base', 'exam', 'tmpfs', 'squashfs', 'backup/home/user'
    ]:
        os.makedirs('/run/initramfs/' + path, exist_ok=True)
    os.chown('/run/initramfs/backup/home/user', 1000,
             1000)  # @todo evtl. shutil.chown() accepts names
    if os.path.exists(retoreStateFile): os.remove(retoreStateFile)